Skip to content

Commit

Permalink
More table driven tests for sub routes
Browse files Browse the repository at this point in the history
  • Loading branch information
krak3n committed Oct 1, 2015
1 parent 358864c commit 7507cf0
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 7 deletions.
19 changes: 12 additions & 7 deletions yam.go
Expand Up @@ -62,26 +62,31 @@ func route(path string, base *Route, y *Yam) *Route {
var route *Route
// /foo/bar/baz [foo bar baz]
parts := strings.Split(path, "/")[1:]
//
// Our starting routes we loop over should be the base route
route = base

// Iterate over the parts
var found bool
for _, part := range parts {
// Iterate over the routes on
found = false
for _, r := range route.Routes {
// This part of the path already exists in the routes
if r.leaf == part {
// Set our base route to now be this route
route = r
found = true
break
}
}
// The part of the path does not exist in the routes, create it
r := &Route{leaf: part, yam: y}
// Add the route to the list of routes
route.Routes = append(route.Routes, r)
// Set the next route to be the one we just created
route = r
if !found {
// The part of the path does not exist in the routes, create it
r := &Route{leaf: part, yam: y}
// Add the route to the list of routes
route.Routes = append(route.Routes, r)
// Set the next route to be the one we just created
route = r
}
}

return route
Expand Down
61 changes: 61 additions & 0 deletions yam_test.go
Expand Up @@ -451,3 +451,64 @@ func TestDelete(t *testing.T) {
t.Errorf("Body was\n%vshould be:\n%v", string(body[:]), string(expected[:]))
}
}

func TestSubRoutes(t *testing.T) {
fn := func(body string) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(body[:]))
}
}

mux := New()
mux.Route("/").Get(fn("GET /"))
bar := mux.Route("/bar").Get(fn("GET /bar"))
bar.Post(fn("Post /bar"))
foo := mux.Route("/foo").Get(fn("GET /foo"))
foo.Route("/:bar/baz").Get(fn("GET /foo/:bar/baz"))
mux.Route("/foo").Put(fn("PUT /foo"))

s := httptest.NewServer(mux)
defer s.Close()

// Table Teest It
var tests = []struct {
request TestRequest
response TestResponse
}{
{
TestRequest{"/", "GET"},
TestResponse{http.StatusOK, []byte("GET /")},
},
{
TestRequest{"/bar", "GET"},
TestResponse{http.StatusOK, []byte("GET /bar")},
},
{
TestRequest{"/bar", "POST"},
TestResponse{http.StatusOK, []byte("POST /bar")},
},
{
TestRequest{"/foo", "GET"},
TestResponse{http.StatusOK, []byte("GET /foo")},
},
{
TestRequest{"/foo", "PUT"},
TestResponse{http.StatusOK, []byte("PUT /foo")},
},
{
TestRequest{"/foo/antyhing/baz", "GET"},
TestResponse{http.StatusOK, []byte("GET /foo/:bar/baz")},
},
}

for _, test := range tests {
req, _ := http.NewRequest(test.request.Method, s.URL+test.request.Path, nil)
c := &http.Client{}
res, _ := c.Do(req)

if res.StatusCode != test.response.Status {
t.Errorf("Status was: %v, should be: %v", res.StatusCode, test.response.Status)
}
}

}

0 comments on commit 7507cf0

Please sign in to comment.