Example #1
0
func TestSubRoute(t *testing.T) {
	api := ninja.New()

	calledRoute1 := false
	calledRoute2 := false
	api.Route("/foo/bar",
		ninja.Route{
			"GET": func(w http.ResponseWriter, r *http.Request) {
				calledRoute1 = true
			},
		},
	)
	api.Route("/foo/bar/baz",
		ninja.Route{
			"GET": func(w http.ResponseWriter, r *http.Request) {
				calledRoute2 = true
			},
		},
	)
	request, _ := http.NewRequest("GET", "/foo/bar/baz", nil)

	api.ServeHTTP(mockResponseWriter{}, request)

	if calledRoute1 {
		t.Error("Should NOT have called the less specific route")
	}

	if !calledRoute2 {
		t.Error("Should have called the more specific route")
	}
}
Example #2
0
func TestMiddleware(t *testing.T) {
	api := ninja.New()

	calledRoute := false
	calledMiddleware := false
	api.Route("/foo/bar",
		ninja.Route{
			"GET": func(w http.ResponseWriter, r *http.Request) {
				calledRoute = true
			},
		},
		func(w http.ResponseWriter, r *http.Request) error {
			calledMiddleware = true
			return errors.New("")
		},
	)

	request, _ := http.NewRequest("GET", "/foo/bar", nil)

	api.ServeHTTP(mockResponseWriter{}, request)

	if calledRoute {
		t.Error("Should NOT have called the route method")
	}

	if !calledMiddleware {
		t.Error("Should have called the middleware method")
	}
}
Example #3
0
func TestMethodMuxing(t *testing.T) {
	api := ninja.New()

	calledRoute1 := false
	calledRoute2 := false
	api.Route("/foo/bar",
		ninja.Route{
			"GET": func(w http.ResponseWriter, r *http.Request) {
				calledRoute1 = true
			},
			"POST": func(w http.ResponseWriter, r *http.Request) {
				calledRoute2 = true
			},
		},
	)
	request, _ := http.NewRequest("POST", "/foo/bar", nil)

	api.ServeHTTP(mockResponseWriter{}, request)

	if calledRoute1 {
		t.Error("Should NOT have called the GET method")
	}

	if !calledRoute2 {
		t.Error("Should have called the POST method")
	}
}
Example #4
0
func TestRouteNotCoveringSubRoutes(t *testing.T) {
	api := ninja.New()

	calledRoute := false
	api.Route("/foo/bar",
		ninja.Route{
			"GET": func(w http.ResponseWriter, r *http.Request) {
				calledRoute = true
			},
		},
	)
	request, _ := http.NewRequest("GET", "/foo/bar/baz", nil)

	api.ServeHTTP(mockResponseWriter{}, request)

	if calledRoute {
		t.Error("Should NOT have called the route method")
	}
}