Example #1
0
func TestSlashPathsRouting(t *testing.T) {
	m := mix.New()
	m.Get("/hello/world", func(rw http.ResponseWriter, r *http.Request) {
		rw.Write([]byte("Hello World"))
	})

	equals(t, "Hello World", req(m, "GET", "/hello/world/").Body.String())
}
Example #2
0
func TestHead(t *testing.T) {
	m := mix.New()
	m.Get("/", func(rw http.ResponseWriter, r *http.Request) {
		fmt.Fprint(rw, "GET")
	})

	equals(t, "GET", req(m, "HEAD", "/").Body.String())
}
Example #3
0
func TestCustomNotFound(t *testing.T) {
	m := mix.New()
	m.NotFound = func(w http.ResponseWriter, r *http.Request) {
		http.Error(w, "Custom Not Found!", 405)
	}
	res := req(m, "GET", "/not-here")
	equals(t, 405, res.Code)
	equals(t, "Custom Not Found!\n", res.Body.String())
}
Example #4
0
func TestBasicParams(t *testing.T) {
	m := mix.New()
	m.Get("/pages/:pageId/events/:id", func(rw http.ResponseWriter, r *http.Request) {
		params := mix.Params(r)
		fmt.Fprint(rw, params["pageId"], params["id"])
	})

	res := req(m, "GET", "/pages/123/events/456")
	equals(t, "123456", res.Body.String())
}
Example #5
0
func Benchmark1Route(b *testing.B) {
	m := mix.New()
	m.Get("/pages", func(http.ResponseWriter, *http.Request) {
	})
	req, _ := http.NewRequest("GET", "/pages", nil)
	res := httptest.NewRecorder()

	for i := 0; i < b.N; i++ {
		m.ServeHTTP(res, req)
	}
}
Example #6
0
func Benchmark1RouteWithParams(b *testing.B) {
	m := mix.New()
	m.Get("/pages/:pageId/events/:id", func(http.ResponseWriter, *http.Request) {
	})

	req, _ := http.NewRequest("GET", "/pages/123/events/456", nil)
	res := httptest.NewRecorder()

	for i := 0; i < b.N; i++ {
		m.ServeHTTP(res, req)
	}
}
Example #7
0
func TestGroup(t *testing.T) {
	m := mix.New()
	m.Group("/admin", func(r *mix.Router) {
		r.Get("/pages", func(rw http.ResponseWriter, r *http.Request) {
			fmt.Fprint(rw, "pages route")
		})
		r.Get("/events/:id", func(rw http.ResponseWriter, r *http.Request) {
			fmt.Fprint(rw, mix.Params(r)["id"])
		})
	})
	res := req(m, "GET", "/admin/pages")
	equals(t, "pages route", res.Body.String())
	res = req(m, "GET", "/admin/events/123")
	equals(t, "123", res.Body.String())
}
Example #8
0
func TestNestedGroup(t *testing.T) {
	m := mix.New()
	m.Group("/one", func(r *mix.Router) {
		m.Group("/two", func(r *mix.Router) {
			r.Get("/pages", func(rw http.ResponseWriter, r *http.Request) {
				fmt.Fprint(rw, "pages route")
			})
			r.Get("/events", func(rw http.ResponseWriter, r *http.Request) {
				fmt.Fprint(rw, "events route")
			})
		})
	})
	res := req(m, "GET", "/one/two/pages")
	equals(t, "pages route", res.Body.String())
	res = req(m, "GET", "/one/two/events")
	equals(t, "events route", res.Body.String())
}
Example #9
0
func TestGroupMiddleware(t *testing.T) {
	m := mix.New()

	auth := func(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
		fmt.Fprint(rw, "middleware ")
		next(rw, r)
	}

	m.Group("/admin", func(r *mix.Router) {
		r.Get("/foo", func(rw http.ResponseWriter, r *http.Request) {
			fmt.Fprint(rw, "foo route")
		})
	}, negroni.HandlerFunc(auth))

	res := req(m, "GET", "/admin/foo")
	equals(t, "middleware foo route", res.Body.String())
}
Example #10
0
func TestMethods(t *testing.T) {
	m := mix.New()

	fn := func(name string) http.HandlerFunc {
		return func(rw http.ResponseWriter, r *http.Request) {
			fmt.Fprint(rw, "method: ", name)
		}
	}

	m.Get("/", fn("GET"))
	m.Post("/", fn("POST"))
	m.Put("/", fn("PUT"))
	m.Patch("/", fn("PATCH"))
	m.Option("/", fn("OPTION"))
	m.Delete("/", fn("DELETE"))

	for _, method := range []string{"GET", "POST", "PUT", "PATCH", "OPTION", "DELETE"} {
		equals(t, "method: "+method, req(m, method, "/").Body.String())
	}
}
Example #11
0
func TestDefaultNotFound(t *testing.T) {
	m := mix.New()
	res := req(m, "GET", "/not-here")
	equals(t, 404, res.Code)
	equals(t, "404 page not found\n", res.Body.String())
}