-
Notifications
You must be signed in to change notification settings - Fork 2
/
header_test.go
156 lines (134 loc) · 5.63 KB
/
header_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright (c) Jeevanandam M (https://github.com/jeevatkm)
// go-aah/ahttp source code and usage is governed by a MIT style
// license that can be found in the LICENSE file.
package ahttp
import (
"net/http"
"net/url"
"testing"
"aahframework.org/test.v0/assert"
)
func TestHTTPParseAcceptHeaderLanguage(t *testing.T) {
req1 := createRawHTTPRequest(HeaderAcceptLanguage, "en-us;q=0.0,en;q=0.7, da, en-gb;q=0.8")
specs := ParseAccept(req1, HeaderAcceptLanguage)
assert.Equal(t, "da", specs.MostQualified().Value)
assert.Equal(t, 4, len(specs))
assert.Equal(t, specs[1].Value, "en-gb")
assert.Equal(t, specs[1].Q, float32(0.8))
assert.Equal(t, specs[2].Value, "en")
assert.Equal(t, specs[2].Q, float32(0.7))
req2 := createRawHTTPRequest(HeaderAcceptLanguage, "en-gb;leve=1;q=0.8, da, en;level=2;q=0.7, en-us;q=gg")
specs = ParseAccept(req2, HeaderAcceptLanguage)
assert.Equal(t, "da", specs.MostQualified().Value)
assert.Equal(t, 4, len(specs))
assert.Equal(t, specs[1].Value, "en-gb")
assert.Equal(t, specs[1].Q, float32(0.8))
assert.Equal(t, specs[2].Value, "en")
assert.Equal(t, specs[2].Q, float32(0.7))
req3 := createRawHTTPRequest(HeaderAcceptLanguage, "zh, en-us; q=0.8, en; q=0.6")
specs = ParseAccept(req3, HeaderAcceptLanguage)
assert.Equal(t, "zh", specs.MostQualified().Value)
assert.Equal(t, 3, len(specs))
assert.Equal(t, specs[1].Value, "en-us")
assert.Equal(t, specs[1].Q, float32(0.8))
assert.Equal(t, specs[2].Value, "en")
assert.Equal(t, specs[2].Q, float32(0.6))
req4 := createRawHTTPRequest(HeaderAcceptLanguage, "en-gb;q=0.8, da, en;level=2;q=0.7, en-us;leve=1;q=gg")
specs = ParseAccept(req4, HeaderAcceptLanguage)
assert.Equal(t, "da", specs.MostQualified().Value)
assert.Equal(t, 4, len(specs))
assert.Equal(t, specs[1].Value, "en-gb")
assert.Equal(t, specs[1].Q, float32(0.8))
assert.Equal(t, specs[2].Value, "en")
assert.Equal(t, specs[2].Q, float32(0.7))
req5 := createRawHTTPRequest(HeaderAcceptLanguage, "zh, en-us; q=wrong, en; q=0.6")
specs = ParseAccept(req5, HeaderAcceptLanguage)
assert.Equal(t, "zh", specs.MostQualified().Value)
assert.Equal(t, 3, len(specs))
assert.Equal(t, specs[1].Value, "en")
assert.Equal(t, specs[1].Q, float32(0.6))
req6 := createRawHTTPRequest(HeaderAcceptLanguage, "es-mx, es, en")
specs = ParseAccept(req6, HeaderAcceptLanguage)
assert.Equal(t, "es-mx", specs.MostQualified().Value)
assert.Equal(t, 3, len(specs))
assert.Equal(t, specs[1].Value, "es")
assert.Equal(t, specs[1].Q, float32(1.0))
assert.Equal(t, specs[2].Value, "en")
assert.Equal(t, specs[2].Q, float32(1.0))
}
func TestHTTPNegotiateLocale(t *testing.T) {
req1 := createRawHTTPRequest(HeaderAcceptLanguage, "es-mx, es, en")
locale := NegotiateLocale(req1)
assert.Equal(t, "es-mx", locale.Raw)
assert.Equal(t, "es", locale.Language)
assert.Equal(t, "mx", locale.Region)
assert.Equal(t, "es-mx", locale.String())
req2 := createRawHTTPRequest(HeaderAcceptLanguage, "es")
locale = NegotiateLocale(req2)
assert.Equal(t, "es", locale.Raw)
assert.Equal(t, "es", locale.Language)
assert.Equal(t, "", locale.Region)
assert.Equal(t, "es", locale.String())
req3 := createRawHTTPRequest(HeaderAcceptLanguage, "")
locale = NegotiateLocale(req3)
assert.Equal(t, true, locale == nil)
locale = NewLocale("en-CA")
assert.Equal(t, "en-CA", locale.Raw)
assert.Equal(t, "en", locale.Language)
assert.Equal(t, "CA", locale.Region)
assert.Equal(t, "en-CA", locale.String())
}
func TestHTTPNegotiateEncoding(t *testing.T) {
req1 := createRawHTTPRequest(HeaderAcceptEncoding, "compress;q=0.5, gzip;q=1.0")
areq1 := AcquireRequest(req1)
encoding := areq1.AcceptEncoding()
assert.True(t, areq1.IsGzipAccepted)
assert.Equal(t, "gzip", encoding.Value)
assert.Equal(t, "gzip;q=1.0", encoding.Raw)
req2 := createRawHTTPRequest(HeaderAcceptEncoding, "gzip;q=1.0, identity; q=0.5, *;q=0")
areq2 := AcquireRequest(req2)
encoding = areq2.AcceptEncoding()
assert.True(t, areq2.IsGzipAccepted)
assert.Equal(t, "gzip", encoding.Value)
assert.Equal(t, "gzip;q=1.0", encoding.Raw)
req3 := createRawHTTPRequest(HeaderAcceptEncoding, "")
encoding = NegotiateEncoding(req3)
assert.Equal(t, true, encoding == nil)
req4 := createRawHTTPRequest(HeaderAcceptEncoding, "compress;q=0.5")
areq4 := AcquireRequest(req4)
encoding = areq4.AcceptEncoding()
assert.False(t, areq4.IsGzipAccepted)
assert.Equal(t, "compress", encoding.Value)
assert.Equal(t, "compress;q=0.5", encoding.Raw)
}
func TestHTTPAcceptHeaderVendorType(t *testing.T) {
req := createRawHTTPRequest(HeaderAccept, "application/vnd.mycompany.myapp.customer-v2.2+json")
ctype := NegotiateContentType(req)
assert.Equal(t, "application/json", ctype.Mime)
assert.Equal(t, "mycompany.myapp.customer", ctype.Vendor())
assert.Equal(t, "2.2", ctype.Version())
req = createRawHTTPRequest(HeaderAccept, "application/vnd.mycompany.myapp.customer+json")
ctype = NegotiateContentType(req)
assert.Equal(t, "application/json", ctype.Mime)
assert.Equal(t, "mycompany.myapp.customer", ctype.Vendor())
assert.Equal(t, "", ctype.Version())
req = createRawHTTPRequest(HeaderAccept, "application/vnd.api+json")
ctype = NegotiateContentType(req)
assert.Equal(t, "application/json", ctype.Mime)
assert.Equal(t, "api", ctype.Vendor())
assert.Equal(t, "", ctype.Version())
req = createRawHTTPRequest(HeaderAccept, "application/json")
ctype = NegotiateContentType(req)
assert.Equal(t, "application/json", ctype.Mime)
assert.Equal(t, "", ctype.Vendor())
assert.Equal(t, "", ctype.Version())
}
func createRawHTTPRequest(hdrKey, value string) *http.Request {
hdr := http.Header{}
hdr.Set(hdrKey, value)
url, _ := url.Parse("http://localhost:8080/testpath")
return &http.Request{
URL: url,
Header: hdr,
}
}