mirror of
https://github.com/labstack/echo.git
synced 2024-12-22 20:06:21 +02:00
refactor context tests to be separate functions (#2540)
This commit is contained in:
parent
c7d6d4373f
commit
50ebcd8d7c
@ -1323,7 +1323,7 @@ func (b *ValueBinder) unixTime(sourceParam string, dest *time.Time, valueMustExi
|
|||||||
case time.Second:
|
case time.Second:
|
||||||
*dest = time.Unix(n, 0)
|
*dest = time.Unix(n, 0)
|
||||||
case time.Millisecond:
|
case time.Millisecond:
|
||||||
*dest = time.Unix(n/1e3, (n%1e3)*1e6) // TODO: time.UnixMilli(n) exists since Go1.17 switch to that when min version allows
|
*dest = time.UnixMilli(n)
|
||||||
case time.Nanosecond:
|
case time.Nanosecond:
|
||||||
*dest = time.Unix(0, n)
|
*dest = time.Unix(0, n)
|
||||||
}
|
}
|
||||||
|
772
context_test.go
772
context_test.go
File diff suppressed because it is too large
Load Diff
38
json_test.go
38
json_test.go
@ -1,7 +1,7 @@
|
|||||||
package echo
|
package echo
|
||||||
|
|
||||||
import (
|
import (
|
||||||
testify "github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"strings"
|
"strings"
|
||||||
@ -16,16 +16,14 @@ func TestDefaultJSONCodec_Encode(t *testing.T) {
|
|||||||
rec := httptest.NewRecorder()
|
rec := httptest.NewRecorder()
|
||||||
c := e.NewContext(req, rec).(*context)
|
c := e.NewContext(req, rec).(*context)
|
||||||
|
|
||||||
assert := testify.New(t)
|
|
||||||
|
|
||||||
// Echo
|
// Echo
|
||||||
assert.Equal(e, c.Echo())
|
assert.Equal(t, e, c.Echo())
|
||||||
|
|
||||||
// Request
|
// Request
|
||||||
assert.NotNil(c.Request())
|
assert.NotNil(t, c.Request())
|
||||||
|
|
||||||
// Response
|
// Response
|
||||||
assert.NotNil(c.Response())
|
assert.NotNil(t, c.Response())
|
||||||
|
|
||||||
//--------
|
//--------
|
||||||
// Default JSON encoder
|
// Default JSON encoder
|
||||||
@ -34,16 +32,16 @@ func TestDefaultJSONCodec_Encode(t *testing.T) {
|
|||||||
enc := new(DefaultJSONSerializer)
|
enc := new(DefaultJSONSerializer)
|
||||||
|
|
||||||
err := enc.Serialize(c, user{1, "Jon Snow"}, "")
|
err := enc.Serialize(c, user{1, "Jon Snow"}, "")
|
||||||
if assert.NoError(err) {
|
if assert.NoError(t, err) {
|
||||||
assert.Equal(userJSON+"\n", rec.Body.String())
|
assert.Equal(t, userJSON+"\n", rec.Body.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
req = httptest.NewRequest(http.MethodPost, "/", nil)
|
req = httptest.NewRequest(http.MethodPost, "/", nil)
|
||||||
rec = httptest.NewRecorder()
|
rec = httptest.NewRecorder()
|
||||||
c = e.NewContext(req, rec).(*context)
|
c = e.NewContext(req, rec).(*context)
|
||||||
err = enc.Serialize(c, user{1, "Jon Snow"}, " ")
|
err = enc.Serialize(c, user{1, "Jon Snow"}, " ")
|
||||||
if assert.NoError(err) {
|
if assert.NoError(t, err) {
|
||||||
assert.Equal(userJSONPretty+"\n", rec.Body.String())
|
assert.Equal(t, userJSONPretty+"\n", rec.Body.String())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -55,16 +53,14 @@ func TestDefaultJSONCodec_Decode(t *testing.T) {
|
|||||||
rec := httptest.NewRecorder()
|
rec := httptest.NewRecorder()
|
||||||
c := e.NewContext(req, rec).(*context)
|
c := e.NewContext(req, rec).(*context)
|
||||||
|
|
||||||
assert := testify.New(t)
|
|
||||||
|
|
||||||
// Echo
|
// Echo
|
||||||
assert.Equal(e, c.Echo())
|
assert.Equal(t, e, c.Echo())
|
||||||
|
|
||||||
// Request
|
// Request
|
||||||
assert.NotNil(c.Request())
|
assert.NotNil(t, c.Request())
|
||||||
|
|
||||||
// Response
|
// Response
|
||||||
assert.NotNil(c.Response())
|
assert.NotNil(t, c.Response())
|
||||||
|
|
||||||
//--------
|
//--------
|
||||||
// Default JSON encoder
|
// Default JSON encoder
|
||||||
@ -74,8 +70,8 @@ func TestDefaultJSONCodec_Decode(t *testing.T) {
|
|||||||
|
|
||||||
var u = user{}
|
var u = user{}
|
||||||
err := enc.Deserialize(c, &u)
|
err := enc.Deserialize(c, &u)
|
||||||
if assert.NoError(err) {
|
if assert.NoError(t, err) {
|
||||||
assert.Equal(u, user{ID: 1, Name: "Jon Snow"})
|
assert.Equal(t, u, user{ID: 1, Name: "Jon Snow"})
|
||||||
}
|
}
|
||||||
|
|
||||||
var userUnmarshalSyntaxError = user{}
|
var userUnmarshalSyntaxError = user{}
|
||||||
@ -83,8 +79,8 @@ func TestDefaultJSONCodec_Decode(t *testing.T) {
|
|||||||
rec = httptest.NewRecorder()
|
rec = httptest.NewRecorder()
|
||||||
c = e.NewContext(req, rec).(*context)
|
c = e.NewContext(req, rec).(*context)
|
||||||
err = enc.Deserialize(c, &userUnmarshalSyntaxError)
|
err = enc.Deserialize(c, &userUnmarshalSyntaxError)
|
||||||
assert.IsType(&HTTPError{}, err)
|
assert.IsType(t, &HTTPError{}, err)
|
||||||
assert.EqualError(err, "code=400, message=Syntax error: offset=1, error=invalid character 'i' looking for beginning of value, internal=invalid character 'i' looking for beginning of value")
|
assert.EqualError(t, err, "code=400, message=Syntax error: offset=1, error=invalid character 'i' looking for beginning of value, internal=invalid character 'i' looking for beginning of value")
|
||||||
|
|
||||||
var userUnmarshalTypeError = struct {
|
var userUnmarshalTypeError = struct {
|
||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
@ -95,7 +91,7 @@ func TestDefaultJSONCodec_Decode(t *testing.T) {
|
|||||||
rec = httptest.NewRecorder()
|
rec = httptest.NewRecorder()
|
||||||
c = e.NewContext(req, rec).(*context)
|
c = e.NewContext(req, rec).(*context)
|
||||||
err = enc.Deserialize(c, &userUnmarshalTypeError)
|
err = enc.Deserialize(c, &userUnmarshalTypeError)
|
||||||
assert.IsType(&HTTPError{}, err)
|
assert.IsType(t, &HTTPError{}, err)
|
||||||
assert.EqualError(err, "code=400, message=Unmarshal type error: expected=string, got=number, field=id, offset=7, internal=json: cannot unmarshal number into Go struct field .id of type string")
|
assert.EqualError(t, err, "code=400, message=Unmarshal type error: expected=string, got=number, field=id, offset=7, internal=json: cannot unmarshal number into Go struct field .id of type string")
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user