Skip to content

Commit

Permalink
Using httptest
Browse files Browse the repository at this point in the history
Signed-off-by: Vishal Rana <[email protected]>
  • Loading branch information
vishr committed May 13, 2017
1 parent 8f5fb63 commit cb9255e
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 16 deletions.
4 changes: 2 additions & 2 deletions middleware/body_limit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestBodyLimit(t *testing.T) {
assert.Equal(t, http.StatusRequestEntityTooLarge, he.Code)

// Based on content read (within limit)
req, _ = http.NewRequest(echo.POST, "/", bytes.NewReader(hw))
req = httptest.NewRequest(echo.POST, "/", bytes.NewReader(hw))
rec = httptest.NewRecorder()
c = e.NewContext(req, rec)
if assert.NoError(t, BodyLimit("2M")(h)(c)) {
Expand All @@ -45,7 +45,7 @@ func TestBodyLimit(t *testing.T) {
}

// Based on content read (overlimit)
req, _ = http.NewRequest(echo.POST, "/", bytes.NewReader(hw))
req = httptest.NewRequest(echo.POST, "/", bytes.NewReader(hw))
rec = httptest.NewRecorder()
c = e.NewContext(req, rec)
he = BodyLimit("2B")(h)(c).(*echo.HTTPError)
Expand Down
2 changes: 1 addition & 1 deletion middleware/compress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestGzip(t *testing.T) {
assert.Equal(t, "test", rec.Body.String())

// Gzip
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)
Expand Down
5 changes: 2 additions & 3 deletions middleware/cors_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package middleware

import (
"net/http"
"net/http/httptest"
"testing"

Expand All @@ -21,7 +20,7 @@ func TestCORS(t *testing.T) {
assert.Equal(t, "*", rec.Header().Get(echo.HeaderAccessControlAllowOrigin))

// Allow origins
req, _ = http.NewRequest(echo.GET, "/", nil)
req = httptest.NewRequest(echo.GET, "/", nil)
rec = httptest.NewRecorder()
c = e.NewContext(req, rec)
h = CORSWithConfig(CORSConfig{
Expand All @@ -32,7 +31,7 @@ func TestCORS(t *testing.T) {
assert.Equal(t, "localhost", rec.Header().Get(echo.HeaderAccessControlAllowOrigin))

// Preflight request
req, _ = http.NewRequest(echo.OPTIONS, "/", nil)
req = httptest.NewRequest(echo.OPTIONS, "/", nil)
rec = httptest.NewRecorder()
c = e.NewContext(req, rec)
req.Header.Set(echo.HeaderOrigin, "localhost")
Expand Down
4 changes: 2 additions & 2 deletions middleware/csrf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ func TestCSRF(t *testing.T) {
assert.Contains(t, rec.Header().Get(echo.HeaderSetCookie), "_csrf")

// Without CSRF cookie
req, _ = http.NewRequest(echo.POST, "/", nil)
req = httptest.NewRequest(echo.POST, "/", nil)
rec = httptest.NewRecorder()
c = e.NewContext(req, rec)
assert.Error(t, h(c))

// Empty/invalid CSRF token
req, _ = http.NewRequest(echo.POST, "/", nil)
req = httptest.NewRequest(echo.POST, "/", nil)
rec = httptest.NewRecorder()
c = e.NewContext(req, rec)
req.Header.Set(echo.HeaderXCSRFToken, "")
Expand Down
2 changes: 1 addition & 1 deletion middleware/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestLogger(t *testing.T) {
h(c)

// Status 5xx with empty path
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 {
Expand Down
6 changes: 3 additions & 3 deletions middleware/method_override_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestMethodOverride(t *testing.T) {

// Override with form parameter
m = MethodOverrideWithConfig(MethodOverrideConfig{Getter: MethodFromForm("_method")})
req, _ = http.NewRequest(echo.POST, "/", bytes.NewReader([]byte("_method="+echo.DELETE)))
req = httptest.NewRequest(echo.POST, "/", bytes.NewReader([]byte("_method="+echo.DELETE)))
rec = httptest.NewRecorder()
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationForm)
c = e.NewContext(req, rec)
Expand All @@ -36,14 +36,14 @@ func TestMethodOverride(t *testing.T) {

// Override with query parameter
m = MethodOverrideWithConfig(MethodOverrideConfig{Getter: MethodFromQuery("_method")})
req, _ = http.NewRequest(echo.POST, "/?_method="+echo.DELETE, nil)
req = httptest.NewRequest(echo.POST, "/?_method="+echo.DELETE, nil)
rec = httptest.NewRecorder()
c = e.NewContext(req, rec)
m(h)(c)
assert.Equal(t, echo.DELETE, req.Method)

// Ignore `GET`
req, _ = http.NewRequest(echo.GET, "/", nil)
req = httptest.NewRequest(echo.GET, "/", nil)
req.Header.Set(echo.HeaderXHTTPMethodOverride, echo.DELETE)
assert.Equal(t, echo.GET, req.Method)
}
2 changes: 1 addition & 1 deletion middleware/request_id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

func TestRequestID(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)
handler := func(c echo.Context) error {
Expand Down
6 changes: 3 additions & 3 deletions middleware/slash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestAddTrailingSlash(t *testing.T) {
assert.Equal(t, "/add-slash/", req.RequestURI)

// With config
req, _ = http.NewRequest(echo.GET, "/add-slash?key=value", nil)
req = httptest.NewRequest(echo.GET, "/add-slash?key=value", nil)
rec = httptest.NewRecorder()
c = e.NewContext(req, rec)
h = AddTrailingSlashWithConfig(TrailingSlashConfig{
Expand All @@ -48,7 +48,7 @@ func TestRemoveTrailingSlash(t *testing.T) {
assert.Equal(t, "/remove-slash", req.RequestURI)

// With config
req, _ = http.NewRequest(echo.GET, "/remove-slash/?key=value", nil)
req = httptest.NewRequest(echo.GET, "/remove-slash/?key=value", nil)
rec = httptest.NewRecorder()
c = e.NewContext(req, rec)
h = RemoveTrailingSlashWithConfig(TrailingSlashConfig{
Expand All @@ -61,7 +61,7 @@ func TestRemoveTrailingSlash(t *testing.T) {
assert.Equal(t, "/remove-slash?key=value", rec.Header().Get(echo.HeaderLocation))

// With bare URL
req, _ = http.NewRequest(echo.GET, "http://localhost", nil)
req = httptest.NewRequest(echo.GET, "http://localhost", nil)
rec = httptest.NewRecorder()
c = e.NewContext(req, rec)
h = RemoveTrailingSlash()(func(c echo.Context) error {
Expand Down

0 comments on commit cb9255e

Please sign in to comment.