From 413bfc1685b5ad38a509e28e0e63b7f915019517 Mon Sep 17 00:00:00 2001 From: vinchent Date: Sun, 14 Jul 2024 11:59:43 +0200 Subject: [PATCH] user postData instead --- internal/handlers/handlers_test.go | 126 +++++++++++++++-------------- 1 file changed, 64 insertions(+), 62 deletions(-) diff --git a/internal/handlers/handlers_test.go b/internal/handlers/handlers_test.go index c0680e1..da666cb 100644 --- a/internal/handlers/handlers_test.go +++ b/internal/handlers/handlers_test.go @@ -58,7 +58,7 @@ func TestHandlers(t *testing.T) { t.Fatal(err) } if resp.StatusCode != e.expectedStatusCode { - t.Errorf("for %s, expected %d but got %d", e.name, e.expectedStatusCode, resp.StatusCode) + t.Errorf("for %s, expected %d but got %d\n", e.name, e.expectedStatusCode, resp.StatusCode) } } } @@ -98,7 +98,7 @@ func TestRepository_MakeReservation(t *testing.T) { handler.ServeHTTP(rr, req) if rr.Code != test.expectedStatusCode { - t.Errorf("for %s, reservation handler returned response code: got %d, wanted %d", + t.Errorf("for %s, reservation handler returned response code: got %d, wanted %d\n", test.name, rr.Code, http.StatusOK) } } @@ -109,103 +109,103 @@ func TestRepository_MakeReservation(t *testing.T) { var postMakeReservationTests = []struct { name string - reservationInfo []string + reservationInfo []postData roomID int expectedStatusCode int }{ { "ok", - []string{ - "first_name=John", - "last_name=Smith", - "email=john@smith.com", - "phone=1234", - "room_id=1", - "start_date=2050-01-01", - "end_date=2050-01-02", + []postData{ + {key: "first_name", value: "John"}, + {key: "last_name", value: "Smith"}, + {key: "email", value: "john@smith.com"}, + {key: "phone", value: "1234"}, + {key: "room_id", value: "1"}, + {key: "start_date", value: "2050-01-01"}, + {key: "end_date", value: "2050-01-02"}, }, 1, http.StatusSeeOther, }, { "no_session", - []string{ - "first_name=John", - "last_name=Smith", - "email=john@smith.com", - "phone=1234", - "room_id=0", - "start_date=2050-01-01", - "end_date=2050-01-02", + []postData{ + {key: "first_name", value: "John"}, + {key: "last_name", value: "Smith"}, + {key: "email", value: "john@smith.com"}, + {key: "phone", value: "1234"}, + {key: "room_id", value: "0"}, + {key: "start_date", value: "2050-01-01"}, + {key: "end_date", value: "2050-01-02"}, }, 0, http.StatusTemporaryRedirect, }, { "no_post_data", - []string{}, + []postData{}, 0, http.StatusTemporaryRedirect, }, { "missing first name", - []string{ - "last_name=Smith", - "email=john@smith.com", - "phone=1234", - "room_id=1", - "start_date=2050-01-01", - "end_date=2050-01-02", + []postData{ + {key: "last_name", value: "Smith"}, + {key: "email", value: "john@smith.com"}, + {key: "phone", value: "1234"}, + {key: "room_id", value: "0"}, + {key: "start_date", value: "2050-01-01"}, + {key: "end_date", value: "2050-01-02"}, }, 1, http.StatusOK, }, { "wrong first name", - []string{ - "first_name=J", - "last_name=Smith", - "email=john@smith.com", - "phone=1234", - "room_id=1", - "start_date=2050-01-01", - "end_date=2050-01-02", + []postData{ + {key: "first_name", value: "J"}, + {key: "last_name", value: "Smith"}, + {key: "email", value: "john@smith.com"}, + {key: "phone", value: "1234"}, + {key: "room_id", value: "0"}, + {key: "start_date", value: "2050-01-01"}, + {key: "end_date", value: "2050-01-02"}, }, 1, http.StatusOK, }, { "wrong email", - []string{ - "first_name=John", - "last_name=Smith", - "email=john@smith", - "phone=1234", - "room_id=1", - "start_date=2050-01-01", - "end_date=2050-01-02", + []postData{ + {key: "first_name", value: "John"}, + {key: "last_name", value: "Smith"}, + {key: "email", value: "john@smith"}, + {key: "phone", value: "1234"}, + {key: "room_id", value: "0"}, + {key: "start_date", value: "2050-01-01"}, + {key: "end_date", value: "2050-01-02"}, }, 1, http.StatusOK, }, { "insert reservation error", - []string{ - "first_name=John", - "last_name=Smith", - "email=john@smith.com", - "phone=1234", - "room_id=2", - "start_date=2050-01-01", - "end_date=2050-01-02", + []postData{ + {key: "first_name", value: "John"}, + {key: "last_name", value: "Smith"}, + {key: "email", value: "john@smith.com"}, + {key: "phone", value: "1234"}, + {key: "room_id", value: "2"}, + {key: "start_date", value: "2050-01-01"}, + {key: "end_date", value: "2050-01-02"}, }, 2, http.StatusTemporaryRedirect, }, { "insert room restriction error", - []string{ - "first_name=John", - "last_name=Smith", - "email=john@smith.com", - "phone=1234", - "room_id=100", - "start_date=2050-01-01", - "end_date=2050-01-02", + []postData{ + {key: "first_name", value: "John"}, + {key: "last_name", value: "Smith"}, + {key: "email", value: "john@smith.com"}, + {key: "phone", value: "1234"}, + {key: "room_id", value: "100"}, + {key: "start_date", value: "2050-01-01"}, + {key: "end_date", value: "2050-01-02"}, }, 100, http.StatusTemporaryRedirect, }, @@ -215,9 +215,11 @@ func TestRepository_PostMakeReservation(t *testing.T) { for _, test := range postMakeReservationTests { var reqBody string if len(test.reservationInfo) > 0 { - reqBody = test.reservationInfo[0] + reqBody = fmt.Sprintf("%s=%s", test.reservationInfo[0].key, + test.reservationInfo[0].value) for _, element := range test.reservationInfo[1:] { - reqBody = fmt.Sprintf("%s&%s", reqBody, element) + reqBody = fmt.Sprintf("%s&%s=%s", reqBody, element.key, + element.value) } } layout := "2006-01-02" @@ -245,7 +247,7 @@ func TestRepository_PostMakeReservation(t *testing.T) { handler.ServeHTTP(rr, req) if rr.Code != test.expectedStatusCode { - fmt.Printf("for %s, reservation handler returned response code: got %d, wanted %d", + fmt.Printf("for %s, reservation handler returned response code: got %d, wanted %d\n", test.name, rr.Code, test.expectedStatusCode) } }