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

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module github.com/nolwn/go-router
go 1.14

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))
}
}

40
router.go Normal file
View File

@ -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
}

62
router_test.go Normal file
View File

@ -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)
// }