2024-07-01 20:34:16 +00:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
2024-07-13 14:35:17 +00:00
|
|
|
"context"
|
|
|
|
"go-udemy-web-1/internal/models"
|
|
|
|
"log"
|
2024-07-01 20:34:16 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
type postData struct {
|
|
|
|
key string
|
|
|
|
value string
|
|
|
|
}
|
|
|
|
|
|
|
|
var theTests = []struct {
|
|
|
|
name string
|
|
|
|
url string
|
|
|
|
method string
|
|
|
|
expectedStatusCode int
|
|
|
|
}{
|
2024-07-13 14:47:23 +00:00
|
|
|
{"home", "/", "GET", http.StatusOK},
|
|
|
|
{"about", "/about", "GET", http.StatusOK},
|
|
|
|
{"gq", "/generals-quarters", "GET", http.StatusOK},
|
|
|
|
{"ms", "/majors-suite", "GET", http.StatusOK},
|
|
|
|
{"sa", "/availability", "GET", http.StatusOK},
|
|
|
|
{"contact", "/contact", "GET", http.StatusOK},
|
|
|
|
|
2024-07-13 14:35:17 +00:00
|
|
|
// {"post-search-avail", "/availability", "POST", []postData{
|
|
|
|
// {key: "start", value: "2020-01-01"},
|
|
|
|
// {key: "end", value: "2020-01-02"},
|
|
|
|
// }, http.StatusOK},
|
|
|
|
// {"post-search-avail-json", "/availability-json", "POST", []postData{
|
|
|
|
// {key: "start", value: "2020-01-01"},
|
|
|
|
// {key: "end", value: "2020-01-02"},
|
|
|
|
// }, http.StatusOK},
|
|
|
|
// {"make-reservation", "/make-reservation", "POST", []postData{
|
|
|
|
// {key: "first_name", value: "John"},
|
|
|
|
// {key: "last_name", value: "Smith"},
|
|
|
|
// {key: "email", value: "me@here.com"},
|
|
|
|
// {key: "phone", value: "12345"},
|
|
|
|
// }, http.StatusOK},
|
2024-07-01 20:34:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestHandlers(t *testing.T) {
|
|
|
|
routes := getRoutes()
|
|
|
|
ts := httptest.NewTLSServer(routes)
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
for _, e := range theTests {
|
2024-07-13 14:47:23 +00:00
|
|
|
resp, err := ts.Client().Get(ts.URL + e.url)
|
|
|
|
if err != nil {
|
|
|
|
t.Log(err)
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if resp.StatusCode != e.expectedStatusCode {
|
|
|
|
t.Errorf("for %s, expected %d but got %d", e.name, e.expectedStatusCode, resp.StatusCode)
|
2024-07-01 20:34:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-07-13 14:35:17 +00:00
|
|
|
|
|
|
|
func TestRepository_Reservation(t *testing.T) {
|
|
|
|
reservation := models.Reservation{
|
|
|
|
RoomID: 1,
|
|
|
|
Room: models.Room{
|
|
|
|
ID: 1,
|
|
|
|
RoomName: "General's Quarters",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
req, _ := http.NewRequest("GET", "/make-reservation", nil)
|
|
|
|
ctx := getCtx(req)
|
|
|
|
|
|
|
|
req = req.WithContext(ctx)
|
|
|
|
|
|
|
|
rr := httptest.NewRecorder()
|
|
|
|
|
|
|
|
session.Put(ctx, "reservation", reservation)
|
|
|
|
|
|
|
|
handler := http.HandlerFunc(Repo.MakeReservation)
|
|
|
|
|
|
|
|
handler.ServeHTTP(rr, req)
|
|
|
|
|
|
|
|
if rr.Code != http.StatusOK {
|
|
|
|
t.Errorf("Reservation handler returned response code: got %d, wanted %d",
|
|
|
|
rr.Code, http.StatusOK)
|
|
|
|
}
|
2024-07-13 14:47:23 +00:00
|
|
|
|
|
|
|
// test case where reservation is not in session (reset everything)
|
|
|
|
req, _ = http.NewRequest("GET", "/make-reservation", nil)
|
|
|
|
ctx = getCtx(req)
|
|
|
|
req = req.WithContext(ctx)
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
|
|
|
|
handler.ServeHTTP(rr, req)
|
|
|
|
if rr.Code != http.StatusTemporaryRedirect {
|
|
|
|
t.Errorf("Reservation handler returned response code: got %d, wanted %d",
|
|
|
|
rr.Code, http.StatusTemporaryRedirect)
|
|
|
|
}
|
|
|
|
|
|
|
|
// test with non-existant room
|
|
|
|
req, _ = http.NewRequest("GET", "/make-reservation", nil)
|
|
|
|
ctx = getCtx(req)
|
|
|
|
req = req.WithContext(ctx)
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
reservation.RoomID = 100
|
|
|
|
session.Put(ctx, "reservation", reservation)
|
|
|
|
|
|
|
|
handler.ServeHTTP(rr, req)
|
|
|
|
if rr.Code != http.StatusTemporaryRedirect {
|
|
|
|
t.Errorf("Reservation handler returned response code: got %d, wanted %d",
|
|
|
|
rr.Code, http.StatusTemporaryRedirect)
|
|
|
|
}
|
2024-07-13 14:35:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getCtx(req *http.Request) context.Context {
|
|
|
|
ctx, err := session.Load(req.Context(), req.Header.Get("X-Session"))
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ctx
|
|
|
|
}
|