MCPcopy
hub / github.com/labstack/echo / TestDefaultJSONCodec_Decode

Function TestDefaultJSONCodec_Decode

json_test.go:53–100  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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.
53func 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}

Callers

nothing calls this directly

Calls 6

EchoMethod · 0.95
RequestMethod · 0.95
ResponseMethod · 0.95
NewFunction · 0.85
NewContextMethod · 0.80
DeserializeMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…