本文整理汇总了Golang中github.com/insionng/vodka/test.NewRequest函数的典型用法代码示例。如果您正苦于以下问题:Golang NewRequest函数的具体用法?Golang NewRequest怎么用?Golang NewRequest使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewRequest函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: TestRemoveTrailingSlash
func TestRemoveTrailingSlash(t *testing.T) {
e := vodka.New()
req := test.NewRequest(vodka.GET, "/remove-slash/", nil)
rec := test.NewResponseRecorder()
c := e.NewContext(req, rec)
h := RemoveTrailingSlash()(func(c vodka.Context) error {
return nil
})
h(c)
assert.Equal(t, "/remove-slash", req.URL().Path())
assert.Equal(t, "/remove-slash", req.URI())
// With config
req = test.NewRequest(vodka.GET, "/remove-slash/?key=value", nil)
rec = test.NewResponseRecorder()
c = e.NewContext(req, rec)
h = RemoveTrailingSlashWithConfig(TrailingSlashConfig{
RedirectCode: http.StatusMovedPermanently,
})(func(c vodka.Context) error {
return nil
})
h(c)
assert.Equal(t, http.StatusMovedPermanently, rec.Status())
assert.Equal(t, "/remove-slash?key=value", rec.Header().Get(vodka.HeaderLocation))
// With bare URL
req = test.NewRequest(vodka.GET, "http://localhost", nil)
rec = test.NewResponseRecorder()
c = e.NewContext(req, rec)
h = RemoveTrailingSlash()(func(c vodka.Context) error {
return nil
})
h(c)
assert.Equal(t, "", req.URL().Path())
}
开发者ID:insionng,项目名称:vodka,代码行数:35,代码来源:slash_test.go
示例2: TestGzip
func TestGzip(t *testing.T) {
e := vodka.New()
req := test.NewRequest(vodka.GET, "/", nil)
rec := test.NewResponseRecorder()
c := e.NewContext(req, rec)
// Skip if no Accept-Encoding header
h := Gzip()(func(c vodka.Context) error {
c.Response().Write([]byte("test")) // For Content-Type sniffing
return nil
})
h(c)
assert.Equal(t, "test", rec.Body.String())
req = test.NewRequest(vodka.GET, "/", nil)
req.Header().Set(vodka.HeaderAcceptEncoding, "gzip")
rec = test.NewResponseRecorder()
c = e.NewContext(req, rec)
// Gzip
h(c)
assert.Equal(t, "gzip", rec.Header().Get(vodka.HeaderContentEncoding))
assert.Contains(t, rec.Header().Get(vodka.HeaderContentType), vodka.MIMETextPlain)
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())
}
}
开发者ID:insionng,项目名称:vodka,代码行数:31,代码来源:compress_test.go
示例3: TestCORS
func TestCORS(t *testing.T) {
e := vodka.New()
req := test.NewRequest(vodka.GET, "/", nil)
rec := test.NewResponseRecorder()
c := e.NewContext(req, rec)
cors := CORSWithConfig(CORSConfig{
AllowCredentials: true,
})
h := cors(func(c vodka.Context) error {
return c.String(http.StatusOK, "test")
})
// No origin header
h(c)
assert.Equal(t, "", rec.Header().Get(vodka.HeaderAccessControlAllowOrigin))
// Empty origin header
req = test.NewRequest(vodka.GET, "/", nil)
rec = test.NewResponseRecorder()
c = e.NewContext(req, rec)
req.Header().Set(vodka.HeaderOrigin, "")
h(c)
assert.Equal(t, "*", rec.Header().Get(vodka.HeaderAccessControlAllowOrigin))
// Wildcard origin
req = test.NewRequest(vodka.GET, "/", nil)
rec = test.NewResponseRecorder()
c = e.NewContext(req, rec)
req.Header().Set(vodka.HeaderOrigin, "localhost")
h(c)
assert.Equal(t, "*", rec.Header().Get(vodka.HeaderAccessControlAllowOrigin))
// Simple request
req = test.NewRequest(vodka.GET, "/", nil)
rec = test.NewResponseRecorder()
c = e.NewContext(req, rec)
req.Header().Set(vodka.HeaderOrigin, "localhost")
cors = CORSWithConfig(CORSConfig{
AllowOrigins: []string{"localhost"},
AllowCredentials: true,
MaxAge: 3600,
})
h = cors(func(c vodka.Context) error {
return c.String(http.StatusOK, "test")
})
h(c)
assert.Equal(t, "localhost", rec.Header().Get(vodka.HeaderAccessControlAllowOrigin))
// Preflight request
req = test.NewRequest(vodka.OPTIONS, "/", nil)
rec = test.NewResponseRecorder()
c = e.NewContext(req, rec)
req.Header().Set(vodka.HeaderOrigin, "localhost")
req.Header().Set(vodka.HeaderContentType, vodka.MIMEApplicationJSON)
h(c)
assert.Equal(t, "localhost", rec.Header().Get(vodka.HeaderAccessControlAllowOrigin))
assert.NotEmpty(t, rec.Header().Get(vodka.HeaderAccessControlAllowMethods))
assert.Equal(t, "true", rec.Header().Get(vodka.HeaderAccessControlAllowCredentials))
assert.Equal(t, "3600", rec.Header().Get(vodka.HeaderAccessControlMaxAge))
}
开发者ID:insionng,项目名称:vodka,代码行数:60,代码来源:cors_test.go
示例4: TestStatic
func TestStatic(t *testing.T) {
e := vodka.New()
req := test.NewRequest(vodka.GET, "/", nil)
rec := test.NewResponseRecorder()
c := e.NewContext(req, rec)
h := Static("../_fixture")(func(c vodka.Context) error {
return vodka.ErrNotFound
})
// Directory
if assert.NoError(t, h(c)) {
assert.Contains(t, rec.Body.String(), "Vodka")
}
// HTML5 mode
req = test.NewRequest(vodka.GET, "/client", nil)
rec = test.NewResponseRecorder()
c = e.NewContext(req, rec)
static := StaticWithConfig(StaticConfig{
Root: "../_fixture",
HTML5: true,
})
h = static(func(c vodka.Context) error {
return vodka.ErrNotFound
})
if assert.NoError(t, h(c)) {
assert.Equal(t, http.StatusOK, rec.Status())
}
// Browse
req = test.NewRequest(vodka.GET, "/", nil)
rec = test.NewResponseRecorder()
c = e.NewContext(req, rec)
static = StaticWithConfig(StaticConfig{
Root: "../_fixture/images",
Browse: true,
})
h = static(func(c vodka.Context) error {
return vodka.ErrNotFound
})
if assert.NoError(t, h(c)) {
assert.Contains(t, rec.Body.String(), "walle")
}
// Not found
req = test.NewRequest(vodka.GET, "/not-found", nil)
rec = test.NewResponseRecorder()
c = e.NewContext(req, rec)
static = StaticWithConfig(StaticConfig{
Root: "../_fixture/images",
})
h = static(func(c vodka.Context) error {
return vodka.ErrNotFound
})
assert.Error(t, h(c))
}
开发者ID:insionng,项目名称:vodka,代码行数:56,代码来源:static_test.go
示例5: TestLoggerIPAddress
func TestLoggerIPAddress(t *testing.T) {
e := vodka.New()
req := test.NewRequest(vodka.GET, "/", nil)
rec := test.NewResponseRecorder()
c := e.NewContext(req, rec)
buf := new(bytes.Buffer)
e.Logger().SetOutput(buf)
ip := "127.0.0.1"
h := Logger()(func(c vodka.Context) error {
return c.String(http.StatusOK, "test")
})
// With X-Real-IP
req.Header().Add(vodka.HeaderXRealIP, ip)
h(c)
assert.Contains(t, ip, buf.String())
// With X-Forwarded-For
buf.Reset()
req.Header().Del(vodka.HeaderXRealIP)
req.Header().Add(vodka.HeaderXForwardedFor, ip)
h(c)
assert.Contains(t, ip, buf.String())
buf.Reset()
h(c)
assert.Contains(t, ip, buf.String())
}
开发者ID:insionng,项目名称:vodka,代码行数:28,代码来源:logger_test.go
示例6: TestVodkaNotFound
func TestVodkaNotFound(t *testing.T) {
e := New()
req := test.NewRequest(GET, "/files", nil)
rec := test.NewResponseRecorder()
e.ServeHTTP(req, rec)
assert.Equal(t, http.StatusNotFound, rec.Status())
}
开发者ID:insionng,项目名称:vodka,代码行数:7,代码来源:vodka_test.go
示例7: TestSecure
func TestSecure(t *testing.T) {
e := vodka.New()
req := test.NewRequest(vodka.GET, "/", nil)
rec := test.NewResponseRecorder()
c := e.NewContext(req, rec)
h := func(c vodka.Context) error {
return c.String(http.StatusOK, "test")
}
// Default
Secure()(h)(c)
assert.Equal(t, "1; mode=block", rec.Header().Get(vodka.HeaderXXSSProtection))
assert.Equal(t, "nosniff", rec.Header().Get(vodka.HeaderXContentTypeOptions))
assert.Equal(t, "SAMEORIGIN", rec.Header().Get(vodka.HeaderXFrameOptions))
assert.Equal(t, "", rec.Header().Get(vodka.HeaderStrictTransportSecurity))
assert.Equal(t, "", rec.Header().Get(vodka.HeaderContentSecurityPolicy))
// Custom
req.Header().Set(vodka.HeaderXForwardedProto, "https")
rec = test.NewResponseRecorder()
c = e.NewContext(req, rec)
SecureWithConfig(SecureConfig{
XSSProtection: "",
ContentTypeNosniff: "",
XFrameOptions: "",
HSTSMaxAge: 3600,
ContentSecurityPolicy: "default-src 'self'",
})(h)(c)
assert.Equal(t, "", rec.Header().Get(vodka.HeaderXXSSProtection))
assert.Equal(t, "", rec.Header().Get(vodka.HeaderXContentTypeOptions))
assert.Equal(t, "", rec.Header().Get(vodka.HeaderXFrameOptions))
assert.Equal(t, "max-age=3600; includeSubdomains", rec.Header().Get(vodka.HeaderStrictTransportSecurity))
assert.Equal(t, "default-src 'self'", rec.Header().Get(vodka.HeaderContentSecurityPolicy))
}
开发者ID:insionng,项目名称:vodka,代码行数:34,代码来源:secure_test.go
示例8: TestContextServeContent
func TestContextServeContent(t *testing.T) {
e := New()
req := test.NewRequest(GET, "/", nil)
rec := test.NewResponseRecorder()
c := e.NewContext(req, rec)
fs := http.Dir("_fixture/images")
f, err := fs.Open("walle.png")
if assert.NoError(t, err) {
fi, err := f.Stat()
if assert.NoError(t, err) {
// Not cached
if assert.NoError(t, c.ServeContent(f, fi.Name(), fi.ModTime())) {
assert.Equal(t, http.StatusOK, rec.Status())
}
// Cached
rec = test.NewResponseRecorder()
c = e.NewContext(req, rec)
req.Header().Set(HeaderIfModifiedSince, fi.ModTime().UTC().Format(http.TimeFormat))
if assert.NoError(t, c.ServeContent(f, fi.Name(), fi.ModTime())) {
assert.Equal(t, http.StatusNotModified, rec.Status())
}
}
}
}
开发者ID:insionng,项目名称:vodka,代码行数:26,代码来源:context_test.go
示例9: TestVodkaMethodNotAllowed
func TestVodkaMethodNotAllowed(t *testing.T) {
e := New()
e.GET("/", func(c Context) error {
return c.String(http.StatusOK, "Vodka!")
})
req := test.NewRequest(POST, "/", nil)
rec := test.NewResponseRecorder()
e.ServeHTTP(req, rec)
assert.Equal(t, http.StatusMethodNotAllowed, rec.Status())
}
开发者ID:insionng,项目名称:vodka,代码行数:10,代码来源:vodka_test.go
示例10: TestContextRedirect
func TestContextRedirect(t *testing.T) {
e := New()
req := test.NewRequest(GET, "/", nil)
rec := test.NewResponseRecorder()
c := e.NewContext(req, rec)
assert.Equal(t, nil, c.Redirect(http.StatusMovedPermanently, "http://insionng.github.io/vodka"))
assert.Equal(t, http.StatusMovedPermanently, rec.Status())
assert.Equal(t, "http://insionng.github.io/vodka", rec.Header().Get(HeaderLocation))
assert.Error(t, c.Redirect(310, "http://insionng.github.io/vodka"))
}
开发者ID:insionng,项目名称:vodka,代码行数:10,代码来源:context_test.go
示例11: TestBinderQueryParams
func TestBinderQueryParams(t *testing.T) {
e := New()
req := test.NewRequest(GET, "/?id=1&name=Jon Snow", nil)
rec := test.NewResponseRecorder()
c := e.NewContext(req, rec)
u := new(user)
err := c.Bind(u)
if assert.NoError(t, err) {
assert.Equal(t, 1, u.ID)
assert.Equal(t, "Jon Snow", u.Name)
}
}
开发者ID:insionng,项目名称:vodka,代码行数:12,代码来源:binder_test.go
示例12: TestBinderForm
func TestBinderForm(t *testing.T) {
testBinderOkay(t, strings.NewReader(userForm), MIMEApplicationForm)
testBinderError(t, nil, MIMEApplicationForm)
e := New()
req := test.NewRequest(POST, "/", strings.NewReader(userForm))
rec := test.NewResponseRecorder()
c := e.NewContext(req, rec)
req.Header().Set(HeaderContentType, MIMEApplicationForm)
var obj = make([]struct{ Field string }, 0)
err := c.Bind(&obj)
assert.Error(t, err)
}
开发者ID:insionng,项目名称:vodka,代码行数:12,代码来源:binder_test.go
示例13: TestGzipErrorReturned
func TestGzipErrorReturned(t *testing.T) {
e := vodka.New()
e.Use(Gzip())
e.GET("/", func(c vodka.Context) error {
return vodka.NewHTTPError(http.StatusInternalServerError, "error")
})
req := test.NewRequest(vodka.GET, "/", nil)
rec := test.NewResponseRecorder()
e.ServeHTTP(req, rec)
assert.Empty(t, rec.Header().Get(vodka.HeaderContentEncoding))
assert.Equal(t, "error", rec.Body.String())
}
开发者ID:insionng,项目名称:vodka,代码行数:12,代码来源:compress_test.go
示例14: TestNonWWWRedirect
func TestNonWWWRedirect(t *testing.T) {
e := vodka.New()
next := func(c vodka.Context) (err error) {
return c.NoContent(http.StatusOK)
}
req := test.NewRequest(vodka.GET, "http://www.insionng.com", nil)
res := test.NewResponseRecorder()
c := e.NewContext(req, res)
NonWWWRedirect()(next)(c)
assert.Equal(t, http.StatusMovedPermanently, res.Status())
assert.Equal(t, "http://insionng.com", res.Header().Get(vodka.HeaderLocation))
}
开发者ID:insionng,项目名称:vodka,代码行数:12,代码来源:redirect_test.go
示例15: TestBodyLimit
func TestBodyLimit(t *testing.T) {
e := vodka.New()
hw := []byte("Hello, World!")
req := test.NewRequest(vodka.POST, "/", bytes.NewReader(hw))
rec := test.NewResponseRecorder()
c := e.NewContext(req, rec)
h := func(c vodka.Context) error {
body, err := ioutil.ReadAll(c.Request().Body())
if err != nil {
return err
}
return c.String(http.StatusOK, string(body))
}
// Based on content length (within limit)
if assert.NoError(t, BodyLimit("2M")(h)(c)) {
assert.Equal(t, http.StatusOK, rec.Status())
assert.Equal(t, hw, rec.Body.Bytes())
}
// Based on content read (overlimit)
he := BodyLimit("2B")(h)(c).(*vodka.HTTPError)
assert.Equal(t, http.StatusRequestEntityTooLarge, he.Code)
// Based on content read (within limit)
req = test.NewRequest(vodka.POST, "/", bytes.NewReader(hw))
rec = test.NewResponseRecorder()
c = e.NewContext(req, rec)
if assert.NoError(t, BodyLimit("2M")(h)(c)) {
assert.Equal(t, http.StatusOK, rec.Status())
assert.Equal(t, "Hello, World!", rec.Body.String())
}
// Based on content read (overlimit)
req = test.NewRequest(vodka.POST, "/", bytes.NewReader(hw))
rec = test.NewResponseRecorder()
c = e.NewContext(req, rec)
he = BodyLimit("2B")(h)(c).(*vodka.HTTPError)
assert.Equal(t, http.StatusRequestEntityTooLarge, he.Code)
}
开发者ID:insionng,项目名称:vodka,代码行数:40,代码来源:body_limit_test.go
示例16: testBinderOkay
func testBinderOkay(t *testing.T, r io.Reader, ctype string) {
e := New()
req := test.NewRequest(POST, "/", r)
rec := test.NewResponseRecorder()
c := e.NewContext(req, rec)
req.Header().Set(HeaderContentType, ctype)
u := new(user)
err := c.Bind(u)
if assert.NoError(t, err) {
assert.Equal(t, 1, u.ID)
assert.Equal(t, "Jon Snow", u.Name)
}
}
开发者ID:insionng,项目名称:vodka,代码行数:13,代码来源:binder_test.go
示例17: TestLogger
func TestLogger(t *testing.T) {
// Note: Just for the test coverage, not a real test.
e := vodka.New()
req := test.NewRequest(vodka.GET, "/", nil)
rec := test.NewResponseRecorder()
c := e.NewContext(req, rec)
h := Logger()(func(c vodka.Context) error {
return c.String(http.StatusOK, "test")
})
// Status 2xx
h(c)
// Status 3xx
rec = test.NewResponseRecorder()
c = e.NewContext(req, rec)
h = Logger()(func(c vodka.Context) error {
return c.String(http.StatusTemporaryRedirect, "test")
})
h(c)
// Status 4xx
rec = test.NewResponseRecorder()
c = e.NewContext(req, rec)
h = Logger()(func(c vodka.Context) error {
return c.String(http.StatusNotFound, "test")
})
h(c)
// Status 5xx with empty path
req = test.NewRequest(vodka.GET, "", nil)
rec = test.NewResponseRecorder()
c = e.NewContext(req, rec)
h = Logger()(func(c vodka.Context) error {
return errors.New("error")
})
h(c)
}
开发者ID:insionng,项目名称:vodka,代码行数:38,代码来源:logger_test.go
示例18: TestCSRFTokenFromForm
func TestCSRFTokenFromForm(t *testing.T) {
f := make(url.Values)
f.Set("csrf", "token")
e := vodka.New()
req := test.NewRequest(vodka.POST, "/", strings.NewReader(f.Encode()))
req.Header().Add(vodka.HeaderContentType, vodka.MIMEApplicationForm)
c := e.NewContext(req, nil)
token, err := csrfTokenFromForm("csrf")(c)
if assert.NoError(t, err) {
assert.Equal(t, "token", token)
}
token, err = csrfTokenFromForm("invalid")(c)
assert.Error(t, err)
}
开发者ID:insionng,项目名称:vodka,代码行数:14,代码来源:csrf_test.go
示例19: TestGzipNoContent
func TestGzipNoContent(t *testing.T) {
e := vodka.New()
req := test.NewRequest(vodka.GET, "/", nil)
rec := test.NewResponseRecorder()
c := e.NewContext(req, rec)
h := Gzip()(func(c vodka.Context) error {
return c.NoContent(http.StatusOK)
})
if assert.NoError(t, h(c)) {
assert.Empty(t, rec.Header().Get(vodka.HeaderContentEncoding))
assert.Empty(t, rec.Header().Get(vodka.HeaderContentType))
assert.Equal(t, 0, len(rec.Body.Bytes()))
}
}
开发者ID:insionng,项目名称:vodka,代码行数:14,代码来源:compress_test.go
示例20: TestRecover
func TestRecover(t *testing.T) {
e := vodka.New()
buf := new(bytes.Buffer)
e.SetLogOutput(buf)
req := test.NewRequest(vodka.GET, "/", nil)
rec := test.NewResponseRecorder()
c := e.NewContext(req, rec)
h := Recover()(vodka.HandlerFunc(func(c vodka.Context) error {
panic("test")
}))
h(c)
assert.Equal(t, http.StatusInternalServerError, rec.Status())
assert.Contains(t, buf.String(), "PANIC RECOVER")
}
开发者ID:insionng,项目名称:vodka,代码行数:14,代码来源:recover_test.go
注:本文中的github.com/insionng/vodka/test.NewRequest函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论