示例#1
0
func TestGzip(t *testing.T) {
	req, _ := http.NewRequest(vodka.GET, "/", nil)
	rec := httptest.NewRecorder()
	c := vodka.NewContext(req, vodka.NewResponse(rec), vodka.New())
	h := func(c *vodka.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(vodka.GET, "/", nil)
	req.Header.Set(vodka.AcceptEncoding, "gzip")
	rec = httptest.NewRecorder()
	c = vodka.NewContext(req, vodka.NewResponse(rec), vodka.New())

	// Gzip
	Gzip()(h)(c)
	assert.Equal(t, http.StatusOK, rec.Code)
	assert.Equal(t, "gzip", rec.Header().Get(vodka.ContentEncoding))
	assert.Contains(t, rec.Header().Get(vodka.ContentType), vodka.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())
	}
}
示例#2
0
func TestRecover(t *testing.T) {
	e := vodka.New()
	e.SetDebug(true)
	req, _ := http.NewRequest(vodka.GET, "/", nil)
	rec := httptest.NewRecorder()
	c := vodka.NewContext(req, vodka.NewResponse(rec), e)
	h := func(c *vodka.Context) error {
		panic("test")
	}
	Recover()(h)(c)
	assert.Equal(t, http.StatusInternalServerError, rec.Code)
	assert.Contains(t, rec.Body.String(), "panic recover")
}
示例#3
0
func TestRealIPHeader(t *testing.T) {
	e := vodka.New()
	req, _ := http.NewRequest(vodka.GET, "/", nil)
	req.Header.Add("X-Real-IP", "127.0.0.1")
	req.Header.Add("X-Forwarded-For", "127.0.0.1")
	rec := httptest.NewRecorder()
	c := vodka.NewContext(req, vodka.NewResponse(rec), e)

	// Status 2xx
	h := func(c *vodka.Context) error {
		return c.String(http.StatusOK, "test")
	}
	Logger()(h)(c)
}
示例#4
0
func TestLogger(t *testing.T) {
	e := vodka.New()
	req, _ := http.NewRequest(vodka.GET, "/", nil)
	rec := httptest.NewRecorder()
	c := vodka.NewContext(req, vodka.NewResponse(rec), e)

	// Status 2xx
	h := func(c *vodka.Context) error {
		return c.String(http.StatusOK, "test")
	}
	Logger()(h)(c)

	// Status 3xx
	rec = httptest.NewRecorder()
	c = vodka.NewContext(req, vodka.NewResponse(rec), e)
	h = func(c *vodka.Context) error {
		return c.String(http.StatusTemporaryRedirect, "test")
	}
	Logger()(h)(c)

	// Status 4xx
	rec = httptest.NewRecorder()
	c = vodka.NewContext(req, vodka.NewResponse(rec), e)
	h = func(c *vodka.Context) error {
		return c.String(http.StatusNotFound, "test")
	}
	Logger()(h)(c)

	// Status 5xx with empty path
	req, _ = http.NewRequest(vodka.GET, "", nil)
	rec = httptest.NewRecorder()
	c = vodka.NewContext(req, vodka.NewResponse(rec), e)
	h = func(c *vodka.Context) error {
		return errors.New("error")
	}
	Logger()(h)(c)
}
示例#5
0
文件: auth_test.go 项目: hiproz/vodka
func TestBasicAuth(t *testing.T) {
	req, _ := http.NewRequest(vodka.GET, "/", nil)
	rec := httptest.NewRecorder()
	c := vodka.NewContext(req, vodka.NewResponse(rec), vodka.New())
	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(vodka.Authorization, auth)
	assert.NoError(t, ba(c))

	//---------------------
	// Invalid credentials
	//---------------------

	// Incorrect password
	auth = Basic + " " + base64.StdEncoding.EncodeToString([]byte("joe:password"))
	req.Header.Set(vodka.Authorization, auth)
	he := ba(c).(*vodka.HTTPError)
	assert.Equal(t, http.StatusUnauthorized, he.Code())
	assert.Equal(t, Basic+" realm=Restricted", rec.Header().Get(vodka.WWWAuthenticate))

	// Empty Authorization header
	req.Header.Set(vodka.Authorization, "")
	he = ba(c).(*vodka.HTTPError)
	assert.Equal(t, http.StatusUnauthorized, he.Code())
	assert.Equal(t, Basic+" realm=Restricted", rec.Header().Get(vodka.WWWAuthenticate))

	// Invalid Authorization header
	auth = base64.StdEncoding.EncodeToString([]byte("invalid"))
	req.Header.Set(vodka.Authorization, auth)
	he = ba(c).(*vodka.HTTPError)
	assert.Equal(t, http.StatusUnauthorized, he.Code())
	assert.Equal(t, Basic+" realm=Restricted", rec.Header().Get(vodka.WWWAuthenticate))

	// WebSocket
	c.Request().Header.Set(vodka.Upgrade, vodka.WebSocket)
	assert.NoError(t, ba(c))
}
示例#6
0
func BenchmarkGzip(b *testing.B) {

	b.StopTimer()
	b.ReportAllocs()

	h := func(c *vodka.Context) error {
		c.Response().Write([]byte("test")) // For Content-Type sniffing
		return nil
	}
	req, _ := http.NewRequest(vodka.GET, "/", nil)
	req.Header.Set(vodka.AcceptEncoding, "gzip")

	b.StartTimer()

	for i := 0; i < b.N; i++ {
		rec := httptest.NewRecorder()
		c := vodka.NewContext(req, vodka.NewResponse(rec), vodka.New())
		Gzip()(h)(c)
	}

}