1
0
mirror of https://github.com/labstack/echo.git synced 2024-12-24 20:14:31 +02:00

Fixed test

Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana 2017-02-22 15:57:12 -08:00
parent d259f88324
commit 29fd5831ff
19 changed files with 71 additions and 64 deletions

View File

@ -1,7 +1,7 @@
language: go
go:
- 1.6
- 1.7
- 1.8
- tip
install:
- go get golang.org/x/tools/cmd/cover

View File

@ -102,7 +102,7 @@ func TestBindForm(t *testing.T) {
testBindOkay(t, strings.NewReader(userForm), MIMEApplicationForm)
testBindError(t, nil, MIMEApplicationForm)
e := New()
req, _ := http.NewRequest(POST, "/", strings.NewReader(userForm))
req := httptest.NewRequest(POST, "/", strings.NewReader(userForm))
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
req.Header.Set(HeaderContentType, MIMEApplicationForm)
@ -113,7 +113,7 @@ func TestBindForm(t *testing.T) {
func TestBindQueryParams(t *testing.T) {
e := New()
req, _ := http.NewRequest(GET, "/?id=1&name=Jon Snow", nil)
req := httptest.NewRequest(GET, "/?id=1&name=Jon+Snow", nil)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
u := new(user)
@ -126,7 +126,7 @@ func TestBindQueryParams(t *testing.T) {
func TestBindUnmarshalParam(t *testing.T) {
e := New()
req, _ := http.NewRequest(GET, "/?ts=2016-12-06T19:09:05Z&sa=one,two,three&ta=2016-12-06T19:09:05Z&ta=2016-12-06T19:09:05Z&ST=baz", nil)
req := httptest.NewRequest(GET, "/?ts=2016-12-06T19:09:05Z&sa=one,two,three&ta=2016-12-06T19:09:05Z&ta=2016-12-06T19:09:05Z&ST=baz", nil)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
result := struct {
@ -148,7 +148,7 @@ func TestBindUnmarshalParam(t *testing.T) {
func TestBindUnmarshalParamPtr(t *testing.T) {
e := New()
req, _ := http.NewRequest(GET, "/?ts=2016-12-06T19:09:05Z", nil)
req := httptest.NewRequest(GET, "/?ts=2016-12-06T19:09:05Z", nil)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
result := struct {
@ -270,7 +270,7 @@ func assertBindTestStruct(t *testing.T, ts *bindTestStruct) {
func testBindOkay(t *testing.T, r io.Reader, ctype string) {
e := New()
req, _ := http.NewRequest(POST, "/", r)
req := httptest.NewRequest(POST, "/", r)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
req.Header.Set(HeaderContentType, ctype)
@ -284,7 +284,7 @@ func testBindOkay(t *testing.T, r io.Reader, ctype string) {
func testBindError(t *testing.T, r io.Reader, ctype string) {
e := New()
req, _ := http.NewRequest(POST, "/", r)
req := httptest.NewRequest(POST, "/", r)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
req.Header.Set(HeaderContentType, ctype)

View File

@ -32,7 +32,7 @@ func (t *Template) Render(w io.Writer, name string, data interface{}, c Context)
func TestContext(t *testing.T) {
e := New()
req, _ := http.NewRequest(POST, "/", strings.NewReader(userJSON))
req := httptest.NewRequest(POST, "/", strings.NewReader(userJSON))
rec := httptest.NewRecorder()
c := e.NewContext(req, rec).(*context)
@ -195,7 +195,7 @@ func TestContext(t *testing.T) {
func TestContextCookie(t *testing.T) {
e := New()
req, _ := http.NewRequest(GET, "/", nil)
req := httptest.NewRequest(GET, "/", nil)
theme := "theme=light"
user := "user=Jon Snow"
req.Header.Add(HeaderCookie, theme)
@ -255,7 +255,7 @@ func TestContextPath(t *testing.T) {
func TestContextPathParam(t *testing.T) {
e := New()
req, _ := http.NewRequest(GET, "/", nil)
req := httptest.NewRequest(GET, "/", nil)
c := e.NewContext(req, nil)
// ParamNames
@ -272,7 +272,7 @@ func TestContextPathParam(t *testing.T) {
func TestContextPathParamNamesAlais(t *testing.T) {
e := New()
req, _ := http.NewRequest(GET, "/", nil)
req := httptest.NewRequest(GET, "/", nil)
c := e.NewContext(req, nil)
c.SetParamNames("id,name")
@ -288,7 +288,7 @@ func TestContextFormValue(t *testing.T) {
f.Set("email", "jon@labstack.com")
e := New()
req, _ := http.NewRequest(POST, "/", strings.NewReader(f.Encode()))
req := httptest.NewRequest(POST, "/", strings.NewReader(f.Encode()))
req.Header.Add(HeaderContentType, MIMEApplicationForm)
c := e.NewContext(req, nil)
@ -310,7 +310,7 @@ func TestContextQueryParam(t *testing.T) {
q := make(url.Values)
q.Set("name", "Jon Snow")
q.Set("email", "jon@labstack.com")
req, _ := http.NewRequest(GET, "/?"+q.Encode(), nil)
req := httptest.NewRequest(GET, "/?"+q.Encode(), nil)
e := New()
c := e.NewContext(req, nil)
@ -334,7 +334,7 @@ func TestContextFormFile(t *testing.T) {
w.Write([]byte("test"))
}
mr.Close()
req, _ := http.NewRequest(POST, "/", buf)
req := httptest.NewRequest(POST, "/", buf)
req.Header.Set(HeaderContentType, mr.FormDataContentType())
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
@ -350,7 +350,7 @@ func TestContextMultipartForm(t *testing.T) {
mw := multipart.NewWriter(buf)
mw.WriteField("name", "Jon Snow")
mw.Close()
req, _ := http.NewRequest(POST, "/", buf)
req := httptest.NewRequest(POST, "/", buf)
req.Header.Set(HeaderContentType, mw.FormDataContentType())
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
@ -362,7 +362,7 @@ func TestContextMultipartForm(t *testing.T) {
func TestContextRedirect(t *testing.T) {
e := New()
req, _ := http.NewRequest(GET, "/", nil)
req := httptest.NewRequest(GET, "/", nil)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
assert.Equal(t, nil, c.Redirect(http.StatusMovedPermanently, "http://labstack.github.io/echo"))

View File

@ -404,7 +404,7 @@ func (e *Echo) Static(prefix, root string) {
func static(i i, prefix, root string) {
h := func(c Context) error {
name := filepath.Join(root, path.Clean("/"+c.Param("*"))) // `/` for security
name := filepath.Join(root, path.Clean("/"+c.Param("*"))) // "/"+ for security
return c.File(name)
}
i.GET(prefix, h)

View File

@ -42,7 +42,7 @@ const userXMLPretty = `<user>
func TestEcho(t *testing.T) {
e := New()
req, _ := http.NewRequest(GET, "/", nil)
req := httptest.NewRequest(GET, "/", nil)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
@ -164,7 +164,7 @@ func TestEchoHandler(t *testing.T) {
func TestEchoWrapHandler(t *testing.T) {
e := New()
req, _ := http.NewRequest(GET, "", nil)
req := httptest.NewRequest(GET, "/", nil)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
h := WrapHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
@ -179,7 +179,7 @@ func TestEchoWrapHandler(t *testing.T) {
func TestEchoWrapMiddleware(t *testing.T) {
e := New()
req, _ := http.NewRequest(GET, "", nil)
req := httptest.NewRequest(GET, "/", nil)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
buf := new(bytes.Buffer)
@ -364,7 +364,7 @@ func TestEchoGroup(t *testing.T) {
func TestEchoNotFound(t *testing.T) {
e := New()
req, _ := http.NewRequest(GET, "/files", nil)
req := httptest.NewRequest(GET, "/files", nil)
rec := httptest.NewRecorder()
e.ServeHTTP(rec, req)
assert.Equal(t, http.StatusNotFound, rec.Code)
@ -375,7 +375,7 @@ func TestEchoMethodNotAllowed(t *testing.T) {
e.GET("/", func(c Context) error {
return c.String(http.StatusOK, "Echo!")
})
req, _ := http.NewRequest(POST, "/", nil)
req := httptest.NewRequest(POST, "/", nil)
rec := httptest.NewRecorder()
e.ServeHTTP(rec, req)
assert.Equal(t, http.StatusMethodNotAllowed, rec.Code)
@ -418,7 +418,7 @@ func testMethod(t *testing.T, method, path string, e *Echo) {
}
func request(method, path string, e *Echo) (int, string) {
req, _ := http.NewRequest(method, path, nil)
req := httptest.NewRequest(method, path, nil)
rec := httptest.NewRecorder()
e.ServeHTTP(rec, req)
return rec.Code, rec.Body.String()

View File

@ -12,7 +12,7 @@ import (
func TestBasicAuth(t *testing.T) {
e := echo.New()
req, _ := http.NewRequest(echo.GET, "/", nil)
req := httptest.NewRequest(echo.GET, "/", nil)
res := httptest.NewRecorder()
c := e.NewContext(req, res)
f := func(u, p string, c echo.Context) bool {

View File

@ -14,7 +14,7 @@ import (
func TestBodyLimit(t *testing.T) {
e := echo.New()
hw := []byte("Hello, World!")
req, _ := http.NewRequest(echo.POST, "/", bytes.NewReader(hw))
req := httptest.NewRequest(echo.POST, "/", bytes.NewReader(hw))
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
h := func(c echo.Context) error {

View File

@ -14,7 +14,7 @@ import (
func TestGzip(t *testing.T) {
e := echo.New()
req, _ := http.NewRequest(echo.GET, "/", nil)
req := httptest.NewRequest(echo.GET, "/", nil)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
@ -45,7 +45,7 @@ func TestGzip(t *testing.T) {
func TestGzipNoContent(t *testing.T) {
e := echo.New()
req, _ := http.NewRequest(echo.GET, "/", nil)
req := httptest.NewRequest(echo.GET, "/", nil)
req.Header.Set(echo.HeaderAcceptEncoding, gzipScheme)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
@ -65,7 +65,7 @@ func TestGzipErrorReturned(t *testing.T) {
e.GET("/", func(c echo.Context) error {
return echo.ErrNotFound
})
req, _ := http.NewRequest(echo.GET, "/", nil)
req := httptest.NewRequest(echo.GET, "/", nil)
req.Header.Set(echo.HeaderAcceptEncoding, gzipScheme)
rec := httptest.NewRecorder()
e.ServeHTTP(rec, req)
@ -78,7 +78,7 @@ func TestGzipWithStatic(t *testing.T) {
e := echo.New()
e.Use(Gzip())
e.Static("/test", "../_fixture/images")
req, _ := http.NewRequest(echo.GET, "/test/walle.png", nil)
req := httptest.NewRequest(echo.GET, "/test/walle.png", nil)
req.Header.Set(echo.HeaderAcceptEncoding, gzipScheme)
rec := httptest.NewRecorder()
e.ServeHTTP(rec, req)

View File

@ -13,7 +13,7 @@ func TestCORS(t *testing.T) {
e := echo.New()
// Wildcard origin
req, _ := http.NewRequest(echo.GET, "/", nil)
req := httptest.NewRequest(echo.GET, "/", nil)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
h := CORS()(echo.NotFoundHandler)

View File

@ -14,7 +14,7 @@ import (
func TestCSRF(t *testing.T) {
e := echo.New()
req, _ := http.NewRequest(echo.GET, "/", nil)
req := httptest.NewRequest(echo.GET, "/", nil)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
csrf := CSRFWithConfig(CSRFConfig{
@ -54,7 +54,7 @@ func TestCSRFTokenFromForm(t *testing.T) {
f := make(url.Values)
f.Set("csrf", "token")
e := echo.New()
req, _ := http.NewRequest(echo.POST, "/", strings.NewReader(f.Encode()))
req := httptest.NewRequest(echo.POST, "/", strings.NewReader(f.Encode()))
req.Header.Add(echo.HeaderContentType, echo.MIMEApplicationForm)
c := e.NewContext(req, nil)
token, err := csrfTokenFromForm("csrf")(c)
@ -69,7 +69,7 @@ func TestCSRFTokenFromQuery(t *testing.T) {
q := make(url.Values)
q.Set("csrf", "token")
e := echo.New()
req, _ := http.NewRequest(echo.GET, "/?"+q.Encode(), nil)
req := httptest.NewRequest(echo.GET, "/?"+q.Encode(), nil)
req.Header.Add(echo.HeaderContentType, echo.MIMEApplicationForm)
c := e.NewContext(req, nil)
token, err := csrfTokenFromQuery("csrf")(c)

View File

@ -153,7 +153,7 @@ func TestJWT(t *testing.T) {
tc.reqURL = "/"
}
req, _ := http.NewRequest(echo.GET, tc.reqURL, nil)
req := httptest.NewRequest(echo.GET, tc.reqURL, nil)
res := httptest.NewRecorder()
req.Header.Set(echo.HeaderAuthorization, tc.hdrAuth)
req.Header.Set(echo.HeaderCookie, tc.hdrCookie)

View File

@ -11,7 +11,7 @@ import (
func TestKeyAuth(t *testing.T) {
e := echo.New()
req, _ := http.NewRequest(echo.GET, "/", nil)
req := httptest.NewRequest(echo.GET, "/", nil)
res := httptest.NewRecorder()
c := e.NewContext(req, res)
config := KeyAuthConfig{

View File

@ -16,7 +16,7 @@ import (
func TestLogger(t *testing.T) {
// Note: Just for the test coverage, not a real test.
e := echo.New()
req, _ := http.NewRequest(echo.GET, "/", nil)
req := httptest.NewRequest(echo.GET, "/", nil)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
h := Logger()(func(c echo.Context) error {
@ -54,7 +54,7 @@ func TestLogger(t *testing.T) {
func TestLoggerIPAddress(t *testing.T) {
e := echo.New()
req, _ := http.NewRequest(echo.GET, "/", nil)
req := httptest.NewRequest(echo.GET, "/", nil)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
buf := new(bytes.Buffer)
@ -98,7 +98,7 @@ func TestLoggerTemplate(t *testing.T) {
return c.String(http.StatusOK, "Header Logged")
})
req, _ := http.NewRequest(echo.GET, "/?username=apagano-param&password=secret", nil)
req := httptest.NewRequest(echo.GET, "/?username=apagano-param&password=secret", nil)
req.RequestURI = "/"
req.Header.Add(echo.HeaderXRealIP, "127.0.0.1")
req.Header.Add("Referer", "google.com")

View File

@ -18,7 +18,7 @@ func TestMethodOverride(t *testing.T) {
}
// Override with http header
req, _ := http.NewRequest(echo.POST, "/", nil)
req := httptest.NewRequest(echo.POST, "/", nil)
rec := httptest.NewRecorder()
req.Header.Set(echo.HeaderXHTTPMethodOverride, echo.DELETE)
c := e.NewContext(req, rec)

View File

@ -14,7 +14,7 @@ func TestRecover(t *testing.T) {
e := echo.New()
buf := new(bytes.Buffer)
e.Logger.SetOutput(buf)
req, _ := http.NewRequest(echo.GET, "/", nil)
req := httptest.NewRequest(echo.GET, "/", nil)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
h := Recover()(echo.HandlerFunc(func(c echo.Context) error {

View File

@ -14,12 +14,13 @@ func TestRedirectHTTPSRedirect(t *testing.T) {
next := func(c echo.Context) (err error) {
return c.NoContent(http.StatusOK)
}
req, _ := http.NewRequest(echo.GET, "http://labstack.com", nil)
req := httptest.NewRequest(echo.GET, "/", nil)
req.Host = "labstack.com"
res := httptest.NewRecorder()
c := e.NewContext(req, res)
HTTPSRedirect()(next)(c)
assert.Equal(t, http.StatusMovedPermanently, res.Code)
assert.Equal(t, "https://labstack.com", res.Header().Get(echo.HeaderLocation))
assert.Equal(t, "https://labstack.com/", res.Header().Get(echo.HeaderLocation))
}
func TestRedirectHTTPSWWWRedirect(t *testing.T) {
@ -27,12 +28,13 @@ func TestRedirectHTTPSWWWRedirect(t *testing.T) {
next := func(c echo.Context) (err error) {
return c.NoContent(http.StatusOK)
}
req, _ := http.NewRequest(echo.GET, "http://labstack.com", nil)
req := httptest.NewRequest(echo.GET, "/", nil)
req.Host = "labstack.com"
res := httptest.NewRecorder()
c := e.NewContext(req, res)
HTTPSWWWRedirect()(next)(c)
assert.Equal(t, http.StatusMovedPermanently, res.Code)
assert.Equal(t, "https://www.labstack.com", res.Header().Get(echo.HeaderLocation))
assert.Equal(t, "https://www.labstack.com/", res.Header().Get(echo.HeaderLocation))
}
func TestRedirectHTTPSNonWWWRedirect(t *testing.T) {
@ -40,12 +42,13 @@ func TestRedirectHTTPSNonWWWRedirect(t *testing.T) {
next := func(c echo.Context) (err error) {
return c.NoContent(http.StatusOK)
}
req, _ := http.NewRequest(echo.GET, "http://www.labstack.com", nil)
req := httptest.NewRequest(echo.GET, "/", nil)
req.Host = "www.labstack.com"
res := httptest.NewRecorder()
c := e.NewContext(req, res)
HTTPSNonWWWRedirect()(next)(c)
assert.Equal(t, http.StatusMovedPermanently, res.Code)
assert.Equal(t, "https://labstack.com", res.Header().Get(echo.HeaderLocation))
assert.Equal(t, "https://labstack.com/", res.Header().Get(echo.HeaderLocation))
}
func TestRedirectWWWRedirect(t *testing.T) {
@ -53,12 +56,13 @@ func TestRedirectWWWRedirect(t *testing.T) {
next := func(c echo.Context) (err error) {
return c.NoContent(http.StatusOK)
}
req, _ := http.NewRequest(echo.GET, "http://labstack.com", nil)
req := httptest.NewRequest(echo.GET, "/", nil)
req.Host = "labstack.com"
res := httptest.NewRecorder()
c := e.NewContext(req, res)
WWWRedirect()(next)(c)
assert.Equal(t, http.StatusMovedPermanently, res.Code)
assert.Equal(t, "http://www.labstack.com", res.Header().Get(echo.HeaderLocation))
assert.Equal(t, "http://www.labstack.com/", res.Header().Get(echo.HeaderLocation))
}
func TestRedirectNonWWWRedirect(t *testing.T) {
@ -66,10 +70,11 @@ func TestRedirectNonWWWRedirect(t *testing.T) {
next := func(c echo.Context) (err error) {
return c.NoContent(http.StatusOK)
}
req, _ := http.NewRequest(echo.GET, "http://www.labstack.com", nil)
req := httptest.NewRequest(echo.GET, "/", nil)
req.Host = "www.labstack.com"
res := httptest.NewRecorder()
c := e.NewContext(req, res)
NonWWWRedirect()(next)(c)
assert.Equal(t, http.StatusMovedPermanently, res.Code)
assert.Equal(t, "http://labstack.com", res.Header().Get(echo.HeaderLocation))
assert.Equal(t, "http://labstack.com/", res.Header().Get(echo.HeaderLocation))
}

View File

@ -11,7 +11,7 @@ import (
func TestSecure(t *testing.T) {
e := echo.New()
req, _ := http.NewRequest(echo.GET, "/", nil)
req := httptest.NewRequest(echo.GET, "/", nil)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
h := func(c echo.Context) error {

View File

@ -11,7 +11,7 @@ import (
func TestAddTrailingSlash(t *testing.T) {
e := echo.New()
req, _ := http.NewRequest(echo.GET, "/add-slash", nil)
req := httptest.NewRequest(echo.GET, "/add-slash", nil)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
h := AddTrailingSlash()(func(c echo.Context) error {
@ -37,7 +37,7 @@ func TestAddTrailingSlash(t *testing.T) {
func TestRemoveTrailingSlash(t *testing.T) {
e := echo.New()
req, _ := http.NewRequest(echo.GET, "/remove-slash/", nil)
req := httptest.NewRequest(echo.GET, "/remove-slash/", nil)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
h := RemoveTrailingSlash()(func(c echo.Context) error {

View File

@ -11,11 +11,9 @@ import (
func TestStatic(t *testing.T) {
e := echo.New()
// TODO: Once go1.6 is dropped, use `httptest.Request()`.
req, _ := http.NewRequest(echo.GET, "/", nil)
req := httptest.NewRequest(echo.GET, "/", nil)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
c.SetParamNames("*")
config := StaticConfig{
Root: "../_fixture",
}
@ -27,22 +25,25 @@ func TestStatic(t *testing.T) {
}
// File found
h = StaticWithConfig(config)(echo.NotFoundHandler)
c.SetParamValues("/images/walle.png")
req = httptest.NewRequest(echo.GET, "/images/walle.png", nil)
rec = httptest.NewRecorder()
c = e.NewContext(req, rec)
if assert.NoError(t, h(c)) {
assert.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, rec.Header().Get(echo.HeaderContentLength), "219885")
}
// File not found
c.SetParamValues("/none")
rec.Body.Reset()
req = httptest.NewRequest(echo.GET, "/none", nil)
rec = httptest.NewRecorder()
c = e.NewContext(req, rec)
he := h(c).(*echo.HTTPError)
assert.Equal(t, http.StatusNotFound, he.Code)
// HTML5
c.SetParamValues("/random")
rec.Body.Reset()
req = httptest.NewRequest(echo.GET, "/random", nil)
rec = httptest.NewRecorder()
c = e.NewContext(req, rec)
config.HTML5 = true
static := StaticWithConfig(config)
h = static(echo.NotFoundHandler)
@ -52,8 +53,9 @@ func TestStatic(t *testing.T) {
}
// Browse
c.SetParamValues("/")
rec.Body.Reset()
req = httptest.NewRequest(echo.GET, "/", nil)
rec = httptest.NewRecorder()
c = e.NewContext(req, rec)
config.Browse = true
static = StaticWithConfig(config)
h = static(echo.NotFoundHandler)