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.
(t *testing.T)
| 51 | // Note this test is deliberately simple as there's not a lot to test. |
| 52 | // Just need to ensure it writes JSONs. The heavy work is done by the context methods. |
| 53 | func TestDefaultJSONCodec_Decode(t *testing.T) { |
| 54 | e := New() |
| 55 | req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(userJSON)) |
| 56 | rec := httptest.NewRecorder() |
| 57 | c := e.NewContext(req, rec) |
| 58 | |
| 59 | // Echo |
| 60 | assert.Equal(t, e, c.Echo()) |
| 61 | |
| 62 | // Request |
| 63 | assert.NotNil(t, c.Request()) |
| 64 | |
| 65 | // Response |
| 66 | assert.NotNil(t, c.Response()) |
| 67 | |
| 68 | //-------- |
| 69 | // Default JSON encoder |
| 70 | //-------- |
| 71 | |
| 72 | enc := new(DefaultJSONSerializer) |
| 73 | |
| 74 | var u = user{} |
| 75 | err := enc.Deserialize(c, &u) |
| 76 | if assert.NoError(t, err) { |
| 77 | assert.Equal(t, u, user{ID: 1, Name: "Jon Snow"}) |
| 78 | } |
| 79 | |
| 80 | var userUnmarshalSyntaxError = user{} |
| 81 | req = httptest.NewRequest(http.MethodPost, "/", strings.NewReader(invalidContent)) |
| 82 | rec = httptest.NewRecorder() |
| 83 | c = e.NewContext(req, rec) |
| 84 | err = enc.Deserialize(c, &userUnmarshalSyntaxError) |
| 85 | assert.IsType(t, &HTTPError{}, err) |
| 86 | assert.EqualError(t, err, "code=400, message=Bad Request, err=invalid character 'i' looking for beginning of value") |
| 87 | |
| 88 | var userUnmarshalTypeError = struct { |
| 89 | ID string `json:"id"` |
| 90 | Name string `json:"name"` |
| 91 | }{} |
| 92 | |
| 93 | req = httptest.NewRequest(http.MethodPost, "/", strings.NewReader(userJSON)) |
| 94 | rec = httptest.NewRecorder() |
| 95 | c = e.NewContext(req, rec) |
| 96 | err = enc.Deserialize(c, &userUnmarshalTypeError) |
| 97 | assert.IsType(t, &HTTPError{}, err) |
| 98 | assert.EqualError(t, err, "code=400, message=Bad Request, err=json: cannot unmarshal number into Go struct field .id of type string") |
| 99 | |
| 100 | } |
nothing calls this directly
no test coverage detected
searching dependent graphs…