Skip to content

Commit

Permalink
completed the handler tests
Browse files Browse the repository at this point in the history
  • Loading branch information
olliephillips committed Apr 29, 2022
1 parent df3e46f commit 7fa6f7f
Show file tree
Hide file tree
Showing 2 changed files with 198 additions and 8 deletions.
14 changes: 8 additions & 6 deletions internal/routes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ type App struct {
Mtx *sync.Mutex
}

// this is the information we will output for list
type listDS struct {
Endpoint string `json:"endpoint"`
Source string `json:"source"`
}

// Home provides basic instruction on how to poll the datasources hosted by the application as text format.
func (a *App) Home(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
Expand All @@ -42,12 +48,6 @@ func (a *App) ListDatasources(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Content-Type", "application/json")

// this is the information we will output for list
type listDS struct {
Endpoint string `json:"endpoint"`
Source string `json:"source"`
}

list := []listDS{}

// iterate the datasources
Expand Down Expand Up @@ -104,6 +104,7 @@ func (a *App) DatasourceGetAll(w http.ResponseWriter, r *http.Request) {
logMsg := fmt.Sprintf("Served GET /%s request - 404 Not Found", dsReq)
a.Logger.Info(logMsg)
w.WriteHeader(http.StatusNotFound)
_, _ = fmt.Fprint(w, "404 page not found")
return
}

Expand Down Expand Up @@ -226,6 +227,7 @@ func (a *App) DatasourceGetByID(w http.ResponseWriter, r *http.Request) {
logMsg := fmt.Sprintf("Served GET /%s/%s request - 404 Not Found", dsReq, id)
a.Logger.Info(logMsg)
w.WriteHeader(http.StatusNotFound)
_, _ = fmt.Fprint(w, "404 page not found")
return
}

Expand Down
192 changes: 190 additions & 2 deletions internal/routes/routes_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package routes

import (
"encoding/json"
"net/http"
"net/http/httptest"
"reflect"
"strings"
"sync"
"testing"

"github.com/gorilla/mux"

"github.com/spoonboy-io/dujour/internal"
"github.com/spoonboy-io/koan"
)
Expand Down Expand Up @@ -38,8 +43,8 @@ func createTestAppContext() *App {
EndpointName: "people3",
Data: map[string]interface{}{
"result": []map[string]interface{}{
{"id": 1, "name": "Test", "age": 100},
{"id": 2, "name": "Test2", "age": 25},
{"id": "abc", "name": "Test", "age": 100},
{"id": "DEF", "name": "Test2", "age": 25},
},
},
},
Expand Down Expand Up @@ -84,3 +89,186 @@ func TestHomeHandler(t *testing.T) {
rr.Body.String(), expected)
}
}

func TestListDatasources(t *testing.T) {
testCases := []struct {
name string
requestMethod string
requestURI string
wantStatus int
wantListDS []listDS
}{
{
"simple request for the /list endpoint should be 200 OK",
"GET",
"/list",
http.StatusOK,
[]listDS{
{
Endpoint: "people",
Source: "data/people.csv",
},
{
Endpoint: "people2",
Source: "data/people2.json",
},
{
Endpoint: "people3",
Source: "data/people3.json",
},
},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {

app := createTestAppContext()

req, err := http.NewRequest(tc.requestMethod, tc.requestURI, nil)
if err != nil {
t.Fatal(err)
}

rr := httptest.NewRecorder()
handler := http.HandlerFunc(app.ListDatasources)
handler.ServeHTTP(rr, req)

if status := rr.Code; status != tc.wantStatus {
t.Errorf("handler returned wrong status code: got %v want %v",
status, tc.wantStatus)
}

gotListDS := []listDS{}
err = json.Unmarshal(rr.Body.Bytes(), &gotListDS)
if err != nil {
t.Fatal(err)
}

if !reflect.DeepEqual(gotListDS, tc.wantListDS) {
t.Errorf("handler returned unexpected body: got %v want %v",
gotListDS, tc.wantListDS)
}

})
}
}

func TestDatasourceGetAll(t *testing.T) {
testCases := []struct {
name string
requestMethod string
requestURI string
wantStatus int
wantBody string
}{
{
"request for /people endpoint should be 200 OK",
"GET",
"/people",
http.StatusOK,
"[{\"age\":\"100\",\"id\":\"1\",\"name\":\"Test\"},{\"age\":\"25\",\"id\":\"2\",\"name\":\"Test2\"}]",
},
{
"request for /servers endpoint should be 404 Not Found",
"GET",
"/servers",
http.StatusNotFound,
"404pagenotfound",
},
{
"request for /People endpoint should be 404 Not Found (we support lowercase routes by design)",
"GET",
"/People",
http.StatusNotFound,
"404pagenotfound",
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {

app := createTestAppContext()

req, err := http.NewRequest(tc.requestMethod, tc.requestURI, nil)
if err != nil {
t.Fatal(err)
}

rr := httptest.NewRecorder()
testMux := mux.NewRouter()
testMux.HandleFunc("/{datasource:[a-z0-9=\\-\\/]+}", app.DatasourceGetAll).Methods("GET")
testMux.ServeHTTP(rr, req)

if status := rr.Code; status != tc.wantStatus {
t.Errorf("handler returned wrong status code: got %v want %v",
status, tc.wantStatus)
}

// we do this to make comparison of the output simpler
gotBody := strings.ReplaceAll(rr.Body.String(), "\n", "")
gotBody = strings.ReplaceAll(gotBody, " ", "")
if gotBody != tc.wantBody {
t.Errorf("handler returned unexpected body: got %v want %v",
gotBody, tc.wantBody)
}

})
}
}

func TestDatasourceGetByID(t *testing.T) {
testCases := []struct {
name string
requestMethod string
requestURI string
wantStatus int
wantBody string
}{
{
"request for /people/1 endpoint should be 200 OK",
"GET",
"/people/1",
http.StatusOK,
"{\"age\":\"100\",\"id\":\"1\",\"name\":\"Test\"}",
},
{
"request for /people/10 endpoint should be 404 Not Found",
"GET",
"/people/10",
http.StatusNotFound,
"404pagenotfound",
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {

app := createTestAppContext()

req, err := http.NewRequest(tc.requestMethod, tc.requestURI, nil)
if err != nil {
t.Fatal(err)
}

rr := httptest.NewRecorder()
testMux := mux.NewRouter()
testMux.HandleFunc("/{datasource:[a-z0-9=\\-\\/]+}/{id:[a-zA-Z0-9=\\-\\/]+}", app.DatasourceGetByID).Methods("GET")
testMux.ServeHTTP(rr, req)

if status := rr.Code; status != tc.wantStatus {
t.Errorf("handler returned wrong status code: got %v want %v",
status, tc.wantStatus)
}

// we do this to make comparison of the output simpler
gotBody := strings.ReplaceAll(rr.Body.String(), "\n", "")
gotBody = strings.ReplaceAll(gotBody, " ", "")
if gotBody != tc.wantBody {
t.Errorf("handler returned unexpected body: got %v want %v",
gotBody, tc.wantBody)
}

})
}
}

0 comments on commit 7fa6f7f

Please sign in to comment.