From 01852dbaf58a31196fddc3a71f35c61df9b9c90a Mon Sep 17 00:00:00 2001 From: nolwn Date: Sun, 30 Aug 2020 16:29:15 -0700 Subject: [PATCH] Initial commit -- Router and add AddRoute method and tests. --- go.mod | 3 +++ integration_test.go | 44 ++++++++++++++++++++++++++++++++ router.go | 40 +++++++++++++++++++++++++++++ router_test.go | 62 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 149 insertions(+) create mode 100644 go.mod create mode 100644 integration_test.go create mode 100644 router.go create mode 100644 router_test.go diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..1be6f7d --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/nolwn/go-router + +go 1.14 \ No newline at end of file diff --git a/integration_test.go b/integration_test.go new file mode 100644 index 0000000..8735b22 --- /dev/null +++ b/integration_test.go @@ -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)) + } +} diff --git a/router.go b/router.go new file mode 100644 index 0000000..3f2e095 --- /dev/null +++ b/router.go @@ -0,0 +1,40 @@ +package router + +import "net/http" + +type route struct { + method string + path string + callback http.HandlerFunc +} + +// Router is the main router object that keeps track of an looks up routes. +type Router struct { + routes []route +} + +// NewRouter is a constructor for Router. +func NewRouter() (r Router) { + return +} + +// AddRoute adds a new route with a corresponding callback to the router. +func (r *Router) AddRoute(method string, path string, callback http.HandlerFunc) (err error) { + r.routes = append(r.routes, route{method, path, callback}) + + return +} + +// Get is a convinience method which calls Router.AddRoute with the "GET" method. +func (r *Router) Get(path string, callback http.HandlerFunc) { + r.AddRoute(http.MethodGet, path, callback) +} + +// // Handle I don't know what this is yet, I assume it's called when there's a request +// func (r *Router) Handle(w http.ResponseWriter, req *http.Request) { +// return +// } + +func (r Router) ServeHTTP(w http.ResponseWriter, req *http.Request) { + return +} diff --git a/router_test.go b/router_test.go new file mode 100644 index 0000000..d1a1a0e --- /dev/null +++ b/router_test.go @@ -0,0 +1,62 @@ +package router + +import ( + "fmt" + "net/http" + "testing" +) + +func TestAddRouter(t *testing.T) { + r := Router{} + + err := addAndCheckRoute(r, http.MethodGet, "/", func(http.ResponseWriter, *http.Request) {}, 0) + + if err != nil { + t.Error("The route was not correctly added to the router: ", err) + } +} + +func addAndCheckRoute(r Router, method string, path string, callback http.HandlerFunc, expectedIndex int) (err error) { + err = r.AddRoute(method, path, callback) + + if err != nil { + return + } + + if len(r.routes) != expectedIndex+1 { + err = fmt.Errorf("Expected there to be %d route(s), but there are %d", expectedIndex+1, len(r.routes)) + + return + } + + route := r.routes[expectedIndex] + + if route.method != method { + err = fmt.Errorf("Expected the route method to be %s, but it was %s", method, route.method) + + return + } + + if route.path != path { + err = fmt.Errorf("Expected the route path to be %s, but it was %s", path, route.path) + + return + } + + if route.callback == nil { + err = fmt.Errorf("Expected route to have a callback function, but the callback was nil") + + return + } + + return +} + +// func TestHandle(t *testing.T) { +// r := NewRouter() + +// request, _ := http.NewRequest(http.MethodGet, "http://example.domain/api", nil) +// var writer http.ResponseWriter + +// r.Handle(writer, request) +// }