func TestGzip(t *testing.T) { e := core.New() req, _ := http.NewRequest(core.GET, "/", nil) rec := httptest.NewRecorder() c := core.NewContext(req, core.NewResponse(rec, e), e) h := func(c *core.Context) error { c.Response().Write([]byte("test")) // For Content-Type sniffing return nil } // Skip if no Accept-Encoding header Gzip()(h)(c) assert.Equal(t, http.StatusOK, rec.Code) assert.Equal(t, "test", rec.Body.String()) req, _ = http.NewRequest(core.GET, "/", nil) req.Header.Set(core.AcceptEncoding, "gzip") rec = httptest.NewRecorder() c = core.NewContext(req, core.NewResponse(rec, e), e) // Gzip Gzip()(h)(c) assert.Equal(t, http.StatusOK, rec.Code) assert.Equal(t, "gzip", rec.Header().Get(core.ContentEncoding)) assert.Contains(t, rec.Header().Get(core.ContentType), core.TextPlain) r, err := gzip.NewReader(rec.Body) defer r.Close() if assert.NoError(t, err) { buf := new(bytes.Buffer) buf.ReadFrom(r) assert.Equal(t, "test", buf.String()) } }
func TestLoggerIPAddress(t *testing.T) { e := core.New() req, _ := http.NewRequest(core.GET, "/", nil) rec := httptest.NewRecorder() c := core.NewContext(req, core.NewResponse(rec, e), e) buf := new(bytes.Buffer) e.Logger().SetOutput(buf) ip := "127.0.0.1" h := func(c *core.Context) error { return c.String(http.StatusOK, "test") } mw := Logger() // With X-Real-IP req.Header.Add(core.XRealIP, ip) mw(h)(c) assert.Contains(t, buf.String(), ip) // With X-Forwarded-For buf.Reset() req.Header.Del(core.XRealIP) req.Header.Add(core.XForwardedFor, ip) mw(h)(c) assert.Contains(t, buf.String(), ip) // with req.RemoteAddr buf.Reset() mw(h)(c) assert.Contains(t, buf.String(), ip) }
func TestRecover(t *testing.T) { e := core.New() e.SetDebug(true) req, _ := http.NewRequest(core.GET, "/", nil) rec := httptest.NewRecorder() c := core.NewContext(req, core.NewResponse(rec, e), e) h := func(c *core.Context) error { panic("test") } Recover()(h)(c) assert.Equal(t, http.StatusInternalServerError, rec.Code) assert.Contains(t, rec.Body.String(), "panic recover") }
func TestLogger(t *testing.T) { // Note: Just for the test coverage, not a real test. e := core.New() req, _ := http.NewRequest(core.GET, "/", nil) rec := httptest.NewRecorder() c := core.NewContext(req, core.NewResponse(rec, e), e) // Status 2xx h := func(c *core.Context) error { return c.String(http.StatusOK, "test") } Logger()(h)(c) // Status 3xx rec = httptest.NewRecorder() c = core.NewContext(req, core.NewResponse(rec, e), e) h = func(c *core.Context) error { return c.String(http.StatusTemporaryRedirect, "test") } Logger()(h)(c) // Status 4xx rec = httptest.NewRecorder() c = core.NewContext(req, core.NewResponse(rec, e), e) h = func(c *core.Context) error { return c.String(http.StatusNotFound, "test") } Logger()(h)(c) // Status 5xx with empty path req, _ = http.NewRequest(core.GET, "", nil) rec = httptest.NewRecorder() c = core.NewContext(req, core.NewResponse(rec, e), e) h = func(c *core.Context) error { return errors.New("error") } Logger()(h)(c) }
func TestBasicAuth(t *testing.T) { e := core.New() req, _ := http.NewRequest(core.GET, "/", nil) rec := httptest.NewRecorder() c := core.NewContext(req, core.NewResponse(rec, e), e) fn := func(u, p string) bool { if u == "joe" && p == "secret" { return true } return false } ba := BasicAuth(fn) // Valid credentials auth := Basic + " " + base64.StdEncoding.EncodeToString([]byte("joe:secret")) req.Header.Set(core.Authorization, auth) assert.NoError(t, ba(c)) //--------------------- // Invalid credentials //--------------------- // Incorrect password auth = Basic + " " + base64.StdEncoding.EncodeToString([]byte("joe:password")) req.Header.Set(core.Authorization, auth) he := ba(c).(*core.HTTPError) assert.Equal(t, http.StatusUnauthorized, he.Code()) assert.Equal(t, Basic+" realm=Restricted", rec.Header().Get(core.WWWAuthenticate)) // Empty Authorization header req.Header.Set(core.Authorization, "") he = ba(c).(*core.HTTPError) assert.Equal(t, http.StatusUnauthorized, he.Code()) assert.Equal(t, Basic+" realm=Restricted", rec.Header().Get(core.WWWAuthenticate)) // Invalid Authorization header auth = base64.StdEncoding.EncodeToString([]byte("invalid")) req.Header.Set(core.Authorization, auth) he = ba(c).(*core.HTTPError) assert.Equal(t, http.StatusUnauthorized, he.Code()) assert.Equal(t, Basic+" realm=Restricted", rec.Header().Get(core.WWWAuthenticate)) // WebSocket c.Request().Header.Set(core.Upgrade, core.WebSocket) assert.NoError(t, ba(c)) }
func BenchmarkGzip(b *testing.B) { b.StopTimer() b.ReportAllocs() h := func(c *core.Context) error { c.Response().Write([]byte("test")) // For Content-Type sniffing return nil } req, _ := http.NewRequest(core.GET, "/", nil) req.Header.Set(core.AcceptEncoding, "gzip") b.StartTimer() for i := 0; i < b.N; i++ { e := core.New() rec := httptest.NewRecorder() c := core.NewContext(req, core.NewResponse(rec, e), e) Gzip()(h)(c) } }