2021-07-05 21:33:19 +02:00
package echo
import (
2022-12-04 22:17:48 +02:00
"github.com/stretchr/testify/assert"
2021-07-05 21:33:19 +02:00
"net/http"
"net/http/httptest"
"strings"
"testing"
)
// Note this test is deliberately simple as there's not a lot to test.
// Just need to ensure it writes JSONs. The heavy work is done by the context methods.
func TestDefaultJSONCodec_Encode ( t * testing . T ) {
e := New ( )
req := httptest . NewRequest ( http . MethodPost , "/" , nil )
rec := httptest . NewRecorder ( )
2021-07-15 22:34:01 +02:00
c := e . NewContext ( req , rec ) . ( * DefaultContext )
2021-07-05 21:33:19 +02:00
// Echo
2022-12-04 22:17:48 +02:00
assert . Equal ( t , e , c . Echo ( ) )
2021-07-05 21:33:19 +02:00
// Request
2022-12-04 22:17:48 +02:00
assert . NotNil ( t , c . Request ( ) )
2021-07-05 21:33:19 +02:00
// Response
2022-12-04 22:17:48 +02:00
assert . NotNil ( t , c . Response ( ) )
2021-07-05 21:33:19 +02:00
//--------
// Default JSON encoder
//--------
enc := new ( DefaultJSONSerializer )
err := enc . Serialize ( c , user { 1 , "Jon Snow" } , "" )
2022-12-04 22:17:48 +02:00
if assert . NoError ( t , err ) {
assert . Equal ( t , userJSON + "\n" , rec . Body . String ( ) )
2021-07-05 21:33:19 +02:00
}
req = httptest . NewRequest ( http . MethodPost , "/" , nil )
rec = httptest . NewRecorder ( )
2021-07-15 22:34:01 +02:00
c = e . NewContext ( req , rec ) . ( * DefaultContext )
2021-07-05 21:33:19 +02:00
err = enc . Serialize ( c , user { 1 , "Jon Snow" } , " " )
2022-12-04 22:17:48 +02:00
if assert . NoError ( t , err ) {
assert . Equal ( t , userJSONPretty + "\n" , rec . Body . String ( ) )
2021-07-05 21:33:19 +02:00
}
}
// Note this test is deliberately simple as there's not a lot to test.
// Just need to ensure it writes JSONs. The heavy work is done by the context methods.
func TestDefaultJSONCodec_Decode ( t * testing . T ) {
e := New ( )
req := httptest . NewRequest ( http . MethodPost , "/" , strings . NewReader ( userJSON ) )
rec := httptest . NewRecorder ( )
2021-07-15 22:34:01 +02:00
c := e . NewContext ( req , rec ) . ( * DefaultContext )
2021-07-05 21:33:19 +02:00
// Echo
2022-12-04 22:17:48 +02:00
assert . Equal ( t , e , c . Echo ( ) )
2021-07-05 21:33:19 +02:00
// Request
2022-12-04 22:17:48 +02:00
assert . NotNil ( t , c . Request ( ) )
2021-07-05 21:33:19 +02:00
// Response
2022-12-04 22:17:48 +02:00
assert . NotNil ( t , c . Response ( ) )
2021-07-05 21:33:19 +02:00
//--------
// Default JSON encoder
//--------
enc := new ( DefaultJSONSerializer )
var u = user { }
err := enc . Deserialize ( c , & u )
2022-12-04 22:17:48 +02:00
if assert . NoError ( t , err ) {
assert . Equal ( t , u , user { ID : 1 , Name : "Jon Snow" } )
2021-07-05 21:33:19 +02:00
}
var userUnmarshalSyntaxError = user { }
req = httptest . NewRequest ( http . MethodPost , "/" , strings . NewReader ( invalidContent ) )
rec = httptest . NewRecorder ( )
2021-07-15 22:34:01 +02:00
c = e . NewContext ( req , rec ) . ( * DefaultContext )
2021-07-05 21:33:19 +02:00
err = enc . Deserialize ( c , & userUnmarshalSyntaxError )
2022-12-04 22:17:48 +02:00
assert . IsType ( t , & HTTPError { } , err )
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" )
2021-07-05 21:33:19 +02:00
var userUnmarshalTypeError = struct {
ID string ` json:"id" `
Name string ` json:"name" `
} { }
req = httptest . NewRequest ( http . MethodPost , "/" , strings . NewReader ( userJSON ) )
rec = httptest . NewRecorder ( )
2021-07-15 22:34:01 +02:00
c = e . NewContext ( req , rec ) . ( * DefaultContext )
2021-07-05 21:33:19 +02:00
err = enc . Deserialize ( c , & userUnmarshalTypeError )
2022-12-04 22:17:48 +02:00
assert . IsType ( t , & HTTPError { } , err )
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" )
2021-07-05 21:33:19 +02:00
}