Skip to content

Commit 2e439b9

Browse files
committed
added lecture 126 get request handlers test as well as one post request handler test
1 parent f38944e commit 2e439b9

File tree

3 files changed

+249
-54
lines changed

3 files changed

+249
-54
lines changed

internal/handlers/handlers.go

+13-13
Original file line numberDiff line numberDiff line change
@@ -215,14 +215,14 @@ func (m *Repository) MakeReservation(w http.ResponseWriter, r *http.Request) {
215215
// helpers.ServerError(w, errors.New("cannot get reservation back from session"))
216216
// return
217217
m.App.Session.Put(r.Context(), "error", "Cannot get reservation back from session")
218-
http.Redirect(w, r, "/", http.StatusSeeOther)
218+
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
219219
return
220220
}
221221

222222
bungalow, err := m.DB.GetBungalowByID(res.BungalowID)
223223
if err != nil {
224224
m.App.Session.Put(r.Context(), "error", "Cannot find bungalow!")
225-
http.Redirect(w, r, "/", http.StatusSeeOther)
225+
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
226226
return
227227
}
228228

@@ -251,8 +251,8 @@ func (m *Repository) MakeReservation(w http.ResponseWriter, r *http.Request) {
251251
func (m *Repository) PostMakeReservation(w http.ResponseWriter, r *http.Request) {
252252
err := r.ParseForm()
253253
if err != nil {
254-
m.App.Session.Put(r.Context(), "error", "can't get data from form")
255-
http.Redirect(w, r, "/", http.StatusSeeOther)
254+
m.App.Session.Put(r.Context(), "error", "can't parse form")
255+
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
256256
return
257257
}
258258

@@ -267,22 +267,22 @@ func (m *Repository) PostMakeReservation(w http.ResponseWriter, r *http.Request)
267267

268268
startDate, err := time.Parse(layout, sd)
269269
if err != nil {
270-
m.App.Session.Put(r.Context(), "error", "can't get data from form")
271-
http.Redirect(w, r, "/", http.StatusSeeOther)
270+
m.App.Session.Put(r.Context(), "error", "can't parse arrival date")
271+
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
272272
return
273273
}
274274

275275
endDate, err := time.Parse(layout, ed)
276276
if err != nil {
277-
m.App.Session.Put(r.Context(), "error", "can't get data from form")
278-
http.Redirect(w, r, "/", http.StatusSeeOther)
277+
m.App.Session.Put(r.Context(), "error", "can't parse departurte date")
278+
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
279279
return
280280
}
281281

282282
bungalowID, err := strconv.Atoi(r.Form.Get("bungalow_id"))
283283
if err != nil {
284-
m.App.Session.Put(r.Context(), "error", "can't get data from form")
285-
http.Redirect(w, r, "/", http.StatusSeeOther)
284+
m.App.Session.Put(r.Context(), "error", "can't parse bungalow id")
285+
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
286286
return
287287
}
288288

@@ -304,7 +304,7 @@ func (m *Repository) PostMakeReservation(w http.ResponseWriter, r *http.Request)
304304
if !form.Valid() {
305305
data := make(map[string]interface{})
306306
data["reservation"] = reservation
307-
307+
http.Error(w, "my own error message", http.StatusSeeOther)
308308
render.Template(w, r, "make-reservation-page.tpml", &models.TemplateData{
309309
Form: form,
310310
Data: data,
@@ -315,7 +315,7 @@ func (m *Repository) PostMakeReservation(w http.ResponseWriter, r *http.Request)
315315
newReservationID, err := m.DB.InsertReservation(reservation)
316316
if err != nil {
317317
m.App.Session.Put(r.Context(), "error", "can't write reservation to database")
318-
http.Redirect(w, r, "/", http.StatusSeeOther)
318+
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
319319
return
320320
}
321321

@@ -330,7 +330,7 @@ func (m *Repository) PostMakeReservation(w http.ResponseWriter, r *http.Request)
330330
err = m.DB.InsertBungalowRestriction(restriction)
331331
if err != nil {
332332
m.App.Session.Put(r.Context(), "error", "can't reserve bungalow in database")
333-
http.Redirect(w, r, "/", http.StatusSeeOther)
333+
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
334334
return
335335
}
336336

internal/handlers/handlers_test.go

+224-41
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ package handlers
22

33
import (
44
"context"
5+
"fmt"
56
"log"
67
"net/http"
78
"net/http/httptest"
8-
"net/url"
9+
"strings"
910
"testing"
1011

1112
"github.com/jagottsicher/myGoWebApplication/internal/models"
@@ -20,32 +21,16 @@ var allTheHandlerTests = []struct {
2021
name string
2122
url string
2223
method string
23-
params []postData
2424
expectedStatusCode int
2525
}{
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},
4934
}
5035

5136
func TestAllTheHandlers(t *testing.T) {
@@ -66,22 +51,6 @@ func TestAllTheHandlers(t *testing.T) {
6651
if response.StatusCode != test.expectedStatusCode {
6752
t.Errorf("%s: expected %d, got %d", test.name, test.expectedStatusCode, response.StatusCode)
6853
}
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-
}
8554
}
8655
}
8756
}
@@ -117,6 +86,220 @@ func TestRepository_MakeReservation(t *testing.T) {
11786
if rr.Code != http.StatusOK {
11887
t.Errorf("handler MakeReservation failed: unexpected response code: got %d, wanted %d", rr.Code, http.StatusOK)
11988
}
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+
120303
}
121304

122305
func getCtx(req *http.Request) context.Context {

0 commit comments

Comments
 (0)