This repository has been archived by the owner on Jun 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 64
/
locale_test.go
169 lines (127 loc) · 3.72 KB
/
locale_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
package contentful
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
func TestLocalesServiceList(t *testing.T) {
var err error
assert := assert.New(t)
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(r.Method, "GET")
assert.Equal(r.URL.Path, "/spaces/"+spaceID+"/locales")
checkHeaders(r, assert)
w.WriteHeader(200)
fmt.Fprintln(w, readTestData("locales.json"))
})
// test server
server := httptest.NewServer(handler)
defer server.Close()
// cma client
cma = NewCMA(CMAToken)
cma.BaseURL = server.URL
_, err = cma.Locales.List(spaceID).Next()
assert.Nil(err)
}
func TestLocalesServiceGet(t *testing.T) {
var err error
assert := assert.New(t)
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(r.Method, "GET")
assert.Equal(r.URL.Path, "/spaces/"+spaceID+"/locales/4aGeQYgByqQFJtToAOh2JJ")
checkHeaders(r, assert)
w.WriteHeader(200)
fmt.Fprintln(w, readTestData("locale_1.json"))
})
// test server
server := httptest.NewServer(handler)
defer server.Close()
// cma client
cma = NewCMA(CMAToken)
cma.BaseURL = server.URL
locale, err := cma.Locales.Get(spaceID, "4aGeQYgByqQFJtToAOh2JJ")
assert.Nil(err)
assert.Equal("U.S. English", locale.Name)
assert.Equal("en-US", locale.Code)
}
func TestLocalesServiceUpsertCreate(t *testing.T) {
var err error
assert := assert.New(t)
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(r.Method, "POST")
assert.Equal(r.RequestURI, "/spaces/"+spaceID+"/locales")
checkHeaders(r, assert)
var payload map[string]interface{}
err := json.NewDecoder(r.Body).Decode(&payload)
assert.Nil(err)
assert.Equal("German (Austria)", payload["name"])
assert.Equal("de-AT", payload["code"])
w.WriteHeader(200)
fmt.Fprintln(w, readTestData("locale_1.json"))
})
// test server
server := httptest.NewServer(handler)
defer server.Close()
// cma client
cma = NewCMA(CMAToken)
cma.BaseURL = server.URL
locale := &Locale{
Name: "German (Austria)",
Code: "de-AT",
}
err = cma.Locales.Upsert(spaceID, locale)
assert.Nil(err)
}
func TestLocalesServiceUpsertUpdate(t *testing.T) {
var err error
assert := assert.New(t)
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(r.Method, "PUT")
assert.Equal(r.RequestURI, "/spaces/"+spaceID+"/locales/4aGeQYgByqQFJtToAOh2JJ")
checkHeaders(r, assert)
var payload map[string]interface{}
err := json.NewDecoder(r.Body).Decode(&payload)
assert.Nil(err)
assert.Equal("modified-name", payload["name"])
assert.Equal("modified-code", payload["code"])
w.WriteHeader(200)
fmt.Fprintln(w, string(readTestData("locale_1.json")))
})
// test server
server := httptest.NewServer(handler)
defer server.Close()
// cma client
cma = NewCMA(CMAToken)
cma.BaseURL = server.URL
locale, err := localeFromTestData("locale_1.json")
assert.Nil(err)
locale.Name = "modified-name"
locale.Code = "modified-code"
err = cma.Locales.Upsert(spaceID, locale)
assert.Nil(err)
}
func TestLocalesServiceDelete(t *testing.T) {
var err error
assert := assert.New(t)
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(r.Method, "DELETE")
assert.Equal(r.RequestURI, "/spaces/"+spaceID+"/locales/4aGeQYgByqQFJtToAOh2JJ")
checkHeaders(r, assert)
w.WriteHeader(200)
})
// test server
server := httptest.NewServer(handler)
defer server.Close()
// cma client
cma = NewCMA(CMAToken)
cma.BaseURL = server.URL
// test locale
locale, err := localeFromTestData("locale_1.json")
assert.Nil(err)
// delete locale
err = cma.Locales.Delete(spaceID, locale)
assert.Nil(err)
}