|
| 1 | +package echo |
| 2 | + |
| 3 | +import ( |
| 4 | + testify "github.com/stretchr/testify/assert" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | +) |
| 10 | + |
| 11 | +// Note this test is deliberately simple as there's not a lot to test. |
| 12 | +// Just need to ensure it writes JSONs. The heavy work is done by the context methods. |
| 13 | +func TestDefaultJSONCodec_Encode(t *testing.T) { |
| 14 | + e := New() |
| 15 | + req := httptest.NewRequest(http.MethodPost, "/", nil) |
| 16 | + rec := httptest.NewRecorder() |
| 17 | + c := e.NewContext(req, rec).(*context) |
| 18 | + |
| 19 | + assert := testify.New(t) |
| 20 | + |
| 21 | + // Echo |
| 22 | + assert.Equal(e, c.Echo()) |
| 23 | + |
| 24 | + // Request |
| 25 | + assert.NotNil(c.Request()) |
| 26 | + |
| 27 | + // Response |
| 28 | + assert.NotNil(c.Response()) |
| 29 | + |
| 30 | + //-------- |
| 31 | + // Default JSON encoder |
| 32 | + //-------- |
| 33 | + |
| 34 | + enc := new(DefaultJSONSerializer) |
| 35 | + |
| 36 | + err := enc.Serialize(c, user{1, "Jon Snow"}, "") |
| 37 | + if assert.NoError(err) { |
| 38 | + assert.Equal(userJSON+"\n", rec.Body.String()) |
| 39 | + } |
| 40 | + |
| 41 | + req = httptest.NewRequest(http.MethodPost, "/", nil) |
| 42 | + rec = httptest.NewRecorder() |
| 43 | + c = e.NewContext(req, rec).(*context) |
| 44 | + err = enc.Serialize(c, user{1, "Jon Snow"}, " ") |
| 45 | + if assert.NoError(err) { |
| 46 | + assert.Equal(userJSONPretty+"\n", rec.Body.String()) |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +// Note this test is deliberately simple as there's not a lot to test. |
| 51 | +// Just need to ensure it writes JSONs. The heavy work is done by the context methods. |
| 52 | +func TestDefaultJSONCodec_Decode(t *testing.T) { |
| 53 | + e := New() |
| 54 | + req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(userJSON)) |
| 55 | + rec := httptest.NewRecorder() |
| 56 | + c := e.NewContext(req, rec).(*context) |
| 57 | + |
| 58 | + assert := testify.New(t) |
| 59 | + |
| 60 | + // Echo |
| 61 | + assert.Equal(e, c.Echo()) |
| 62 | + |
| 63 | + // Request |
| 64 | + assert.NotNil(c.Request()) |
| 65 | + |
| 66 | + // Response |
| 67 | + assert.NotNil(c.Response()) |
| 68 | + |
| 69 | + //-------- |
| 70 | + // Default JSON encoder |
| 71 | + //-------- |
| 72 | + |
| 73 | + enc := new(DefaultJSONSerializer) |
| 74 | + |
| 75 | + var u = user{} |
| 76 | + err := enc.Deserialize(c, &u) |
| 77 | + if assert.NoError(err) { |
| 78 | + assert.Equal(u, user{ID: 1, Name: "Jon Snow"}) |
| 79 | + } |
| 80 | + |
| 81 | + var userUnmarshalSyntaxError = user{} |
| 82 | + req = httptest.NewRequest(http.MethodPost, "/", strings.NewReader(invalidContent)) |
| 83 | + rec = httptest.NewRecorder() |
| 84 | + c = e.NewContext(req, rec).(*context) |
| 85 | + err = enc.Deserialize(c, &userUnmarshalSyntaxError) |
| 86 | + assert.IsType(&HTTPError{}, err) |
| 87 | + 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") |
| 88 | + |
| 89 | + var userUnmarshalTypeError = struct { |
| 90 | + ID string `json:"id"` |
| 91 | + Name string `json:"name"` |
| 92 | + }{} |
| 93 | + |
| 94 | + req = httptest.NewRequest(http.MethodPost, "/", strings.NewReader(userJSON)) |
| 95 | + rec = httptest.NewRecorder() |
| 96 | + c = e.NewContext(req, rec).(*context) |
| 97 | + err = enc.Deserialize(c, &userUnmarshalTypeError) |
| 98 | + assert.IsType(&HTTPError{}, err) |
| 99 | + 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") |
| 100 | + |
| 101 | +} |
0 commit comments