Пример #1
0
func TestAdapters(t *testing.T) {
	hndlr := net.Adapt(
		h1{},
		net.SupportXHTTPMethodOverride(),
		net.WithHeader("X-Men", "Y-Women"))
	w := httptest.NewRecorder()
	req, err := http.NewRequest(net.HTTPMethodGet, "http://example.com/foo", nil)
	req.Header.Set(net.HTTPMethodOverrideHeader, net.HTTPMethodPut)
	assert.NoError(t, err)
	hndlr.ServeHTTP(w, req)
	assert.Equal(t, net.HTTPMethodPut, req.Method)
	assert.Equal(t, "h1 called", w.Body.String())
	assert.Equal(t, "Y-Women", w.Header().Get("X-Men"))
}
Пример #2
0
func TestHttpMethodOverride(t *testing.T) {
	hndlr := net.Adapt(
		h1{},
		net.SupportXHTTPMethodOverride())
	w := httptest.NewRecorder()
	req, err := http.NewRequest(net.HTTPMethodGet, "http://example.com/foo?_method="+net.HTTPMethodPatch, nil)
	assert.NoError(t, err)
	hndlr.ServeHTTP(w, req)
	assert.Equal(t, net.HTTPMethodPatch, req.Method)
	assert.Equal(t, "h1 called", w.Body.String())

	w = httptest.NewRecorder()
	req, err = http.NewRequest(net.HTTPMethodGet, "http://example.com/foo?_method=KARATE", nil)
	assert.NoError(t, err)
	hndlr.ServeHTTP(w, req)
	assert.Equal(t, net.HTTPMethodGet, req.Method)

	w = httptest.NewRecorder()
	req, err = http.NewRequest(net.HTTPMethodGet, "http://example.com/foobar", nil)
	assert.NoError(t, err)
	hndlr.ServeHTTP(w, req)
	assert.Equal(t, net.HTTPMethodGet, req.Method)

}