Initial commit -- Router and add AddRoute method and tests.

This commit is contained in:
2020-08-30 16:29:15 -07:00
commit 01852dbaf5
4 changed files with 149 additions and 0 deletions

44
integration_test.go Normal file
View File

@ -0,0 +1,44 @@
package router
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
)
func SetupTestServer() (handler http.Handler) {
// r := NewRouter()
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("You called the root endpoint."))
})
handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
mux.ServeHTTP(w, r)
})
return
}
func TestServer(t *testing.T) {
var client http.Client
handler := SetupTestServer()
ts := httptest.NewServer(handler)
defer ts.Close()
request, err := http.NewRequest(http.MethodGet, ts.URL, nil)
res, err := client.Do(request)
if err != nil {
t.Fatal("Could not make request", err)
}
body, err := ioutil.ReadAll(res.Body)
res.Body.Close()
if string(body) != "You called the root endpoint." {
t.Errorf("Did not recieve the correct response. Expected 'You called the root endpoint.' Received: %s", string(body))
}
}