Skip to content

Commit

Permalink
Simplify adding post parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
abneed committed Jul 16, 2022
1 parent 9b1885a commit bafbcb3
Showing 1 changed file with 117 additions and 100 deletions.
217 changes: 117 additions & 100 deletions internal/handlers/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package handlers
import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"net/http/httptest"
"net/url"
"reflect"
"strings"
"testing"
Expand Down Expand Up @@ -110,15 +110,16 @@ func TestRepository_Reservation(t *testing.T) {
}

func TestRepository_PostReservation(t *testing.T) {
reqBody := "start_date=2050-01-01"
reqBody = fmt.Sprintf("%s&%s", reqBody, "end_date=2050-01-02")
reqBody = fmt.Sprintf("%s&%s", reqBody, "first_name=Abneed")
reqBody = fmt.Sprintf("%s&%s", reqBody, "last_name=Rodriguez")
reqBody = fmt.Sprintf("%s&%s", reqBody, "[email protected]")
reqBody = fmt.Sprintf("%s&%s", reqBody, "phone=+5218671487159")
reqBody = fmt.Sprintf("%s&%s", reqBody, "room_id=1")

req, _ := http.NewRequest("POST", "/make-reservation", strings.NewReader(reqBody))
postedData := url.Values{}
postedData.Add("start_date", "2050-01-01")
postedData.Add("end_date", "2050-01-02")
postedData.Add("first_name", "Abneed")
postedData.Add("last_name", "Rodriguez")
postedData.Add("email", "[email protected]")
postedData.Add("phone", "55-555-5555")
postedData.Add("room_id", "1")

req, _ := http.NewRequest("POST", "/make-reservation", strings.NewReader(postedData.Encode()))
ctx := getCtx(req)
req = req.WithContext(ctx)

Expand Down Expand Up @@ -150,15 +151,16 @@ func TestRepository_PostReservation(t *testing.T) {
}

// test for invalid start date
reqBody = "start_date=invalid"
reqBody = fmt.Sprintf("%s&%s", reqBody, "end_date=2050-01-2")
reqBody = fmt.Sprintf("%s&%s", reqBody, "first_name=Abneed")
reqBody = fmt.Sprintf("%s&%s", reqBody, "last_name=Rodriguez")
reqBody = fmt.Sprintf("%s&%s", reqBody, "[email protected]")
reqBody = fmt.Sprintf("%s&%s", reqBody, "phone=+5218671487159")
reqBody = fmt.Sprintf("%s&%s", reqBody, "room_id=1")

req, _ = http.NewRequest("POST", "/make-reservation", strings.NewReader(reqBody))
postedData = url.Values{}
postedData.Add("start_date", "invalid")
postedData.Add("end_date", "2050-01-02")
postedData.Add("first_name", "Abneed")
postedData.Add("last_name", "Rodriguez")
postedData.Add("email", "[email protected]")
postedData.Add("phone", "55-555-5555")
postedData.Add("room_id", "1")

req, _ = http.NewRequest("POST", "/make-reservation", strings.NewReader(postedData.Encode()))
ctx = getCtx(req)
req = req.WithContext(ctx)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
Expand All @@ -173,15 +175,16 @@ func TestRepository_PostReservation(t *testing.T) {
}

// test for invalid end date
reqBody = "start_date=2050-01-01"
reqBody = fmt.Sprintf("%s&%s", reqBody, "end_date=invalid")
reqBody = fmt.Sprintf("%s&%s", reqBody, "first_name=Abneed")
reqBody = fmt.Sprintf("%s&%s", reqBody, "last_name=Rodriguez")
reqBody = fmt.Sprintf("%s&%s", reqBody, "[email protected]")
reqBody = fmt.Sprintf("%s&%s", reqBody, "phone=+5218671487159")
reqBody = fmt.Sprintf("%s&%s", reqBody, "room_id=1")

req, _ = http.NewRequest("POST", "/make-reservation", strings.NewReader(reqBody))
postedData = url.Values{}
postedData.Add("start_date", "2050-01-01")
postedData.Add("end_date", "invalid")
postedData.Add("first_name", "Abneed")
postedData.Add("last_name", "Rodriguez")
postedData.Add("email", "[email protected]")
postedData.Add("phone", "55-555-5555")
postedData.Add("room_id", "1")

req, _ = http.NewRequest("POST", "/make-reservation", strings.NewReader(postedData.Encode()))
ctx = getCtx(req)
req = req.WithContext(ctx)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
Expand All @@ -196,15 +199,16 @@ func TestRepository_PostReservation(t *testing.T) {
}

// test for invalid room id
reqBody = "start_date=2050-01-01"
reqBody = fmt.Sprintf("%s&%s", reqBody, "end_date=2050-01-02")
reqBody = fmt.Sprintf("%s&%s", reqBody, "first_name=Abneed")
reqBody = fmt.Sprintf("%s&%s", reqBody, "last_name=Rodriguez")
reqBody = fmt.Sprintf("%s&%s", reqBody, "[email protected]")
reqBody = fmt.Sprintf("%s&%s", reqBody, "phone=+5218671487159")
reqBody = fmt.Sprintf("%s&%s", reqBody, "room_id=invalid")

req, _ = http.NewRequest("POST", "/make-reservation", strings.NewReader(reqBody))
postedData = url.Values{}
postedData.Add("start_date", "2050-01-01")
postedData.Add("end_date", "2050-01-02")
postedData.Add("first_name", "Abneed")
postedData.Add("last_name", "Rodriguez")
postedData.Add("email", "[email protected]")
postedData.Add("phone", "55-555-5555")
postedData.Add("room_id", "invalid")

req, _ = http.NewRequest("POST", "/make-reservation", strings.NewReader(postedData.Encode()))
ctx = getCtx(req)
req = req.WithContext(ctx)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
Expand All @@ -219,15 +223,16 @@ func TestRepository_PostReservation(t *testing.T) {
}

// test for invalid data
reqBody = "start_date=2050-01-01"
reqBody = fmt.Sprintf("%s&%s", reqBody, "end_date=2050-01-02")
reqBody = fmt.Sprintf("%s&%s", reqBody, "first_name=A")
reqBody = fmt.Sprintf("%s&%s", reqBody, "last_name=Rodriguez")
reqBody = fmt.Sprintf("%s&%s", reqBody, "[email protected]")
reqBody = fmt.Sprintf("%s&%s", reqBody, "phone=+5218671487159")
reqBody = fmt.Sprintf("%s&%s", reqBody, "room_id=1")

req, _ = http.NewRequest("POST", "/make-reservation", strings.NewReader(reqBody))
postedData = url.Values{}
postedData.Add("start_date", "2050-01-01")
postedData.Add("end_date", "2050-01-02")
postedData.Add("first_name", "A")
postedData.Add("last_name", "Rodriguez")
postedData.Add("email", "[email protected]")
postedData.Add("phone", "55-555-5555")
postedData.Add("room_id", "1")

req, _ = http.NewRequest("POST", "/make-reservation", strings.NewReader(postedData.Encode()))
ctx = getCtx(req)
req = req.WithContext(ctx)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
Expand All @@ -242,15 +247,16 @@ func TestRepository_PostReservation(t *testing.T) {
}

// test for failure to insert reservation into database
reqBody = "start_date=2050-01-01"
reqBody = fmt.Sprintf("%s&%s", reqBody, "end_date=2050-01-02")
reqBody = fmt.Sprintf("%s&%s", reqBody, "first_name=Abneed")
reqBody = fmt.Sprintf("%s&%s", reqBody, "last_name=Rodriguez")
reqBody = fmt.Sprintf("%s&%s", reqBody, "[email protected]")
reqBody = fmt.Sprintf("%s&%s", reqBody, "phone=+5218671487159")
reqBody = fmt.Sprintf("%s&%s", reqBody, "room_id=2")

req, _ = http.NewRequest("POST", "/make-reservation", strings.NewReader(reqBody))
postedData = url.Values{}
postedData.Add("start_date", "2050-01-01")
postedData.Add("end_date", "2050-01-02")
postedData.Add("first_name", "Abneed")
postedData.Add("last_name", "Rodriguez")
postedData.Add("email", "[email protected]")
postedData.Add("phone", "55-555-5555")
postedData.Add("room_id", "2")

req, _ = http.NewRequest("POST", "/make-reservation", strings.NewReader(postedData.Encode()))
ctx = getCtx(req)
req = req.WithContext(ctx)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
Expand All @@ -265,15 +271,16 @@ func TestRepository_PostReservation(t *testing.T) {
}

// test for failure to insert restriction into database
reqBody = "start_date=2050-01-01"
reqBody = fmt.Sprintf("%s&%s", reqBody, "end_date=2050-01-02")
reqBody = fmt.Sprintf("%s&%s", reqBody, "first_name=Abneed")
reqBody = fmt.Sprintf("%s&%s", reqBody, "last_name=Rodriguez")
reqBody = fmt.Sprintf("%s&%s", reqBody, "[email protected]")
reqBody = fmt.Sprintf("%s&%s", reqBody, "phone=+5218671487159")
reqBody = fmt.Sprintf("%s&%s", reqBody, "room_id=1000")

req, _ = http.NewRequest("POST", "/make-reservation", strings.NewReader(reqBody))
postedData = url.Values{}
postedData.Add("start_date", "2050-01-01")
postedData.Add("end_date", "2050-01-02")
postedData.Add("first_name", "Abneed")
postedData.Add("last_name", "Rodriguez")
postedData.Add("email", "[email protected]")
postedData.Add("phone", "55-555-5555")
postedData.Add("room_id", "1000")

req, _ = http.NewRequest("POST", "/make-reservation", strings.NewReader(postedData.Encode()))
ctx = getCtx(req)
req = req.WithContext(ctx)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
Expand Down Expand Up @@ -302,11 +309,12 @@ func TestRepository_PostAvailability(t *testing.T) {
// first case -- rooms are not available
*****************************************/
// create our request body
reqBody := "start=2050-01-02"
reqBody = fmt.Sprintf("%s&%s", reqBody, "end=2050-01-02")
postedData := url.Values{}
postedData.Add("start", "2050-01-02")
postedData.Add("end", "2050-01-02")

// create our request
req, _ := http.NewRequest("POST", "/search_availability", strings.NewReader(reqBody))
req, _ := http.NewRequest("POST", "/search_availability", strings.NewReader(postedData.Encode()))

// get the context with session
ctx := getCtx(req)
Expand Down Expand Up @@ -335,11 +343,12 @@ func TestRepository_PostAvailability(t *testing.T) {
*****************************************/
// this time, we specify a start date before 2040-01-01, which will give us
// a non-empty slice, indicating that rooms are available
reqBody = "start=2040-01-01"
reqBody = fmt.Sprintf("%s&%s", reqBody, "end=2040-01-02")
postedData = url.Values{}
postedData.Add("start", "2040-01-01")
postedData.Add("end", "2050-01-02")

// create our request
req, _ = http.NewRequest("POST", "/search-availability", strings.NewReader(reqBody))
req, _ = http.NewRequest("POST", "/search-availability", strings.NewReader(postedData.Encode()))

// get the context with session
ctx = getCtx(req)
Expand Down Expand Up @@ -395,9 +404,10 @@ func TestRepository_PostAvailability(t *testing.T) {
// fourth case -- start date in wrong format
*****************************************/
// this time, we specify a start date in the wrong format
reqBody = "start=invalid"
reqBody = fmt.Sprintf("%s&%s", reqBody, "end=2040-01-02")
req, _ = http.NewRequest("POST", "/search-availability", strings.NewReader(reqBody))
postedData = url.Values{}
postedData.Add("start", "invalid")
postedData.Add("end", "2040-01-02")
req, _ = http.NewRequest("POST", "/search-availability", strings.NewReader(postedData.Encode()))

// get the context with session
ctx = getCtx(req)
Expand Down Expand Up @@ -425,9 +435,10 @@ func TestRepository_PostAvailability(t *testing.T) {
// fifth case -- end date in wrong format
*****************************************/
// this time, we specify a start date in the wrong format
reqBody = "start=2040-01-01"
reqBody = fmt.Sprintf("%s&%s", reqBody, "invalid")
req, _ = http.NewRequest("POST", "/search-availability", strings.NewReader(reqBody))
postedData = url.Values{}
postedData.Add("start", "2040-01-01")
postedData.Add("end", "invalid")
req, _ = http.NewRequest("POST", "/search-availability", strings.NewReader(postedData.Encode()))

// get the context with session
ctx = getCtx(req)
Expand Down Expand Up @@ -456,9 +467,10 @@ func TestRepository_PostAvailability(t *testing.T) {
*****************************************/
// this time, we specify a start date of 2060-01-01, which will cause
// our testdb repo to return an error
reqBody = "start=2060-01-01"
reqBody = fmt.Sprintf("%s&%s", reqBody, "end=2060-01-02")
req, _ = http.NewRequest("POST", "/search-availability", strings.NewReader(reqBody))
postedData = url.Values{}
postedData.Add("start", "2060-01-01")
postedData.Add("end", "2060-01-02")
req, _ = http.NewRequest("POST", "/search-availability", strings.NewReader(postedData.Encode()))

// get the context with session
ctx = getCtx(req)
Expand Down Expand Up @@ -488,12 +500,13 @@ func TestRepository_AvailabilityJSON(t *testing.T) {
// first case -- rooms are not available
*****************************************/
// create our request body
reqBody := "start=2050-01-01"
reqBody = fmt.Sprintf("%s&%s", reqBody, "end=2050-01-02")
reqBody = fmt.Sprintf("%s&%s", reqBody, "room_id=1")
postedData := url.Values{}
postedData.Add("start", "2050-01-01")
postedData.Add("end", "2050-01-02")
postedData.Add("room_id", "1")

// create request
req, _ := http.NewRequest("POST", "/search-availability-json", strings.NewReader(reqBody))
req, _ := http.NewRequest("POST", "/search-availability-json", strings.NewReader(postedData.Encode()))

// get context with session
ctx := getCtx(req)
Expand Down Expand Up @@ -526,15 +539,16 @@ func TestRepository_AvailabilityJSON(t *testing.T) {
}

/*****************************************
// second case -- rooms not available
// second case -- rooms available
*****************************************/
// create our request body
reqBody = "start=2040-01-01"
reqBody = fmt.Sprintf("%s&%s", reqBody, "end=2040-01-02")
reqBody = fmt.Sprintf("%s&%s", reqBody, "room_id=1")
postedData = url.Values{}
postedData.Add("start", "2040-01-01")
postedData.Add("end", "2040-01-02")
postedData.Add("room_id", "1")

// create our request
req, _ = http.NewRequest("POST", "/search-availability-json", strings.NewReader(reqBody))
req, _ = http.NewRequest("POST", "/search-availability-json", strings.NewReader(postedData.Encode()))

// get the context with session
ctx = getCtx(req)
Expand Down Expand Up @@ -602,10 +616,11 @@ func TestRepository_AvailabilityJSON(t *testing.T) {
// fourth case -- database error
*****************************************/
// create our request body
reqBody = "start=2060-01-01"
reqBody = fmt.Sprintf("%s&%s", reqBody, "end=2060-01-02")
reqBody = fmt.Sprintf("%s&%s", reqBody, "room_id=1")
req, _ = http.NewRequest("POST", "/search-availability-json", strings.NewReader(reqBody))
postedData = url.Values{}
postedData.Add("start", "2060-01-01")
postedData.Add("end", "2060-01-02")
postedData.Add("room_id", "1")
req, _ = http.NewRequest("POST", "/search-availability-json", strings.NewReader(postedData.Encode()))

// get the context with session
ctx = getCtx(req)
Expand Down Expand Up @@ -639,12 +654,13 @@ func TestRepository_AvailabilityJSON(t *testing.T) {
// fifth case -- start date not valid
*****************************************/
// create our request body
reqBody = "start=invalid"
reqBody = fmt.Sprintf("%s&%s", reqBody, "end=2050-01-02")
reqBody = fmt.Sprintf("%s&%s", reqBody, "room_id=1")
postedData = url.Values{}
postedData.Add("start", "invalid")
postedData.Add("end", "2060-01-02")
postedData.Add("room_id", "1")

// create request
req, _ = http.NewRequest("POST", "/search-availability-json", strings.NewReader(reqBody))
req, _ = http.NewRequest("POST", "/search-availability-json", strings.NewReader(postedData.Encode()))

// get context with session
ctx = getCtx(req)
Expand Down Expand Up @@ -679,12 +695,13 @@ func TestRepository_AvailabilityJSON(t *testing.T) {
// sixth case -- end date not valid
*****************************************/
// create our request body
reqBody = "start=2050-01-01"
reqBody = fmt.Sprintf("%s&%s", reqBody, "end=invalid")
reqBody = fmt.Sprintf("%s&%s", reqBody, "room_id=1")
postedData = url.Values{}
postedData.Add("start", "2050-01-01")
postedData.Add("end", "invalid")
postedData.Add("room_id", "1")

// create request
req, _ = http.NewRequest("POST", "/search-availability-json", strings.NewReader(reqBody))
req, _ = http.NewRequest("POST", "/search-availability-json", strings.NewReader(postedData.Encode()))

// get context with session
ctx = getCtx(req)
Expand Down

0 comments on commit bafbcb3

Please sign in to comment.