-
Notifications
You must be signed in to change notification settings - Fork 3
/
http_test.go
156 lines (143 loc) · 4.83 KB
/
http_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) 2020 Bojan Zivanovic and contributors
// SPDX-License-Identifier: MIT
package address_test
import (
"encoding/json"
"net/http"
"net/http/httptest"
"reflect"
"testing"
"github.com/bojanz/address"
)
// testFormat is a reduced format for testing purposes.
type testFormat struct {
Locale string `json:"locale"`
Layout string `json:"layout"`
RegionType string `json:"region_type"`
Regions map[string]string `json:"regions"`
}
func TestFormatHandlerNoLocale(t *testing.T) {
req, err := http.NewRequest("GET", "/address-formats", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler := address.FormatHandler{}
handler.ServeHTTP(rr, req)
if status := rr.Code; status != http.StatusOK {
t.Errorf("got HTTP %v want HTTP %v", status, http.StatusOK)
}
if contentType := rr.Header().Get("Content-Type"); contentType != "application/json" {
t.Errorf("got %v want %v", contentType, "application/json")
}
if contentLanguage := rr.Header().Get("Content-Language"); contentLanguage != "en" {
t.Errorf("got %v want %v", contentLanguage, "en")
}
var data map[string]testFormat
err = json.Unmarshal(rr.Body.Bytes(), &data)
if err != nil {
t.Fatal(err)
}
format, ok := data["TW"]
wantFormat := address.GetFormat("TW")
wantRegions := make(map[string]string)
for _, regionID := range wantFormat.Regions.Keys() {
wantRegions[regionID], _ = wantFormat.Regions.Get(regionID)
}
if !ok {
t.Errorf(`address format "TW" not found.`)
}
// Confirm that Locale and RegionType were correctly converted to strings.
if format.Locale != wantFormat.Locale.String() {
t.Errorf("got %v, want %v", format.Locale, wantFormat.Locale.String())
}
if format.RegionType != wantFormat.RegionType.String() {
t.Errorf("got %v, want %v", format.Layout, wantFormat.Layout)
}
// Confirm that the correct layout and regions were selected.
if format.Layout != wantFormat.Layout {
t.Errorf("got %q, want %q", format.Layout, wantFormat.Layout)
}
if !reflect.DeepEqual(format.Regions, wantRegions) {
t.Errorf("got %v, want %v", format.Regions, wantRegions)
}
}
func TestFormatHandlerLocaleQuery(t *testing.T) {
req, err := http.NewRequest("GET", "/address-formats?locale=zh", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler := address.FormatHandler{}
handler.ServeHTTP(rr, req)
if status := rr.Code; status != http.StatusOK {
t.Errorf("got HTTP %v want HTTP %v", status, http.StatusOK)
}
if contentType := rr.Header().Get("Content-Type"); contentType != "application/json" {
t.Errorf("got %v want %v", contentType, "application/json")
}
if contentLanguage := rr.Header().Get("Content-Language"); contentLanguage != "zh" {
t.Errorf("got %v want %v", contentLanguage, "zh")
}
var data map[string]testFormat
err = json.Unmarshal(rr.Body.Bytes(), &data)
if err != nil {
t.Fatal(err)
}
format, ok := data["TW"]
wantFormat := address.GetFormat("TW")
wantRegions := make(map[string]string)
for _, regionID := range wantFormat.LocalRegions.Keys() {
wantRegions[regionID], _ = wantFormat.LocalRegions.Get(regionID)
}
if !ok {
t.Errorf(`address format "TW" not found.`)
}
// Confirm that the correct layout and regions were selected.
if format.Layout != wantFormat.LocalLayout {
t.Errorf("got %q, want %q", format.Layout, wantFormat.LocalLayout)
}
if !reflect.DeepEqual(format.Regions, wantRegions) {
t.Errorf("got %v, want %v", format.Regions, wantRegions)
}
}
func TestFormatHandlerLocaleHeader(t *testing.T) {
req, err := http.NewRequest("GET", "/address-formats", nil)
req.Header.Add("Accept-Language", "zh-hant, zh, en;q=0.8")
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler := address.FormatHandler{}
handler.ServeHTTP(rr, req)
if status := rr.Code; status != http.StatusOK {
t.Errorf("got HTTP %v want HTTP %v", status, http.StatusOK)
}
if contentType := rr.Header().Get("Content-Type"); contentType != "application/json" {
t.Errorf("got %v want %v", contentType, "application/json")
}
if contentLanguage := rr.Header().Get("Content-Language"); contentLanguage != "zh-Hant" {
t.Errorf("got %v want %v", contentLanguage, "zh-Hant")
}
var data map[string]testFormat
err = json.Unmarshal(rr.Body.Bytes(), &data)
if err != nil {
t.Fatal(err)
}
format, ok := data["TW"]
wantFormat := address.GetFormat("TW")
wantRegions := make(map[string]string)
for _, regionID := range wantFormat.LocalRegions.Keys() {
wantRegions[regionID], _ = wantFormat.LocalRegions.Get(regionID)
}
if !ok {
t.Errorf(`address format "TW" not found.`)
}
// Confirm that the correct layout and regions were selected.
if format.Layout != wantFormat.LocalLayout {
t.Errorf("got %q, want %q", format.Layout, wantFormat.LocalLayout)
}
if !reflect.DeepEqual(format.Regions, wantRegions) {
t.Errorf("got %v, want %v", format.Regions, wantRegions)
}
}