forked from aeneasr/workshop-dbg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
189 lines (156 loc) · 5.24 KB
/
main_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
package main
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/gorilla/mux"
. "github.com/ory-am/workshop-dbg/store"
"github.com/ory-am/workshop-dbg/store/memory"
"github.com/parnurzeal/gorequest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var mockedContactList = Contacts{
"john-bravo": &Contact{
ID: "john-bravo",
Name: "John Bravo",
Department: "IT",
Company: "ACME Inc",
},
"cathrine-mueller": &Contact{
ID: "cathrine-mueller",
Name: "Cathrine Müller",
Department: "HR",
Company: "Grove AG",
},
}
var mockContact = &Contact{
ID: "eddie-markson",
Name: "Eddie Markson",
Department: "Finance",
Company: "ACME Inc",
}
func TestListContacts(t *testing.T) {
store := &memory.InMemoryStore{Contacts: mockedContactList}
// Initialize everything (very similar to main() function).
router := mux.NewRouter()
router.HandleFunc("/contacts", ListContacts(store)).Methods("GET")
ts := httptest.NewServer(router)
// This helper function makes an http request to ListContacts and validates its output.
fetchAndTestContactList(t, ts, mockedContactList)
}
func TestAddContacts(t *testing.T) {
// We create a copy of the store
contactListForThisTest := copyContacts(mockedContactList)
store := &memory.InMemoryStore{Contacts: contactListForThisTest}
// Initialize the HTTP routes, similar to main()
router := mux.NewRouter()
router.HandleFunc("/contacts", AddContact(store)).Methods("POST")
ts := httptest.NewServer(router)
// Make the request
resp, _, errs := gorequest.New().Post(ts.URL + "/contacts").SendStruct(mockContact).End()
require.Len(t, errs, 0)
require.Equal(t, http.StatusOK, resp.StatusCode)
require.Equal(t, contactListForThisTest[mockContact.ID], mockContact)
}
func TestDeleteContacts(t *testing.T) {
// We create a copy of the store
contactListForThisTest := copyContacts(mockedContactList)
store := &memory.InMemoryStore{Contacts: contactListForThisTest}
// Initialize the HTTP routes, similar to main()
router := mux.NewRouter()
router.HandleFunc("/contacts/{id}", DeleteContact(store)).Methods("DELETE")
ts := httptest.NewServer(router)
// Make the request
resp, _, errs := gorequest.New().Delete(ts.URL + "/contacts/john-bravo").End()
require.Len(t, errs, 0)
require.Equal(t, http.StatusNoContent, resp.StatusCode)
_, found := contactListForThisTest["john-bravo"]
require.False(t, found)
}
func TestUpdateContacts(t *testing.T) {
// We create a copy of the store
contactListForThisTest := copyContacts(mockedContactList)
store := &memory.InMemoryStore{Contacts: contactListForThisTest}
// Initialize the HTTP routes, similar to main()
router := mux.NewRouter()
router.HandleFunc("/contacts/{id}", UpdateContact(store)).Methods("PUT")
ts := httptest.NewServer(router)
// Make the request
resp, _, errs := gorequest.New().Put(ts.URL + "/contacts/john-bravo").SendStruct(mockContact).End()
require.Len(t, errs, 0)
require.Equal(t, http.StatusOK, resp.StatusCode)
// The new contact should be inserted
_, found := contactListForThisTest[mockContact.ID]
require.True(t, found)
}
func TestPis(t *testing.T) {
// Initialize the HTTP routes, similar to main()
router := mux.NewRouter()
router.HandleFunc("/pis", ComputePis).Methods("GET")
ts := httptest.NewServer(router)
// Make the request
resp, body, errs := gorequest.New().Get(ts.URL + "/pis?n=2").End()
require.Len(t, errs, 0)
require.Equal(t, http.StatusOK, resp.StatusCode)
res := struct {
Pi string `json:"pi"`
N int `json:"n"`
}{}
require.Nil(t, json.Unmarshal([]byte(body), &res))
assert.Equal(t, 2, res.N)
}
func TestPi(t *testing.T) {
// Initialize the HTTP routes, similar to main()
router := mux.NewRouter()
router.HandleFunc("/pi", ComputePi).Methods("GET")
ts := httptest.NewServer(router)
// Make the request
resp, body, errs := gorequest.New().Get(ts.URL + "/pi?n=100").End()
require.Len(t, errs, 0)
require.Equal(t, http.StatusOK, resp.StatusCode)
res := struct {
Pi string `json:"pi"`
N int `json:"n"`
}{}
require.Nil(t, json.Unmarshal([]byte(body), &res))
assert.Equal(t, 100, res.N)
}
func TestAllocate(t *testing.T) {
// Initialize the HTTP routes, similar to main()
router := mux.NewRouter()
router.HandleFunc("/allocate", Allocate).Methods("GET")
ts := httptest.NewServer(router)
// Make the request
resp, body, errs := gorequest.New().Get(ts.URL + "/allocate?n=100").End()
require.Len(t, errs, 0)
require.Equal(t, http.StatusOK, resp.StatusCode)
res := struct {
Result string `json:"result"`
N int `json:"n"`
}{}
require.Nil(t, json.Unmarshal([]byte(body), &res))
assert.Equal(t, 100, res.N)
assert.Equal(t, "Processed!", res.Result)
}
func fetchAndTestContactList(t *testing.T, ts *httptest.Server, compareWith Contacts) {
// Request ListContacts
resp, err := http.Get(ts.URL + "/contacts")
// Verify that no errors occurred
require.Nil(t, err)
// Unmarshal the output
var result Contacts
err = json.NewDecoder(resp.Body).Decode(&result)
// Make sure that no error occurred
require.Nil(t, err)
// Compare the outputs
assert.Equal(t, compareWith, result)
}
func copyContacts(original Contacts) Contacts {
result := Contacts{}
for k, v := range original {
result[k] = v
}
return result
}