Пример #1
0
func SetupNamed(t *testing.T, name, method string) (*httptest.ResponseRecorder, string) {
	mux := router.New()
	switch method {
	case "GET":
		mux.NamedGet(name, "/hello", Hello)
	case "POST":
		mux.NamedPost(name, "/hello", Hello)
	case "PATCH":
		mux.NamedPatch(name, "/hello", Hello)
	case "DELETE":
		mux.NamedDel(name, "/hello", Hello)
	}

	w := httptest.NewRecorder()
	r, err := http.NewRequest(method, "/hello", nil)
	if err != nil {
		t.Fatal(err)
	}

	mux.ServeHTTP(w, r)

	url, err := mux.URLFor("barney the dinosaur")
	if err != nil {
		t.Fatal(err)
	}

	return w, url
}
Пример #2
0
func TestURLFor(t *testing.T) {
	err := func() error {
		mux := router.New()
		_, err := mux.URLFor("no name here")
		return err
	}()

	It("should return an error if a name has no handler", func() {
		assert.EqualError(t, err, `No route with name "no name here" found.`)
	})

	err = func() error {
		mux := router.New()
		mux.NamedGet("dinosaur", "/hello", Hello)
		_, err := mux.URLFor("dinosaur", "whoops this shouldn't be here")
		return err
	}()

	It("should dummy test gorilla/mux for the sake of code coverage", func() {
		assert.Error(t, err)
	})
}
Пример #3
0
func Setup(t *testing.T, method string) *httptest.ResponseRecorder {
	mux := router.New()
	switch method {
	case "GET":
		mux.Get("/hello", Hello)
	case "POST":
		mux.Post("/hello", Hello)
	case "PATCH":
		mux.Patch("/hello", Hello)
	case "DELETE":
		mux.Del("/hello", Hello)
	}

	w := httptest.NewRecorder()
	r, err := http.NewRequest(method, "/hello", nil)
	if err != nil {
		t.Fatal(err)
	}

	mux.ServeHTTP(w, r)

	return w
}