1
0
mirror of https://github.com/labstack/echo.git synced 2025-01-12 01:22:21 +02:00

simplify tests (#1206)

This commit is contained in:
Emir Ribić 2018-10-14 09:18:44 +02:00 committed by Vishal Rana
parent ac1f40118a
commit 059c099762
11 changed files with 257 additions and 214 deletions

View File

@ -120,32 +120,37 @@ var values = map[string][]string{
} }
func TestBindJSON(t *testing.T) { func TestBindJSON(t *testing.T) {
testBindOkay(t, strings.NewReader(userJSON), MIMEApplicationJSON) assert := assert.New(t)
testBindError(t, strings.NewReader(invalidContent), MIMEApplicationJSON, &json.SyntaxError{}) testBindOkay(assert, strings.NewReader(userJSON), MIMEApplicationJSON)
testBindError(t, strings.NewReader(userJSONInvalidType), MIMEApplicationJSON, &json.UnmarshalTypeError{}) testBindError(assert, strings.NewReader(invalidContent), MIMEApplicationJSON, &json.SyntaxError{})
testBindError(assert, strings.NewReader(userJSONInvalidType), MIMEApplicationJSON, &json.UnmarshalTypeError{})
} }
func TestBindXML(t *testing.T) { func TestBindXML(t *testing.T) {
testBindOkay(t, strings.NewReader(userXML), MIMEApplicationXML) assert := assert.New(t)
testBindError(t, strings.NewReader(invalidContent), MIMEApplicationXML, errors.New(""))
testBindError(t, strings.NewReader(userXMLConvertNumberError), MIMEApplicationXML, &strconv.NumError{}) testBindOkay(assert, strings.NewReader(userXML), MIMEApplicationXML)
testBindError(t, strings.NewReader(userXMLUnsupportedTypeError), MIMEApplicationXML, &xml.SyntaxError{}) testBindError(assert, strings.NewReader(invalidContent), MIMEApplicationXML, errors.New(""))
testBindOkay(t, strings.NewReader(userXML), MIMETextXML) testBindError(assert, strings.NewReader(userXMLConvertNumberError), MIMEApplicationXML, &strconv.NumError{})
testBindError(t, strings.NewReader(invalidContent), MIMETextXML, errors.New("")) testBindError(assert, strings.NewReader(userXMLUnsupportedTypeError), MIMEApplicationXML, &xml.SyntaxError{})
testBindError(t, strings.NewReader(userXMLConvertNumberError), MIMETextXML, &strconv.NumError{}) testBindOkay(assert, strings.NewReader(userXML), MIMETextXML)
testBindError(t, strings.NewReader(userXMLUnsupportedTypeError), MIMETextXML, &xml.SyntaxError{}) testBindError(assert, strings.NewReader(invalidContent), MIMETextXML, errors.New(""))
testBindError(assert, strings.NewReader(userXMLConvertNumberError), MIMETextXML, &strconv.NumError{})
testBindError(assert, strings.NewReader(userXMLUnsupportedTypeError), MIMETextXML, &xml.SyntaxError{})
} }
func TestBindForm(t *testing.T) { func TestBindForm(t *testing.T) {
testBindOkay(t, strings.NewReader(userForm), MIMEApplicationForm) assert := assert.New(t)
testBindError(t, nil, MIMEApplicationForm, nil)
testBindOkay(assert, strings.NewReader(userForm), MIMEApplicationForm)
testBindError(assert, nil, MIMEApplicationForm, nil)
e := New() e := New()
req := httptest.NewRequest(POST, "/", strings.NewReader(userForm)) req := httptest.NewRequest(POST, "/", strings.NewReader(userForm))
rec := httptest.NewRecorder() rec := httptest.NewRecorder()
c := e.NewContext(req, rec) c := e.NewContext(req, rec)
req.Header.Set(HeaderContentType, MIMEApplicationForm) req.Header.Set(HeaderContentType, MIMEApplicationForm)
err := c.Bind(&[]struct{ Field string }{}) err := c.Bind(&[]struct{ Field string }{})
assert.Error(t, err) assert.Error(err)
} }
func TestBindQueryParams(t *testing.T) { func TestBindQueryParams(t *testing.T) {
@ -200,12 +205,14 @@ func TestBindUnmarshalParam(t *testing.T) {
}{} }{}
err := c.Bind(&result) err := c.Bind(&result)
ts := Timestamp(time.Date(2016, 12, 6, 19, 9, 5, 0, time.UTC)) ts := Timestamp(time.Date(2016, 12, 6, 19, 9, 5, 0, time.UTC))
if assert.NoError(t, err) {
// assert.Equal(t, Timestamp(reflect.TypeOf(&Timestamp{}), time.Date(2016, 12, 6, 19, 9, 5, 0, time.UTC)), result.T) assert := assert.New(t)
assert.Equal(t, ts, result.T) if assert.NoError(err) {
assert.Equal(t, StringArray([]string{"one", "two", "three"}), result.SA) // assert.Equal( Timestamp(reflect.TypeOf(&Timestamp{}), time.Date(2016, 12, 6, 19, 9, 5, 0, time.UTC)), result.T)
assert.Equal(t, []Timestamp{ts, ts}, result.TA) assert.Equal(ts, result.T)
assert.Equal(t, Struct{"baz"}, result.ST) assert.Equal(StringArray([]string{"one", "two", "three"}), result.SA)
assert.Equal([]Timestamp{ts, ts}, result.TA)
assert.Equal(Struct{"baz"}, result.ST)
} }
} }
@ -229,18 +236,22 @@ func TestBindMultipartForm(t *testing.T) {
mw.WriteField("id", "1") mw.WriteField("id", "1")
mw.WriteField("name", "Jon Snow") mw.WriteField("name", "Jon Snow")
mw.Close() mw.Close()
testBindOkay(t, body, mw.FormDataContentType())
assert := assert.New(t)
testBindOkay(assert, body, mw.FormDataContentType())
} }
func TestBindUnsupportedMediaType(t *testing.T) { func TestBindUnsupportedMediaType(t *testing.T) {
testBindError(t, strings.NewReader(invalidContent), MIMEApplicationJSON, &json.SyntaxError{}) assert := assert.New(t)
testBindError(assert, strings.NewReader(invalidContent), MIMEApplicationJSON, &json.SyntaxError{})
} }
func TestBindbindData(t *testing.T) { func TestBindbindData(t *testing.T) {
assert := assert.New(t)
ts := new(bindTestStruct) ts := new(bindTestStruct)
b := new(DefaultBinder) b := new(DefaultBinder)
b.bindData(ts, values, "form") b.bindData(ts, values, "form")
assertBindTestStruct(t, ts) assertBindTestStruct(assert, ts)
} }
func TestBindUnmarshalTypeError(t *testing.T) { func TestBindUnmarshalTypeError(t *testing.T) {
@ -261,6 +272,7 @@ func TestBindUnmarshalTypeError(t *testing.T) {
} }
func TestBindSetWithProperType(t *testing.T) { func TestBindSetWithProperType(t *testing.T) {
assert := assert.New(t)
ts := new(bindTestStruct) ts := new(bindTestStruct)
typ := reflect.TypeOf(ts).Elem() typ := reflect.TypeOf(ts).Elem()
val := reflect.ValueOf(ts).Elem() val := reflect.ValueOf(ts).Elem()
@ -275,9 +287,9 @@ func TestBindSetWithProperType(t *testing.T) {
} }
val := values[typeField.Name][0] val := values[typeField.Name][0]
err := setWithProperType(typeField.Type.Kind(), val, structField) err := setWithProperType(typeField.Type.Kind(), val, structField)
assert.NoError(t, err) assert.NoError(err)
} }
assertBindTestStruct(t, ts) assertBindTestStruct(assert, ts)
type foo struct { type foo struct {
Bar bytes.Buffer Bar bytes.Buffer
@ -285,70 +297,72 @@ func TestBindSetWithProperType(t *testing.T) {
v := &foo{} v := &foo{}
typ = reflect.TypeOf(v).Elem() typ = reflect.TypeOf(v).Elem()
val = reflect.ValueOf(v).Elem() val = reflect.ValueOf(v).Elem()
assert.Error(t, setWithProperType(typ.Field(0).Type.Kind(), "5", val.Field(0))) assert.Error(setWithProperType(typ.Field(0).Type.Kind(), "5", val.Field(0)))
} }
func TestBindSetFields(t *testing.T) { func TestBindSetFields(t *testing.T) {
assert := assert.New(t)
ts := new(bindTestStruct) ts := new(bindTestStruct)
val := reflect.ValueOf(ts).Elem() val := reflect.ValueOf(ts).Elem()
// Int // Int
if assert.NoError(t, setIntField("5", 0, val.FieldByName("I"))) { if assert.NoError(setIntField("5", 0, val.FieldByName("I"))) {
assert.Equal(t, 5, ts.I) assert.Equal(5, ts.I)
} }
if assert.NoError(t, setIntField("", 0, val.FieldByName("I"))) { if assert.NoError(setIntField("", 0, val.FieldByName("I"))) {
assert.Equal(t, 0, ts.I) assert.Equal(0, ts.I)
} }
// Uint // Uint
if assert.NoError(t, setUintField("10", 0, val.FieldByName("UI"))) { if assert.NoError(setUintField("10", 0, val.FieldByName("UI"))) {
assert.Equal(t, uint(10), ts.UI) assert.Equal(uint(10), ts.UI)
} }
if assert.NoError(t, setUintField("", 0, val.FieldByName("UI"))) { if assert.NoError(setUintField("", 0, val.FieldByName("UI"))) {
assert.Equal(t, uint(0), ts.UI) assert.Equal(uint(0), ts.UI)
} }
// Float // Float
if assert.NoError(t, setFloatField("15.5", 0, val.FieldByName("F32"))) { if assert.NoError(setFloatField("15.5", 0, val.FieldByName("F32"))) {
assert.Equal(t, float32(15.5), ts.F32) assert.Equal(float32(15.5), ts.F32)
} }
if assert.NoError(t, setFloatField("", 0, val.FieldByName("F32"))) { if assert.NoError(setFloatField("", 0, val.FieldByName("F32"))) {
assert.Equal(t, float32(0.0), ts.F32) assert.Equal(float32(0.0), ts.F32)
} }
// Bool // Bool
if assert.NoError(t, setBoolField("true", val.FieldByName("B"))) { if assert.NoError(setBoolField("true", val.FieldByName("B"))) {
assert.Equal(t, true, ts.B) assert.Equal(true, ts.B)
} }
if assert.NoError(t, setBoolField("", val.FieldByName("B"))) { if assert.NoError(setBoolField("", val.FieldByName("B"))) {
assert.Equal(t, false, ts.B) assert.Equal(false, ts.B)
} }
ok, err := unmarshalFieldNonPtr("2016-12-06T19:09:05Z", val.FieldByName("T")) ok, err := unmarshalFieldNonPtr("2016-12-06T19:09:05Z", val.FieldByName("T"))
if assert.NoError(t, err) { if assert.NoError(err) {
assert.Equal(t, ok, true) assert.Equal(ok, true)
assert.Equal(t, Timestamp(time.Date(2016, 12, 6, 19, 9, 5, 0, time.UTC)), ts.T) assert.Equal(Timestamp(time.Date(2016, 12, 6, 19, 9, 5, 0, time.UTC)), ts.T)
} }
} }
func assertBindTestStruct(t *testing.T, ts *bindTestStruct) { func assertBindTestStruct(a *assert.Assertions, ts *bindTestStruct) {
assert.Equal(t, 0, ts.I) a.Equal(0, ts.I)
assert.Equal(t, int8(8), ts.I8) a.Equal(int8(8), ts.I8)
assert.Equal(t, int16(16), ts.I16) a.Equal(int16(16), ts.I16)
assert.Equal(t, int32(32), ts.I32) a.Equal(int32(32), ts.I32)
assert.Equal(t, int64(64), ts.I64) a.Equal(int64(64), ts.I64)
assert.Equal(t, uint(0), ts.UI) a.Equal(uint(0), ts.UI)
assert.Equal(t, uint8(8), ts.UI8) a.Equal(uint8(8), ts.UI8)
assert.Equal(t, uint16(16), ts.UI16) a.Equal(uint16(16), ts.UI16)
assert.Equal(t, uint32(32), ts.UI32) a.Equal(uint32(32), ts.UI32)
assert.Equal(t, uint64(64), ts.UI64) a.Equal(uint64(64), ts.UI64)
assert.Equal(t, true, ts.B) a.Equal(true, ts.B)
assert.Equal(t, float32(32.5), ts.F32) a.Equal(float32(32.5), ts.F32)
assert.Equal(t, float64(64.5), ts.F64) a.Equal(float64(64.5), ts.F64)
assert.Equal(t, "test", ts.S) a.Equal("test", ts.S)
assert.Equal(t, "", ts.GetCantSet()) a.Equal("", ts.GetCantSet())
} }
func testBindOkay(t *testing.T, r io.Reader, ctype string) { func testBindOkay(assert *assert.Assertions, r io.Reader, ctype string) {
e := New() e := New()
req := httptest.NewRequest(POST, "/", r) req := httptest.NewRequest(POST, "/", r)
rec := httptest.NewRecorder() rec := httptest.NewRecorder()
@ -356,13 +370,13 @@ func testBindOkay(t *testing.T, r io.Reader, ctype string) {
req.Header.Set(HeaderContentType, ctype) req.Header.Set(HeaderContentType, ctype)
u := new(user) u := new(user)
err := c.Bind(u) err := c.Bind(u)
if assert.NoError(t, err) { if assert.NoError(err) {
assert.Equal(t, 1, u.ID) assert.Equal(1, u.ID)
assert.Equal(t, "Jon Snow", u.Name) assert.Equal("Jon Snow", u.Name)
} }
} }
func testBindError(t *testing.T, r io.Reader, ctype string, expectedInternal error) { func testBindError(assert *assert.Assertions, r io.Reader, ctype string, expectedInternal error) {
e := New() e := New()
req := httptest.NewRequest(POST, "/", r) req := httptest.NewRequest(POST, "/", r)
rec := httptest.NewRecorder() rec := httptest.NewRecorder()
@ -374,14 +388,14 @@ func testBindError(t *testing.T, r io.Reader, ctype string, expectedInternal err
switch { switch {
case strings.HasPrefix(ctype, MIMEApplicationJSON), strings.HasPrefix(ctype, MIMEApplicationXML), strings.HasPrefix(ctype, MIMETextXML), case strings.HasPrefix(ctype, MIMEApplicationJSON), strings.HasPrefix(ctype, MIMEApplicationXML), strings.HasPrefix(ctype, MIMETextXML),
strings.HasPrefix(ctype, MIMEApplicationForm), strings.HasPrefix(ctype, MIMEMultipartForm): strings.HasPrefix(ctype, MIMEApplicationForm), strings.HasPrefix(ctype, MIMEMultipartForm):
if assert.IsType(t, new(HTTPError), err) { if assert.IsType(new(HTTPError), err) {
assert.Equal(t, http.StatusBadRequest, err.(*HTTPError).Code) assert.Equal(http.StatusBadRequest, err.(*HTTPError).Code)
assert.IsType(t, expectedInternal, err.(*HTTPError).Internal) assert.IsType(expectedInternal, err.(*HTTPError).Internal)
} }
default: default:
if assert.IsType(t, new(HTTPError), err) { if assert.IsType(new(HTTPError), err) {
assert.Equal(t, ErrUnsupportedMediaType, err) assert.Equal(ErrUnsupportedMediaType, err)
assert.IsType(t, expectedInternal, err.(*HTTPError).Internal) assert.IsType(expectedInternal, err.(*HTTPError).Internal)
} }
} }
} }

View File

@ -33,14 +33,16 @@ func TestContext(t *testing.T) {
rec := httptest.NewRecorder() rec := httptest.NewRecorder()
c := e.NewContext(req, rec).(*context) c := e.NewContext(req, rec).(*context)
assert := assert.New(t)
// Echo // Echo
assert.Equal(t, e, c.Echo()) assert.Equal(e, c.Echo())
// Request // Request
assert.NotNil(t, c.Request()) assert.NotNil(c.Request())
// Response // Response
assert.NotNil(t, c.Response()) assert.NotNil(c.Response())
//-------- //--------
// Render // Render
@ -51,23 +53,23 @@ func TestContext(t *testing.T) {
} }
c.echo.Renderer = tmpl c.echo.Renderer = tmpl
err := c.Render(http.StatusOK, "hello", "Jon Snow") err := c.Render(http.StatusOK, "hello", "Jon Snow")
if assert.NoError(t, err) { if assert.NoError(err) {
assert.Equal(t, http.StatusOK, rec.Code) assert.Equal(http.StatusOK, rec.Code)
assert.Equal(t, "Hello, Jon Snow!", rec.Body.String()) assert.Equal("Hello, Jon Snow!", rec.Body.String())
} }
c.echo.Renderer = nil c.echo.Renderer = nil
err = c.Render(http.StatusOK, "hello", "Jon Snow") err = c.Render(http.StatusOK, "hello", "Jon Snow")
assert.Error(t, err) assert.Error(err)
// JSON // JSON
rec = httptest.NewRecorder() rec = httptest.NewRecorder()
c = e.NewContext(req, rec).(*context) c = e.NewContext(req, rec).(*context)
err = c.JSON(http.StatusOK, user{1, "Jon Snow"}) err = c.JSON(http.StatusOK, user{1, "Jon Snow"})
if assert.NoError(t, err) { if assert.NoError(err) {
assert.Equal(t, http.StatusOK, rec.Code) assert.Equal(http.StatusOK, rec.Code)
assert.Equal(t, MIMEApplicationJSONCharsetUTF8, rec.Header().Get(HeaderContentType)) assert.Equal(MIMEApplicationJSONCharsetUTF8, rec.Header().Get(HeaderContentType))
assert.Equal(t, userJSON, rec.Body.String()) assert.Equal(userJSON, rec.Body.String())
} }
// JSON with "?pretty" // JSON with "?pretty"
@ -75,10 +77,10 @@ func TestContext(t *testing.T) {
rec = httptest.NewRecorder() rec = httptest.NewRecorder()
c = e.NewContext(req, rec).(*context) c = e.NewContext(req, rec).(*context)
err = c.JSON(http.StatusOK, user{1, "Jon Snow"}) err = c.JSON(http.StatusOK, user{1, "Jon Snow"})
if assert.NoError(t, err) { if assert.NoError(err) {
assert.Equal(t, http.StatusOK, rec.Code) assert.Equal(http.StatusOK, rec.Code)
assert.Equal(t, MIMEApplicationJSONCharsetUTF8, rec.Header().Get(HeaderContentType)) assert.Equal(MIMEApplicationJSONCharsetUTF8, rec.Header().Get(HeaderContentType))
assert.Equal(t, userJSONPretty, rec.Body.String()) assert.Equal(userJSONPretty, rec.Body.String())
} }
req = httptest.NewRequest(GET, "/", nil) // reset req = httptest.NewRequest(GET, "/", nil) // reset
@ -86,37 +88,37 @@ func TestContext(t *testing.T) {
rec = httptest.NewRecorder() rec = httptest.NewRecorder()
c = e.NewContext(req, rec).(*context) c = e.NewContext(req, rec).(*context)
err = c.JSONPretty(http.StatusOK, user{1, "Jon Snow"}, " ") err = c.JSONPretty(http.StatusOK, user{1, "Jon Snow"}, " ")
if assert.NoError(t, err) { if assert.NoError(err) {
assert.Equal(t, http.StatusOK, rec.Code) assert.Equal(http.StatusOK, rec.Code)
assert.Equal(t, MIMEApplicationJSONCharsetUTF8, rec.Header().Get(HeaderContentType)) assert.Equal(MIMEApplicationJSONCharsetUTF8, rec.Header().Get(HeaderContentType))
assert.Equal(t, userJSONPretty, rec.Body.String()) assert.Equal(userJSONPretty, rec.Body.String())
} }
// JSON (error) // JSON (error)
rec = httptest.NewRecorder() rec = httptest.NewRecorder()
c = e.NewContext(req, rec).(*context) c = e.NewContext(req, rec).(*context)
err = c.JSON(http.StatusOK, make(chan bool)) err = c.JSON(http.StatusOK, make(chan bool))
assert.Error(t, err) assert.Error(err)
// JSONP // JSONP
rec = httptest.NewRecorder() rec = httptest.NewRecorder()
c = e.NewContext(req, rec).(*context) c = e.NewContext(req, rec).(*context)
callback := "callback" callback := "callback"
err = c.JSONP(http.StatusOK, callback, user{1, "Jon Snow"}) err = c.JSONP(http.StatusOK, callback, user{1, "Jon Snow"})
if assert.NoError(t, err) { if assert.NoError(err) {
assert.Equal(t, http.StatusOK, rec.Code) assert.Equal(http.StatusOK, rec.Code)
assert.Equal(t, MIMEApplicationJavaScriptCharsetUTF8, rec.Header().Get(HeaderContentType)) assert.Equal(MIMEApplicationJavaScriptCharsetUTF8, rec.Header().Get(HeaderContentType))
assert.Equal(t, callback+"("+userJSON+");", rec.Body.String()) assert.Equal(callback+"("+userJSON+");", rec.Body.String())
} }
// XML // XML
rec = httptest.NewRecorder() rec = httptest.NewRecorder()
c = e.NewContext(req, rec).(*context) c = e.NewContext(req, rec).(*context)
err = c.XML(http.StatusOK, user{1, "Jon Snow"}) err = c.XML(http.StatusOK, user{1, "Jon Snow"})
if assert.NoError(t, err) { if assert.NoError(err) {
assert.Equal(t, http.StatusOK, rec.Code) assert.Equal(http.StatusOK, rec.Code)
assert.Equal(t, MIMEApplicationXMLCharsetUTF8, rec.Header().Get(HeaderContentType)) assert.Equal(MIMEApplicationXMLCharsetUTF8, rec.Header().Get(HeaderContentType))
assert.Equal(t, xml.Header+userXML, rec.Body.String()) assert.Equal(xml.Header+userXML, rec.Body.String())
} }
// XML with "?pretty" // XML with "?pretty"
@ -124,10 +126,10 @@ func TestContext(t *testing.T) {
rec = httptest.NewRecorder() rec = httptest.NewRecorder()
c = e.NewContext(req, rec).(*context) c = e.NewContext(req, rec).(*context)
err = c.XML(http.StatusOK, user{1, "Jon Snow"}) err = c.XML(http.StatusOK, user{1, "Jon Snow"})
if assert.NoError(t, err) { if assert.NoError(err) {
assert.Equal(t, http.StatusOK, rec.Code) assert.Equal(http.StatusOK, rec.Code)
assert.Equal(t, MIMEApplicationXMLCharsetUTF8, rec.Header().Get(HeaderContentType)) assert.Equal(MIMEApplicationXMLCharsetUTF8, rec.Header().Get(HeaderContentType))
assert.Equal(t, xml.Header+userXMLPretty, rec.Body.String()) assert.Equal(xml.Header+userXMLPretty, rec.Body.String())
} }
req = httptest.NewRequest(GET, "/", nil) req = httptest.NewRequest(GET, "/", nil)
@ -135,36 +137,36 @@ func TestContext(t *testing.T) {
rec = httptest.NewRecorder() rec = httptest.NewRecorder()
c = e.NewContext(req, rec).(*context) c = e.NewContext(req, rec).(*context)
err = c.XML(http.StatusOK, make(chan bool)) err = c.XML(http.StatusOK, make(chan bool))
assert.Error(t, err) assert.Error(err)
// XMLPretty // XMLPretty
rec = httptest.NewRecorder() rec = httptest.NewRecorder()
c = e.NewContext(req, rec).(*context) c = e.NewContext(req, rec).(*context)
err = c.XMLPretty(http.StatusOK, user{1, "Jon Snow"}, " ") err = c.XMLPretty(http.StatusOK, user{1, "Jon Snow"}, " ")
if assert.NoError(t, err) { if assert.NoError(err) {
assert.Equal(t, http.StatusOK, rec.Code) assert.Equal(http.StatusOK, rec.Code)
assert.Equal(t, MIMEApplicationXMLCharsetUTF8, rec.Header().Get(HeaderContentType)) assert.Equal(MIMEApplicationXMLCharsetUTF8, rec.Header().Get(HeaderContentType))
assert.Equal(t, xml.Header+userXMLPretty, rec.Body.String()) assert.Equal(xml.Header+userXMLPretty, rec.Body.String())
} }
// String // String
rec = httptest.NewRecorder() rec = httptest.NewRecorder()
c = e.NewContext(req, rec).(*context) c = e.NewContext(req, rec).(*context)
err = c.String(http.StatusOK, "Hello, World!") err = c.String(http.StatusOK, "Hello, World!")
if assert.NoError(t, err) { if assert.NoError(err) {
assert.Equal(t, http.StatusOK, rec.Code) assert.Equal(http.StatusOK, rec.Code)
assert.Equal(t, MIMETextPlainCharsetUTF8, rec.Header().Get(HeaderContentType)) assert.Equal(MIMETextPlainCharsetUTF8, rec.Header().Get(HeaderContentType))
assert.Equal(t, "Hello, World!", rec.Body.String()) assert.Equal("Hello, World!", rec.Body.String())
} }
// HTML // HTML
rec = httptest.NewRecorder() rec = httptest.NewRecorder()
c = e.NewContext(req, rec).(*context) c = e.NewContext(req, rec).(*context)
err = c.HTML(http.StatusOK, "Hello, <strong>World!</strong>") err = c.HTML(http.StatusOK, "Hello, <strong>World!</strong>")
if assert.NoError(t, err) { if assert.NoError(err) {
assert.Equal(t, http.StatusOK, rec.Code) assert.Equal(http.StatusOK, rec.Code)
assert.Equal(t, MIMETextHTMLCharsetUTF8, rec.Header().Get(HeaderContentType)) assert.Equal(MIMETextHTMLCharsetUTF8, rec.Header().Get(HeaderContentType))
assert.Equal(t, "Hello, <strong>World!</strong>", rec.Body.String()) assert.Equal("Hello, <strong>World!</strong>", rec.Body.String())
} }
// Stream // Stream
@ -172,43 +174,43 @@ func TestContext(t *testing.T) {
c = e.NewContext(req, rec).(*context) c = e.NewContext(req, rec).(*context)
r := strings.NewReader("response from a stream") r := strings.NewReader("response from a stream")
err = c.Stream(http.StatusOK, "application/octet-stream", r) err = c.Stream(http.StatusOK, "application/octet-stream", r)
if assert.NoError(t, err) { if assert.NoError(err) {
assert.Equal(t, http.StatusOK, rec.Code) assert.Equal(http.StatusOK, rec.Code)
assert.Equal(t, "application/octet-stream", rec.Header().Get(HeaderContentType)) assert.Equal("application/octet-stream", rec.Header().Get(HeaderContentType))
assert.Equal(t, "response from a stream", rec.Body.String()) assert.Equal("response from a stream", rec.Body.String())
} }
// Attachment // Attachment
rec = httptest.NewRecorder() rec = httptest.NewRecorder()
c = e.NewContext(req, rec).(*context) c = e.NewContext(req, rec).(*context)
err = c.Attachment("_fixture/images/walle.png", "walle.png") err = c.Attachment("_fixture/images/walle.png", "walle.png")
if assert.NoError(t, err) { if assert.NoError(err) {
assert.Equal(t, http.StatusOK, rec.Code) assert.Equal(http.StatusOK, rec.Code)
assert.Equal(t, "attachment; filename=\"walle.png\"", rec.Header().Get(HeaderContentDisposition)) assert.Equal("attachment; filename=\"walle.png\"", rec.Header().Get(HeaderContentDisposition))
assert.Equal(t, 219885, rec.Body.Len()) assert.Equal(219885, rec.Body.Len())
} }
// Inline // Inline
rec = httptest.NewRecorder() rec = httptest.NewRecorder()
c = e.NewContext(req, rec).(*context) c = e.NewContext(req, rec).(*context)
err = c.Inline("_fixture/images/walle.png", "walle.png") err = c.Inline("_fixture/images/walle.png", "walle.png")
if assert.NoError(t, err) { if assert.NoError(err) {
assert.Equal(t, http.StatusOK, rec.Code) assert.Equal(http.StatusOK, rec.Code)
assert.Equal(t, "inline; filename=\"walle.png\"", rec.Header().Get(HeaderContentDisposition)) assert.Equal("inline; filename=\"walle.png\"", rec.Header().Get(HeaderContentDisposition))
assert.Equal(t, 219885, rec.Body.Len()) assert.Equal(219885, rec.Body.Len())
} }
// NoContent // NoContent
rec = httptest.NewRecorder() rec = httptest.NewRecorder()
c = e.NewContext(req, rec).(*context) c = e.NewContext(req, rec).(*context)
c.NoContent(http.StatusOK) c.NoContent(http.StatusOK)
assert.Equal(t, http.StatusOK, rec.Code) assert.Equal(http.StatusOK, rec.Code)
// Error // Error
rec = httptest.NewRecorder() rec = httptest.NewRecorder()
c = e.NewContext(req, rec).(*context) c = e.NewContext(req, rec).(*context)
c.Error(errors.New("error")) c.Error(errors.New("error"))
assert.Equal(t, http.StatusInternalServerError, rec.Code) assert.Equal(http.StatusInternalServerError, rec.Code)
// Reset // Reset
c.SetParamNames("foo") c.SetParamNames("foo")
@ -216,11 +218,11 @@ func TestContext(t *testing.T) {
c.Set("foe", "ban") c.Set("foe", "ban")
c.query = url.Values(map[string][]string{"fon": {"baz"}}) c.query = url.Values(map[string][]string{"fon": {"baz"}})
c.Reset(req, httptest.NewRecorder()) c.Reset(req, httptest.NewRecorder())
assert.Equal(t, 0, len(c.ParamValues())) assert.Equal(0, len(c.ParamValues()))
assert.Equal(t, 0, len(c.ParamNames())) assert.Equal(0, len(c.ParamNames()))
assert.Equal(t, 0, len(c.store)) assert.Equal(0, len(c.store))
assert.Equal(t, "", c.Path()) assert.Equal("", c.Path())
assert.Equal(t, 0, len(c.QueryParams())) assert.Equal(0, len(c.QueryParams()))
} }
func TestContextCookie(t *testing.T) { func TestContextCookie(t *testing.T) {
@ -233,20 +235,22 @@ func TestContextCookie(t *testing.T) {
rec := httptest.NewRecorder() rec := httptest.NewRecorder()
c := e.NewContext(req, rec).(*context) c := e.NewContext(req, rec).(*context)
assert := assert.New(t)
// Read single // Read single
cookie, err := c.Cookie("theme") cookie, err := c.Cookie("theme")
if assert.NoError(t, err) { if assert.NoError(err) {
assert.Equal(t, "theme", cookie.Name) assert.Equal("theme", cookie.Name)
assert.Equal(t, "light", cookie.Value) assert.Equal("light", cookie.Value)
} }
// Read multiple // Read multiple
for _, cookie := range c.Cookies() { for _, cookie := range c.Cookies() {
switch cookie.Name { switch cookie.Name {
case "theme": case "theme":
assert.Equal(t, "light", cookie.Value) assert.Equal("light", cookie.Value)
case "user": case "user":
assert.Equal(t, "Jon Snow", cookie.Value) assert.Equal("Jon Snow", cookie.Value)
} }
} }
@ -261,11 +265,11 @@ func TestContextCookie(t *testing.T) {
HttpOnly: true, HttpOnly: true,
} }
c.SetCookie(cookie) c.SetCookie(cookie)
assert.Contains(t, rec.Header().Get(HeaderSetCookie), "SSID") assert.Contains(rec.Header().Get(HeaderSetCookie), "SSID")
assert.Contains(t, rec.Header().Get(HeaderSetCookie), "Ap4PGTEq") assert.Contains(rec.Header().Get(HeaderSetCookie), "Ap4PGTEq")
assert.Contains(t, rec.Header().Get(HeaderSetCookie), "labstack.com") assert.Contains(rec.Header().Get(HeaderSetCookie), "labstack.com")
assert.Contains(t, rec.Header().Get(HeaderSetCookie), "Secure") assert.Contains(rec.Header().Get(HeaderSetCookie), "Secure")
assert.Contains(t, rec.Header().Get(HeaderSetCookie), "HttpOnly") assert.Contains(rec.Header().Get(HeaderSetCookie), "HttpOnly")
} }
func TestContextPath(t *testing.T) { func TestContextPath(t *testing.T) {
@ -275,12 +279,15 @@ func TestContextPath(t *testing.T) {
r.Add(GET, "/users/:id", nil) r.Add(GET, "/users/:id", nil)
c := e.NewContext(nil, nil) c := e.NewContext(nil, nil)
r.Find(GET, "/users/1", c) r.Find(GET, "/users/1", c)
assert.Equal(t, "/users/:id", c.Path())
assert := assert.New(t)
assert.Equal("/users/:id", c.Path())
r.Add(GET, "/users/:uid/files/:fid", nil) r.Add(GET, "/users/:uid/files/:fid", nil)
c = e.NewContext(nil, nil) c = e.NewContext(nil, nil)
r.Find(GET, "/users/1/files/1", c) r.Find(GET, "/users/1/files/1", c)
assert.Equal(t, "/users/:uid/files/:fid", c.Path()) assert.Equal("/users/:uid/files/:fid", c.Path())
} }
func TestContextPathParam(t *testing.T) { func TestContextPathParam(t *testing.T) {

View File

@ -58,32 +58,34 @@ func TestEcho(t *testing.T) {
func TestEchoStatic(t *testing.T) { func TestEchoStatic(t *testing.T) {
e := New() e := New()
assert := assert.New(t)
// OK // OK
e.Static("/images", "_fixture/images") e.Static("/images", "_fixture/images")
c, b := request(GET, "/images/walle.png", e) c, b := request(GET, "/images/walle.png", e)
assert.Equal(t, http.StatusOK, c) assert.Equal(http.StatusOK, c)
assert.NotEmpty(t, b) assert.NotEmpty(b)
// No file // No file
e.Static("/images", "_fixture/scripts") e.Static("/images", "_fixture/scripts")
c, _ = request(GET, "/images/bolt.png", e) c, _ = request(GET, "/images/bolt.png", e)
assert.Equal(t, http.StatusNotFound, c) assert.Equal(http.StatusNotFound, c)
// Directory // Directory
e.Static("/images", "_fixture/images") e.Static("/images", "_fixture/images")
c, _ = request(GET, "/images", e) c, _ = request(GET, "/images", e)
assert.Equal(t, http.StatusNotFound, c) assert.Equal(http.StatusNotFound, c)
// Directory with index.html // Directory with index.html
e.Static("/", "_fixture") e.Static("/", "_fixture")
c, r := request(GET, "/", e) c, r := request(GET, "/", e)
assert.Equal(t, http.StatusOK, c) assert.Equal(http.StatusOK, c)
assert.Equal(t, true, strings.HasPrefix(r, "<!doctype html>")) assert.Equal(true, strings.HasPrefix(r, "<!doctype html>"))
// Sub-directory with index.html // Sub-directory with index.html
c, r = request(GET, "/folder", e) c, r = request(GET, "/folder", e)
assert.Equal(t, http.StatusOK, c) assert.Equal(http.StatusOK, c)
assert.Equal(t, true, strings.HasPrefix(r, "<!doctype html>")) assert.Equal(true, strings.HasPrefix(r, "<!doctype html>"))
} }
func TestEchoFile(t *testing.T) { func TestEchoFile(t *testing.T) {
@ -270,11 +272,13 @@ func TestEchoURL(t *testing.T) {
g := e.Group("/group") g := e.Group("/group")
g.GET("/users/:uid/files/:fid", getFile) g.GET("/users/:uid/files/:fid", getFile)
assert.Equal(t, "/static/file", e.URL(static)) assert := assert.New(t)
assert.Equal(t, "/users/:id", e.URL(getUser))
assert.Equal(t, "/users/1", e.URL(getUser, "1")) assert.Equal("/static/file", e.URL(static))
assert.Equal(t, "/group/users/1/files/:fid", e.URL(getFile, "1")) assert.Equal("/users/:id", e.URL(getUser))
assert.Equal(t, "/group/users/1/files/1", e.URL(getFile, "1", "1")) assert.Equal("/users/1", e.URL(getUser, "1"))
assert.Equal("/group/users/1/files/:fid", e.URL(getFile, "1"))
assert.Equal("/group/users/1/files/1", e.URL(getFile, "1", "1"))
} }
func TestEchoRoutes(t *testing.T) { func TestEchoRoutes(t *testing.T) {

View File

@ -26,10 +26,12 @@ func TestBasicAuth(t *testing.T) {
return c.String(http.StatusOK, "test") return c.String(http.StatusOK, "test")
}) })
assert := assert.New(t)
// Valid credentials // Valid credentials
auth := basic + " " + base64.StdEncoding.EncodeToString([]byte("joe:secret")) auth := basic + " " + base64.StdEncoding.EncodeToString([]byte("joe:secret"))
req.Header.Set(echo.HeaderAuthorization, auth) req.Header.Set(echo.HeaderAuthorization, auth)
assert.NoError(t, h(c)) assert.NoError(h(c))
h = BasicAuthWithConfig(BasicAuthConfig{ h = BasicAuthWithConfig(BasicAuthConfig{
Skipper: nil, Skipper: nil,
@ -42,28 +44,28 @@ func TestBasicAuth(t *testing.T) {
// Valid credentials // Valid credentials
auth = basic + " " + base64.StdEncoding.EncodeToString([]byte("joe:secret")) auth = basic + " " + base64.StdEncoding.EncodeToString([]byte("joe:secret"))
req.Header.Set(echo.HeaderAuthorization, auth) req.Header.Set(echo.HeaderAuthorization, auth)
assert.NoError(t, h(c)) assert.NoError(h(c))
// Case-insensitive header scheme // Case-insensitive header scheme
auth = strings.ToUpper(basic) + " " + base64.StdEncoding.EncodeToString([]byte("joe:secret")) auth = strings.ToUpper(basic) + " " + base64.StdEncoding.EncodeToString([]byte("joe:secret"))
req.Header.Set(echo.HeaderAuthorization, auth) req.Header.Set(echo.HeaderAuthorization, auth)
assert.NoError(t, h(c)) assert.NoError(h(c))
// Invalid credentials // Invalid credentials
auth = basic + " " + base64.StdEncoding.EncodeToString([]byte("joe:invalid-password")) auth = basic + " " + base64.StdEncoding.EncodeToString([]byte("joe:invalid-password"))
req.Header.Set(echo.HeaderAuthorization, auth) req.Header.Set(echo.HeaderAuthorization, auth)
he := h(c).(*echo.HTTPError) he := h(c).(*echo.HTTPError)
assert.Equal(t, http.StatusUnauthorized, he.Code) assert.Equal(http.StatusUnauthorized, he.Code)
assert.Equal(t, basic+` realm="someRealm"`, res.Header().Get(echo.HeaderWWWAuthenticate)) assert.Equal(basic+` realm="someRealm"`, res.Header().Get(echo.HeaderWWWAuthenticate))
// Missing Authorization header // Missing Authorization header
req.Header.Del(echo.HeaderAuthorization) req.Header.Del(echo.HeaderAuthorization)
he = h(c).(*echo.HTTPError) he = h(c).(*echo.HTTPError)
assert.Equal(t, http.StatusUnauthorized, he.Code) assert.Equal(http.StatusUnauthorized, he.Code)
// Invalid Authorization header // Invalid Authorization header
auth = base64.StdEncoding.EncodeToString([]byte("invalid")) auth = base64.StdEncoding.EncodeToString([]byte("invalid"))
req.Header.Set(echo.HeaderAuthorization, auth) req.Header.Set(echo.HeaderAuthorization, auth)
he = h(c).(*echo.HTTPError) he = h(c).(*echo.HTTPError)
assert.Equal(t, http.StatusUnauthorized, he.Code) assert.Equal(http.StatusUnauthorized, he.Code)
} }

View File

@ -33,11 +33,13 @@ func TestBodyDump(t *testing.T) {
responseBody = string(resBody) responseBody = string(resBody)
}) })
if assert.NoError(t, mw(h)(c)) { assert := assert.New(t)
assert.Equal(t, requestBody, hw)
assert.Equal(t, responseBody, hw) if assert.NoError(mw(h)(c)) {
assert.Equal(t, http.StatusOK, rec.Code) assert.Equal(requestBody, hw)
assert.Equal(t, hw, rec.Body.String()) assert.Equal(responseBody, hw)
assert.Equal(http.StatusOK, rec.Code)
assert.Equal(hw, rec.Body.String())
} }
// Must set default skipper // Must set default skipper

View File

@ -25,23 +25,25 @@ func TestBodyLimit(t *testing.T) {
return c.String(http.StatusOK, string(body)) return c.String(http.StatusOK, string(body))
} }
assert := assert.New(t)
// Based on content length (within limit) // Based on content length (within limit)
if assert.NoError(t, BodyLimit("2M")(h)(c)) { if assert.NoError(BodyLimit("2M")(h)(c)) {
assert.Equal(t, http.StatusOK, rec.Code) assert.Equal(http.StatusOK, rec.Code)
assert.Equal(t, hw, rec.Body.Bytes()) assert.Equal(hw, rec.Body.Bytes())
} }
// Based on content read (overlimit) // Based on content read (overlimit)
he := BodyLimit("2B")(h)(c).(*echo.HTTPError) he := BodyLimit("2B")(h)(c).(*echo.HTTPError)
assert.Equal(t, http.StatusRequestEntityTooLarge, he.Code) assert.Equal(http.StatusRequestEntityTooLarge, he.Code)
// Based on content read (within limit) // Based on content read (within limit)
req = httptest.NewRequest(echo.POST, "/", bytes.NewReader(hw)) req = httptest.NewRequest(echo.POST, "/", bytes.NewReader(hw))
rec = httptest.NewRecorder() rec = httptest.NewRecorder()
c = e.NewContext(req, rec) c = e.NewContext(req, rec)
if assert.NoError(t, BodyLimit("2M")(h)(c)) { if assert.NoError(BodyLimit("2M")(h)(c)) {
assert.Equal(t, http.StatusOK, rec.Code) assert.Equal(http.StatusOK, rec.Code)
assert.Equal(t, "Hello, World!", rec.Body.String()) assert.Equal("Hello, World!", rec.Body.String())
} }
// Based on content read (overlimit) // Based on content read (overlimit)
@ -49,7 +51,7 @@ func TestBodyLimit(t *testing.T) {
rec = httptest.NewRecorder() rec = httptest.NewRecorder()
c = e.NewContext(req, rec) c = e.NewContext(req, rec)
he = BodyLimit("2B")(h)(c).(*echo.HTTPError) he = BodyLimit("2B")(h)(c).(*echo.HTTPError)
assert.Equal(t, http.StatusRequestEntityTooLarge, he.Code) assert.Equal(http.StatusRequestEntityTooLarge, he.Code)
} }
func TestBodyLimitReader(t *testing.T) { func TestBodyLimitReader(t *testing.T) {

View File

@ -24,7 +24,10 @@ func TestGzip(t *testing.T) {
return nil return nil
}) })
h(c) h(c)
assert.Equal(t, "test", rec.Body.String())
assert := assert.New(t)
assert.Equal("test", rec.Body.String())
// Gzip // Gzip
req = httptest.NewRequest(echo.GET, "/", nil) req = httptest.NewRequest(echo.GET, "/", nil)
@ -32,14 +35,14 @@ func TestGzip(t *testing.T) {
rec = httptest.NewRecorder() rec = httptest.NewRecorder()
c = e.NewContext(req, rec) c = e.NewContext(req, rec)
h(c) h(c)
assert.Equal(t, gzipScheme, rec.Header().Get(echo.HeaderContentEncoding)) assert.Equal(gzipScheme, rec.Header().Get(echo.HeaderContentEncoding))
assert.Contains(t, rec.Header().Get(echo.HeaderContentType), echo.MIMETextPlain) assert.Contains(rec.Header().Get(echo.HeaderContentType), echo.MIMETextPlain)
r, err := gzip.NewReader(rec.Body) r, err := gzip.NewReader(rec.Body)
if assert.NoError(t, err) { if assert.NoError(err) {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
defer r.Close() defer r.Close()
buf.ReadFrom(r) buf.ReadFrom(r)
assert.Equal(t, "test", buf.String()) assert.Equal("test", buf.String())
} }
} }

View File

@ -25,21 +25,23 @@ func TestKeyAuth(t *testing.T) {
return c.String(http.StatusOK, "test") return c.String(http.StatusOK, "test")
}) })
assert := assert.New(t)
// Valid key // Valid key
auth := DefaultKeyAuthConfig.AuthScheme + " " + "valid-key" auth := DefaultKeyAuthConfig.AuthScheme + " " + "valid-key"
req.Header.Set(echo.HeaderAuthorization, auth) req.Header.Set(echo.HeaderAuthorization, auth)
assert.NoError(t, h(c)) assert.NoError(h(c))
// Invalid key // Invalid key
auth = DefaultKeyAuthConfig.AuthScheme + " " + "invalid-key" auth = DefaultKeyAuthConfig.AuthScheme + " " + "invalid-key"
req.Header.Set(echo.HeaderAuthorization, auth) req.Header.Set(echo.HeaderAuthorization, auth)
he := h(c).(*echo.HTTPError) he := h(c).(*echo.HTTPError)
assert.Equal(t, http.StatusUnauthorized, he.Code) assert.Equal(http.StatusUnauthorized, he.Code)
// Missing Authorization header // Missing Authorization header
req.Header.Del(echo.HeaderAuthorization) req.Header.Del(echo.HeaderAuthorization)
he = h(c).(*echo.HTTPError) he = h(c).(*echo.HTTPError)
assert.Equal(t, http.StatusBadRequest, he.Code) assert.Equal(http.StatusBadRequest, he.Code)
// Key from custom header // Key from custom header
config.KeyLookup = "header:API-Key" config.KeyLookup = "header:API-Key"
@ -47,7 +49,7 @@ func TestKeyAuth(t *testing.T) {
return c.String(http.StatusOK, "test") return c.String(http.StatusOK, "test")
}) })
req.Header.Set("API-Key", "valid-key") req.Header.Set("API-Key", "valid-key")
assert.NoError(t, h(c)) assert.NoError(h(c))
// Key from query string // Key from query string
config.KeyLookup = "query:key" config.KeyLookup = "query:key"
@ -57,7 +59,7 @@ func TestKeyAuth(t *testing.T) {
q := req.URL.Query() q := req.URL.Query()
q.Add("key", "valid-key") q.Add("key", "valid-key")
req.URL.RawQuery = q.Encode() req.URL.RawQuery = q.Encode()
assert.NoError(t, h(c)) assert.NoError(h(c))
// Key from form // Key from form
config.KeyLookup = "form:key" config.KeyLookup = "form:key"
@ -69,5 +71,5 @@ func TestKeyAuth(t *testing.T) {
req = httptest.NewRequest(echo.POST, "/", strings.NewReader(f.Encode())) req = httptest.NewRequest(echo.POST, "/", strings.NewReader(f.Encode()))
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationForm) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationForm)
c = e.NewContext(req, rec) c = e.NewContext(req, rec)
assert.NoError(t, h(c)) assert.NoError(h(c))
} }

View File

@ -53,7 +53,6 @@ func TestEchoRewritePreMiddleware(t *testing.T) {
// Route // Route
r.Add(echo.GET, "/new", func(c echo.Context) error { r.Add(echo.GET, "/new", func(c echo.Context) error {
return c.NoContent(200) return c.NoContent(200)
return nil
}) })
req := httptest.NewRequest(echo.GET, "/old", nil) req := httptest.NewRequest(echo.GET, "/old", nil)

View File

@ -18,8 +18,10 @@ func TestAddTrailingSlash(t *testing.T) {
return nil return nil
}) })
h(c) h(c)
assert.Equal(t, "/add-slash/", req.URL.Path)
assert.Equal(t, "/add-slash/", req.RequestURI) assert := assert.New(t)
assert.Equal("/add-slash/", req.URL.Path)
assert.Equal("/add-slash/", req.RequestURI)
// With config // With config
req = httptest.NewRequest(echo.GET, "/add-slash?key=value", nil) req = httptest.NewRequest(echo.GET, "/add-slash?key=value", nil)
@ -31,8 +33,8 @@ func TestAddTrailingSlash(t *testing.T) {
return nil return nil
}) })
h(c) h(c)
assert.Equal(t, http.StatusMovedPermanently, rec.Code) assert.Equal(http.StatusMovedPermanently, rec.Code)
assert.Equal(t, "/add-slash/?key=value", rec.Header().Get(echo.HeaderLocation)) assert.Equal("/add-slash/?key=value", rec.Header().Get(echo.HeaderLocation))
} }
func TestRemoveTrailingSlash(t *testing.T) { func TestRemoveTrailingSlash(t *testing.T) {
@ -44,8 +46,11 @@ func TestRemoveTrailingSlash(t *testing.T) {
return nil return nil
}) })
h(c) h(c)
assert.Equal(t, "/remove-slash", req.URL.Path)
assert.Equal(t, "/remove-slash", req.RequestURI) assert := assert.New(t)
assert.Equal("/remove-slash", req.URL.Path)
assert.Equal("/remove-slash", req.RequestURI)
// With config // With config
req = httptest.NewRequest(echo.GET, "/remove-slash/?key=value", nil) req = httptest.NewRequest(echo.GET, "/remove-slash/?key=value", nil)
@ -57,8 +62,8 @@ func TestRemoveTrailingSlash(t *testing.T) {
return nil return nil
}) })
h(c) h(c)
assert.Equal(t, http.StatusMovedPermanently, rec.Code) assert.Equal(http.StatusMovedPermanently, rec.Code)
assert.Equal(t, "/remove-slash?key=value", rec.Header().Get(echo.HeaderLocation)) assert.Equal("/remove-slash?key=value", rec.Header().Get(echo.HeaderLocation))
// With bare URL // With bare URL
req = httptest.NewRequest(echo.GET, "http://localhost", nil) req = httptest.NewRequest(echo.GET, "http://localhost", nil)
@ -68,5 +73,5 @@ func TestRemoveTrailingSlash(t *testing.T) {
return nil return nil
}) })
h(c) h(c)
assert.Equal(t, "", req.URL.Path) assert.Equal("", req.URL.Path)
} }

View File

@ -20,17 +20,20 @@ func TestStatic(t *testing.T) {
// Directory // Directory
h := StaticWithConfig(config)(echo.NotFoundHandler) h := StaticWithConfig(config)(echo.NotFoundHandler)
if assert.NoError(t, h(c)) {
assert.Contains(t, rec.Body.String(), "Echo") assert := assert.New(t)
if assert.NoError(h(c)) {
assert.Contains(rec.Body.String(), "Echo")
} }
// File found // File found
req = httptest.NewRequest(echo.GET, "/images/walle.png", nil) req = httptest.NewRequest(echo.GET, "/images/walle.png", nil)
rec = httptest.NewRecorder() rec = httptest.NewRecorder()
c = e.NewContext(req, rec) c = e.NewContext(req, rec)
if assert.NoError(t, h(c)) { if assert.NoError(h(c)) {
assert.Equal(t, http.StatusOK, rec.Code) assert.Equal(http.StatusOK, rec.Code)
assert.Equal(t, rec.Header().Get(echo.HeaderContentLength), "219885") assert.Equal(rec.Header().Get(echo.HeaderContentLength), "219885")
} }
// File not found // File not found
@ -38,7 +41,7 @@ func TestStatic(t *testing.T) {
rec = httptest.NewRecorder() rec = httptest.NewRecorder()
c = e.NewContext(req, rec) c = e.NewContext(req, rec)
he := h(c).(*echo.HTTPError) he := h(c).(*echo.HTTPError)
assert.Equal(t, http.StatusNotFound, he.Code) assert.Equal(http.StatusNotFound, he.Code)
// HTML5 // HTML5
req = httptest.NewRequest(echo.GET, "/random", nil) req = httptest.NewRequest(echo.GET, "/random", nil)
@ -47,9 +50,9 @@ func TestStatic(t *testing.T) {
config.HTML5 = true config.HTML5 = true
static := StaticWithConfig(config) static := StaticWithConfig(config)
h = static(echo.NotFoundHandler) h = static(echo.NotFoundHandler)
if assert.NoError(t, h(c)) { if assert.NoError(h(c)) {
assert.Equal(t, http.StatusOK, rec.Code) assert.Equal(http.StatusOK, rec.Code)
assert.Contains(t, rec.Body.String(), "Echo") assert.Contains(rec.Body.String(), "Echo")
} }
// Browse // Browse
@ -60,8 +63,8 @@ func TestStatic(t *testing.T) {
config.Browse = true config.Browse = true
static = StaticWithConfig(config) static = StaticWithConfig(config)
h = static(echo.NotFoundHandler) h = static(echo.NotFoundHandler)
if assert.NoError(t, h(c)) { if assert.NoError(h(c)) {
assert.Equal(t, http.StatusOK, rec.Code) assert.Equal(http.StatusOK, rec.Code)
assert.Contains(t, rec.Body.String(), "cert.pem") assert.Contains(rec.Body.String(), "cert.pem")
} }
} }