@@ -2,10 +2,11 @@ package handlers
2
2
3
3
import (
4
4
"context"
5
+ "fmt"
5
6
"log"
6
7
"net/http"
7
8
"net/http/httptest"
8
- "net/url "
9
+ "strings "
9
10
"testing"
10
11
11
12
"github.com/jagottsicher/myGoWebApplication/internal/models"
@@ -20,32 +21,16 @@ var allTheHandlerTests = []struct {
20
21
name string
21
22
url string
22
23
method string
23
- params []postData
24
24
expectedStatusCode int
25
25
}{
26
- // {"home", "/", "GET", []postData{}, http.StatusOK},
27
- // {"about", "/about", "GET", []postData{}, http.StatusOK},
28
- // {"eremite", "/eremite", "GET", []postData{}, http.StatusOK},
29
- // {"couple", "/couple", "GET", []postData{}, http.StatusOK},
30
- // {"family", "/family", "GET", []postData{}, http.StatusOK},
31
- // {"reservation", "/reservation", "GET", []postData{}, http.StatusOK},
32
- // {"contact", "/contact", "GET", []postData{}, http.StatusOK},
33
- // {"make-reservation", "/make-reservation", "GET", []postData{}, http.StatusOK},
34
- // // {"reservation-overview", "/reservation-overview", "GET", []postData{}, http.StatusOK},
35
- // {"not-existing-route", "/not-existing-dummy", "GET", []postData{}, http.StatusNotFound},
36
- // {"post-reservation", "/reservation", "POST", []postData{
37
- // {key: "startingEnd", value: "2023-02-23"},
38
- // {key: "endingEnd", value: "2023-02-25"},
39
- // }, http.StatusOK},
40
- // {"post-reservation-json", "/reservation-json", "POST", []postData{
41
- // {key: "startingEnd", value: "2023-02-23"},
42
- // {key: "endingEnd", value: "2023-02-25"},
43
- // }, http.StatusOK},
44
- // {"post-make-reservation", "/make-reservation", "POST", []postData{
45
- // {key: "full_name", value: "Ricky Spanish"},
46
- // {key: "email", value: "[email protected] "},
47
- // {key: "phone", value: "555-123-4567"},
48
- // }, http.StatusOK},
26
+ {"home" , "/" , "GET" , http .StatusOK },
27
+ {"about" , "/about" , "GET" , http .StatusOK },
28
+ {"eremite" , "/eremite" , "GET" , http .StatusOK },
29
+ {"couple" , "/couple" , "GET" , http .StatusOK },
30
+ {"family" , "/family" , "GET" , http .StatusOK },
31
+ {"reservation" , "/reservation" , "GET" , http .StatusOK },
32
+ {"contact" , "/contact" , "GET" , http .StatusOK },
33
+ {"not-existing-route" , "/not-existing-dummy" , "GET" , http .StatusNotFound },
49
34
}
50
35
51
36
func TestAllTheHandlers (t * testing.T ) {
@@ -66,22 +51,6 @@ func TestAllTheHandlers(t *testing.T) {
66
51
if response .StatusCode != test .expectedStatusCode {
67
52
t .Errorf ("%s: expected %d, got %d" , test .name , test .expectedStatusCode , response .StatusCode )
68
53
}
69
- } else {
70
- values := url.Values {}
71
-
72
- for _ , param := range test .params {
73
- values .Add (param .key , param .value )
74
- }
75
-
76
- response , err := testServer .Client ().PostForm (testServer .URL + test .url , values )
77
- if err != nil {
78
- t .Log (err )
79
- t .Fatal (err )
80
- }
81
-
82
- if response .StatusCode != test .expectedStatusCode {
83
- t .Errorf ("%s: expected %d, got %d" , test .name , test .expectedStatusCode , response .StatusCode )
84
- }
85
54
}
86
55
}
87
56
}
@@ -117,6 +86,220 @@ func TestRepository_MakeReservation(t *testing.T) {
117
86
if rr .Code != http .StatusOK {
118
87
t .Errorf ("handler MakeReservation failed: unexpected response code: got %d, wanted %d" , rr .Code , http .StatusOK )
119
88
}
89
+
90
+ // test case without a reservation in session (reset everything)
91
+ req , _ = http .NewRequest ("GET" , "/make-reservation" , nil )
92
+ ctx = getCtx (req )
93
+ req = req .WithContext (ctx )
94
+ rr = httptest .NewRecorder ()
95
+ handler .ServeHTTP (rr , req )
96
+
97
+ if rr .Code != http .StatusTemporaryRedirect {
98
+ t .Errorf ("handler MakeReservation failed: unexpected response code: got %d, wanted %d" , rr .Code , http .StatusTemporaryRedirect )
99
+ }
100
+
101
+ // test error returned from database query function
102
+ req , _ = http .NewRequest ("GET" , "/make-reservation" , nil )
103
+ ctx = getCtx (req )
104
+ req = req .WithContext (ctx )
105
+ rr = httptest .NewRecorder ()
106
+ reservation .BungalowID = 99
107
+ session .Put (ctx , "reservation" , reservation )
108
+
109
+ handler .ServeHTTP (rr , req )
110
+ if rr .Code != http .StatusTemporaryRedirect {
111
+ t .Errorf ("handler MakeReservation failed: unexpected response code: got %d, wanted %d" , rr .Code , http .StatusTemporaryRedirect )
112
+ }
113
+
114
+ }
115
+
116
+ // TestRepository_PostMakeReservation tests the MakeReservation get-request handler
117
+ func TestRepository_PostMakeReservation (t * testing.T ) {
118
+
119
+ reqBody := "start_date=2037-01-01"
120
+ reqBody = fmt .Sprintf ("%s&%s" , reqBody , "end_date=2037-01-02" )
121
+ reqBody = fmt .Sprintf ("%s&%s" , reqBody , "full_name=Peter Griffin" )
122
+ reqBody = fmt .
Sprintf (
"%s&%s" ,
reqBody ,
"[email protected] " )
123
+ reqBody = fmt .Sprintf ("%s&%s" , reqBody , "phone=1234567890" )
124
+ reqBody = fmt .Sprintf ("%s&%s" , reqBody , "bungalow_id=1" )
125
+
126
+ req , _ := http .NewRequest ("POST" , "/make-reservation" , strings .NewReader (reqBody ))
127
+ ctx := getCtx (req )
128
+ req = req .WithContext (ctx )
129
+
130
+ req .Header .Set ("Content-Type" , "application/x-www-form-urlencoded" )
131
+
132
+ rr := httptest .NewRecorder ()
133
+
134
+ handler := http .HandlerFunc (Repo .PostMakeReservation )
135
+
136
+ handler .ServeHTTP (rr , req )
137
+
138
+ if rr .Code != http .StatusSeeOther {
139
+ t .Errorf ("PostMakeReservation handler returned wrong response code: got %d, wanted %d" , rr .Code , http .StatusSeeOther )
140
+ }
141
+
142
+ // missing post body
143
+ req , _ = http .NewRequest ("POST" , "/make-reservation" , nil )
144
+ ctx = getCtx (req )
145
+ req = req .WithContext (ctx )
146
+
147
+ req .Header .Set ("Content-Type" , "application/x-www-form-urlencoded" )
148
+
149
+ rr = httptest .NewRecorder ()
150
+
151
+ handler = http .HandlerFunc (Repo .PostMakeReservation )
152
+
153
+ handler .ServeHTTP (rr , req )
154
+
155
+ if rr .Code != http .StatusTemporaryRedirect {
156
+ t .Errorf ("PostMakeReservation handler returned wrong response code: got %d, wanted %d" , rr .Code , http .StatusTemporaryRedirect )
157
+ }
158
+
159
+ // invalid start date
160
+ reqBody = "start_date=invalid"
161
+ reqBody = fmt .Sprintf ("%s&%s" , reqBody , "end_date=2037-01-02" )
162
+ reqBody = fmt .Sprintf ("%s&%s" , reqBody , "full_name=Peter Griffin" )
163
+ reqBody = fmt .
Sprintf (
"%s&%s" ,
reqBody ,
"[email protected] " )
164
+ reqBody = fmt .Sprintf ("%s&%s" , reqBody , "phone=1234567890" )
165
+ reqBody = fmt .Sprintf ("%s&%s" , reqBody , "bungalow_id=1" )
166
+
167
+ req , _ = http .NewRequest ("POST" , "/make-reservation" , strings .NewReader (reqBody ))
168
+ ctx = getCtx (req )
169
+ req = req .WithContext (ctx )
170
+
171
+ req .Header .Set ("Content-Type" , "application/x-www-form-urlencoded" )
172
+
173
+ rr = httptest .NewRecorder ()
174
+
175
+ handler = http .HandlerFunc (Repo .PostMakeReservation )
176
+
177
+ handler .ServeHTTP (rr , req )
178
+
179
+ if rr .Code != http .StatusTemporaryRedirect {
180
+ t .Errorf ("PostMakeReservation handler returned wrong response code for invalid start date: got %d, wanted %d" , rr .Code , http .StatusTemporaryRedirect )
181
+ }
182
+
183
+ // invalid end date
184
+ reqBody = "start_date=2037-01-01"
185
+ reqBody = fmt .Sprintf ("%s&%s" , reqBody , "end_date=invalid" )
186
+ reqBody = fmt .Sprintf ("%s&%s" , reqBody , "full_name=Peter Griffin" )
187
+ reqBody = fmt .
Sprintf (
"%s&%s" ,
reqBody ,
"[email protected] " )
188
+ reqBody = fmt .Sprintf ("%s&%s" , reqBody , "phone=1234567890" )
189
+ reqBody = fmt .Sprintf ("%s&%s" , reqBody , "bungalow_id=1" )
190
+
191
+ req , _ = http .NewRequest ("POST" , "/make-reservation" , strings .NewReader (reqBody ))
192
+ ctx = getCtx (req )
193
+ req = req .WithContext (ctx )
194
+
195
+ req .Header .Set ("Content-Type" , "application/x-www-form-urlencoded" )
196
+
197
+ rr = httptest .NewRecorder ()
198
+
199
+ handler = http .HandlerFunc (Repo .PostMakeReservation )
200
+
201
+ handler .ServeHTTP (rr , req )
202
+
203
+ if rr .Code != http .StatusTemporaryRedirect {
204
+ t .Errorf ("PostMakeReservation handler returned wrong response code for invalid end date: got %d, wanted %d" , rr .Code , http .StatusTemporaryRedirect )
205
+ }
206
+
207
+ // invalid bungalow id
208
+ reqBody = "start_date=2037-01-01"
209
+ reqBody = fmt .Sprintf ("%s&%s" , reqBody , "end_date=2037-01-02" )
210
+ reqBody = fmt .Sprintf ("%s&%s" , reqBody , "full_name=Peter Griffin" )
211
+ reqBody = fmt .
Sprintf (
"%s&%s" ,
reqBody ,
"[email protected] " )
212
+ reqBody = fmt .Sprintf ("%s&%s" , reqBody , "phone=1234567890" )
213
+ reqBody = fmt .Sprintf ("%s&%s" , reqBody , "bungalow_id=invalid" )
214
+
215
+ req , _ = http .NewRequest ("POST" , "/make-reservation" , strings .NewReader (reqBody ))
216
+ ctx = getCtx (req )
217
+ req = req .WithContext (ctx )
218
+
219
+ req .Header .Set ("Content-Type" , "application/x-www-form-urlencoded" )
220
+
221
+ rr = httptest .NewRecorder ()
222
+
223
+ handler = http .HandlerFunc (Repo .PostMakeReservation )
224
+
225
+ handler .ServeHTTP (rr , req )
226
+
227
+ if rr .Code != http .StatusTemporaryRedirect {
228
+ t .Errorf ("PostMakeReservation handler returned wrong response code for invalid bungalow id: got %d, wanted %d" , rr .Code , http .StatusTemporaryRedirect )
229
+ }
230
+
231
+ // invalid/insufficient data
232
+ reqBody = "start_date=2037-01-01"
233
+ reqBody = fmt .Sprintf ("%s&%s" , reqBody , "end_date=2037-01-02" )
234
+ reqBody = fmt .Sprintf ("%s&%s" , reqBody , "full_name=P" )
235
+ reqBody = fmt .
Sprintf (
"%s&%s" ,
reqBody ,
"[email protected] " )
236
+ reqBody = fmt .Sprintf ("%s&%s" , reqBody , "phone=1234567890" )
237
+ reqBody = fmt .Sprintf ("%s&%s" , reqBody , "bungalow_id=1" )
238
+
239
+ req , _ = http .NewRequest ("POST" , "/make-reservation" , strings .NewReader (reqBody ))
240
+ ctx = getCtx (req )
241
+ req = req .WithContext (ctx )
242
+
243
+ req .Header .Set ("Content-Type" , "application/x-www-form-urlencoded" )
244
+
245
+ rr = httptest .NewRecorder ()
246
+
247
+ handler = http .HandlerFunc (Repo .PostMakeReservation )
248
+
249
+ handler .ServeHTTP (rr , req )
250
+
251
+ if rr .Code != http .StatusSeeOther {
252
+ t .Errorf ("PostMakeReservation handler returned wrong response code for invalid bungalow id: got %d, wanted %d" , rr .Code , http .StatusSeeOther )
253
+ }
254
+
255
+ // failure inserting reservation into database
256
+ reqBody = "start_date=2037-01-01"
257
+ reqBody = fmt .Sprintf ("%s&%s" , reqBody , "end_date=2037-01-02" )
258
+ reqBody = fmt .Sprintf ("%s&%s" , reqBody , "full_name=Peter Griffin" )
259
+ reqBody = fmt .
Sprintf (
"%s&%s" ,
reqBody ,
"[email protected] " )
260
+ reqBody = fmt .Sprintf ("%s&%s" , reqBody , "phone=1234567890" )
261
+ reqBody = fmt .Sprintf ("%s&%s" , reqBody , "bungalow_id=99" )
262
+
263
+ req , _ = http .NewRequest ("POST" , "/make-reservation" , strings .NewReader (reqBody ))
264
+ ctx = getCtx (req )
265
+ req = req .WithContext (ctx )
266
+
267
+ req .Header .Set ("Content-Type" , "application/x-www-form-urlencoded" )
268
+
269
+ rr = httptest .NewRecorder ()
270
+
271
+ handler = http .HandlerFunc (Repo .PostMakeReservation )
272
+
273
+ handler .ServeHTTP (rr , req )
274
+
275
+ if rr .Code != http .StatusTemporaryRedirect {
276
+ t .Errorf ("PostMakeReservation handler failed when trying to inserting a reservation into the database: got %d, wanted %d" , rr .Code , http .StatusTemporaryRedirect )
277
+ }
278
+
279
+ // failure to inserting restriction into database
280
+ reqBody = "start_date=2037-01-01"
281
+ reqBody = fmt .Sprintf ("%s&%s" , reqBody , "end_date=2037-01-02" )
282
+ reqBody = fmt .Sprintf ("%s&%s" , reqBody , "full_name=Peter Griffin" )
283
+ reqBody = fmt .
Sprintf (
"%s&%s" ,
reqBody ,
"[email protected] " )
284
+ reqBody = fmt .Sprintf ("%s&%s" , reqBody , "phone=1234567890" )
285
+ reqBody = fmt .Sprintf ("%s&%s" , reqBody , "bungalow_id=999" )
286
+
287
+ req , _ = http .NewRequest ("POST" , "/make-reservation" , strings .NewReader (reqBody ))
288
+ ctx = getCtx (req )
289
+ req = req .WithContext (ctx )
290
+
291
+ req .Header .Set ("Content-Type" , "application/x-www-form-urlencoded" )
292
+
293
+ rr = httptest .NewRecorder ()
294
+
295
+ handler = http .HandlerFunc (Repo .PostMakeReservation )
296
+
297
+ handler .ServeHTTP (rr , req )
298
+
299
+ if rr .Code != http .StatusTemporaryRedirect {
300
+ t .Errorf ("PostMakeReservation handler failed when trying to inserting a reservation into the database: got %d, wanted %d" , rr .Code , http .StatusTemporaryRedirect )
301
+ }
302
+
120
303
}
121
304
122
305
func getCtx (req * http.Request ) context.Context {
0 commit comments