MCPcopy Create free account
hub / github.com/DoNewsCode/core / TestEncoder

Function TestEncoder

srvhttp/encode_test.go:31–91  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

29}
30
31func TestEncoder(t *testing.T) {
32 cases := []struct {
33 name string
34 input interface{}
35 error error
36 expected *MockWriter
37 }{
38 {
39 "normal struct",
40 struct {
41 Foo string `json:"foo"`
42 }{"foo"},
43 nil,
44 &MockWriter{
45 code: 200,
46 header: make(http.Header),
47 buffer: *bytes.NewBufferString(`{"foo":"foo"}` + "\n"),
48 },
49 },
50 {
51 "empty string",
52 nil,
53 nil,
54 &MockWriter{
55 code: 200,
56 header: make(http.Header),
57 buffer: *bytes.NewBufferString(`null` + "\n"),
58 },
59 },
60 {
61 "error response",
62 nil,
63 errors.New("foo"),
64 &MockWriter{
65 code: 500,
66 header: make(http.Header),
67 buffer: *bytes.NewBufferString(`{"message":"foo"}` + "\n"),
68 },
69 },
70 {
71 "Error response",
72 nil,
73 unierr.NotFoundErr(errors.New("foo"), "bar"),
74 &MockWriter{
75 code: 404,
76 header: make(http.Header),
77 buffer: *bytes.NewBufferString(`{"code":5,"message":"bar"}` + "\n"),
78 },
79 },
80 }
81 for _, cc := range cases {
82 c := cc
83 t.Run(c.name, func(t *testing.T) {
84 t.Parallel()
85 writer := &MockWriter{header: make(http.Header)}
86 NewResponseEncoder(writer).Encode(c.input, c.error)
87 assert.Equal(t, c.expected.code, writer.code)
88 assert.Equal(t, c.expected.buffer.String(), writer.buffer.String())

Callers

nothing calls this directly

Calls 5

NotFoundErrFunction · 0.92
NewResponseEncoderFunction · 0.85
EncodeMethod · 0.80
StringMethod · 0.65
RunMethod · 0.45

Tested by

no test coverage detected