forked from fiorix/freegeoip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoder_test.go
236 lines (225 loc) · 5.25 KB
/
encoder_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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// Copyright 2009-2014 The freegeoip authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package freegeoip
import (
"bytes"
"encoding/csv"
"encoding/json"
"encoding/xml"
"io/ioutil"
"net/http"
"testing"
"github.com/robertkrimen/otto"
)
func TestCSVEncoder(t *testing.T) {
_, srv, err := runServer("/csv/", &CSVEncoder{})
if err != nil {
t.Fatal(err)
}
defer srv.Close()
resp, err := http.Get(srv.URL + "/csv/8.8.8.8")
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
t.Fatal(resp.Status)
}
row, err := csv.NewReader(resp.Body).Read()
if err != nil {
t.Fatal(err)
}
if row[1] != "US" {
t.Fatalf("Unexpected country code in record: %#v", row)
}
}
func TestXMLEncoder(t *testing.T) {
_, srv, err := runServer("/xml/", &XMLEncoder{})
if err != nil {
t.Fatal(err)
}
defer srv.Close()
resp, err := http.Get(srv.URL + "/xml/8.8.8.8")
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
t.Fatal(resp.Status)
}
var record responseRecord
err = xml.NewDecoder(resp.Body).Decode(&record)
if err != nil {
t.Fatal(err)
}
if record.CountryCode != "US" {
t.Fatalf("Unexpected country code in record: %#v", record.CountryCode)
}
}
func TestXMLEncoderIndent(t *testing.T) {
// TODO: validate indentation?
_, srv, err := runServer("/xml/", &XMLEncoder{Indent: true})
if err != nil {
t.Fatal(err)
}
defer srv.Close()
resp, err := http.Get(srv.URL + "/xml/8.8.8.8")
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
t.Fatal(resp.Status)
}
var record responseRecord
err = xml.NewDecoder(resp.Body).Decode(&record)
if err != nil {
t.Fatal(err)
}
if record.CountryCode != "US" {
t.Fatalf("Unexpected country code in record: %#v", record.CountryCode)
}
}
func TestJSONEncoder(t *testing.T) {
_, srv, err := runServer("/json/", &JSONEncoder{})
if err != nil {
t.Fatal(err)
}
defer srv.Close()
resp, err := http.Get(srv.URL + "/json/8.8.8.8")
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
t.Fatal(resp.Status)
}
var record responseRecord
err = json.NewDecoder(resp.Body).Decode(&record)
if err != nil {
t.Fatal(err)
}
if record.CountryCode != "US" {
t.Fatalf("Unexpected country code in record: %#v", record.CountryCode)
}
}
func TestJSONEncoderIndent(t *testing.T) {
// TODO: validate indentation?
_, srv, err := runServer("/json/", &JSONEncoder{Indent: true})
if err != nil {
t.Fatal(err)
}
defer srv.Close()
resp, err := http.Get(srv.URL + "/json/8.8.8.8")
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
t.Fatal(resp.Status)
}
var record responseRecord
err = json.NewDecoder(resp.Body).Decode(&record)
if err != nil {
t.Fatal(err)
}
if record.CountryCode != "US" {
t.Fatalf("Unexpected country code in record: %#v", record.CountryCode)
}
}
func TestJSONPEncoder(t *testing.T) {
_, srv, err := runServer("/json/", &JSONEncoder{})
if err != nil {
t.Fatal(err)
}
defer srv.Close()
resp, err := http.Get(srv.URL + "/json/8.8.8.8?callback=f")
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
t.Fatal(resp.Status)
}
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
code := bytes.NewBuffer([]byte(`
function f(record) {
set(record.country_code);
};
`))
code.Write(b)
vm := otto.New()
var countryCode string
vm.Set("set", func(call otto.FunctionCall) otto.Value {
if len(call.ArgumentList) > 0 {
countryCode = call.Argument(0).String()
}
return otto.UndefinedValue()
})
_, err = vm.Run(code.Bytes())
if err != nil {
t.Fatal(err)
}
if countryCode != "US" {
t.Fatalf("Unexpected country code in record: %#v", countryCode)
}
}
func TestRequestLang(t *testing.T) {
r := http.Request{}
list := requestLang(&r)
if list != nil {
t.Fatal("Unexpected list is not nil")
}
r.Header = map[string][]string{
"Accept-Language": {"en-us,en;q=0.5"},
}
want := []string{"en-us", "en"}
list = requestLang(&r)
if len(list) != 2 {
t.Fatal("Unexpected list length:", len(list))
}
for i, lang := range want {
if list[i] != lang {
t.Fatal("Unexpected item in list:", list[i])
}
}
}
func TestLocalizedName(t *testing.T) {
names := map[string]string{
"de": "USA",
"en": "United States",
"es": "Estados Unidos",
"fr": "États-Unis",
"ja": "アメリカ合衆国",
"pt-BR": "Estados Unidos",
"ru": "Сша",
"zh-CN": "美国",
}
mkReq := func(lang string) *http.Request {
return &http.Request{
Header: map[string][]string{
"Accept-Language": {lang},
},
}
}
test := map[string]string{
"pt-BR,en": "Estados Unidos",
"pt-br": "United States",
"es-ES,ru;q=0.8,q=0.2": "Сша",
"da, en-gb;q=0.8, en;q=0.7": "United States",
"da, fr;q=0.8, en;q=0.7": "États-Unis",
"da, de;q=0.5, zh-CN;q=0.8": "USA", // TODO: Use qvalue.
"da, es": "Estados Unidos",
"es-ES, ja": "アメリカ合衆国",
}
for k, v := range test {
name := localizedName(names, requestLang(mkReq(k)))
if name != v {
t.Fatalf("Unexpected name: want %q, have %q", v, name)
}
}
}