1
0
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:
Martti T 2023-11-07 13:40:22 +02:00 committed by GitHub
parent c7d6d4373f
commit 50ebcd8d7c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 452 additions and 366 deletions

View File

@ -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)
} }

File diff suppressed because it is too large Load Diff

View File

@ -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")
} }