@@ -12,115 +12,63 @@ type TestResponse struct {
1212 Key string `json:"key"`
1313}
1414
15- func TestResponse_Marshal (t * testing.T ) {
15+ func Test_SendResponse (t * testing.T ) {
1616 tests := []struct {
17- name string
18- response Response [any ]
19- expected string
17+ name string
18+ code int
19+ data any
20+ errs []Error
21+ meta * Meta
22+ expectedCode int
23+ expectedJSON string
2024 }{
2125 {
22- name : "Basic Response" ,
23- response : Response [any ]{Code : 200 , Message : "OK" },
24- expected : `{"code":200,"message":"OK"}` ,
26+ name : "200 OK with TestResponse body" ,
27+ code : http .StatusOK ,
28+ data : & TestResponse {Key : "value" },
29+ errs : nil ,
30+ expectedCode : http .StatusOK ,
31+ expectedJSON : `{"data":{"key":"value"}}` ,
2532 },
2633 {
27- name : "Response with Body" ,
28- response : Response [any ]{Code : 201 , Message : "Created" , Body : map [string ]string {"id" : "123" }},
29- expected : `{"code":201,"message":"Created","body":{"id":"123"}}` ,
34+ name : "404 Not Found without body" ,
35+ code : http .StatusNotFound ,
36+ data : nil ,
37+ errs : []Error {{Code : http .StatusNotFound , Message : "Not Found" }},
38+ expectedCode : http .StatusNotFound ,
39+ expectedJSON : `{"errors":[{"code":404,"message":"Not Found"}]}` ,
3040 },
3141 {
32- name : "Response with Empty Body" ,
33- response : Response [any ]{Code : 204 , Message : "No Content" , Body : nil },
34- expected : `{"code":204,"message":"No Content"}` ,
35- },
36- }
37-
38- for _ , tt := range tests {
39- t .Run (tt .name , func (t * testing.T ) {
40- jsonResponse := tt .response .Marshal ()
41- assert .JSONEq (t , tt .expected , string (jsonResponse ))
42- })
43- }
44- }
45-
46- func Test_SendResponse (t * testing.T ) {
47- tests := []struct {
48- name string
49- message string
50- code int
51- body any
52- expectedCode int
53- expectedBody string
54- expectedHeader string
55- }{
56- {
57- name : "200 OK with TestResponse body" ,
58- message : "Success" ,
59- code : http .StatusOK ,
60- body : & TestResponse {Key : "value" },
61- expectedCode : http .StatusOK ,
62- expectedBody : `{"code":200,"message":"Success","body":{"key":"value"}}` ,
63- expectedHeader : "application/json" ,
42+ name : "200 OK with pagination metadata" ,
43+ code : http .StatusOK ,
44+ data : & TestResponse {Key : "value" },
45+ meta : & Meta {TotalPages : 100 , Page : 1 , PageSize : 10 },
46+ expectedCode : http .StatusOK ,
47+ expectedJSON : `{"data":{"key":"value"},"meta":{"total_pages":100,"page":1,"page_size":10}}` ,
6448 },
6549 {
66- name : "404 Not Found without body" ,
67- message : "Not Found" ,
68- code : http .StatusNotFound ,
69- body : nil ,
70- expectedCode : http .StatusNotFound ,
71- expectedBody : `{"code":404,"message":"Not Found"}` ,
72- expectedHeader : "application/json" ,
50+ name : "400 Bad Request with multiple errors" ,
51+ code : http .StatusBadRequest ,
52+ errs : []Error {{Code : 400 , Message : "Invalid email" }, {Code : 400 , Message : "Invalid password" }},
53+ expectedCode : http .StatusBadRequest ,
54+ expectedJSON : `{"errors":[{"code":400,"message":"Invalid email"},{"code":400,"message":"Invalid password"}]}` ,
7355 },
7456 }
7557
7658 for _ , tt := range tests {
7759 t .Run (tt .name , func (t * testing.T ) {
78- recorder := httptest .NewRecorder ()
60+ w := httptest .NewRecorder ()
7961
80- switch body := tt .body .(type ) {
81- case * TestResponse :
82- SendResponse [TestResponse ](recorder , tt .message , tt .code , body )
62+ switch data := tt .data .(type ) {
63+ case TestResponse :
64+ SendResponse [TestResponse ](w , tt .code , data , tt .errs , tt . meta )
8365 default :
84- SendResponse ( recorder , tt .message , tt .code , & tt .body )
66+ SendResponse [ any ]( w , tt .code , tt .data , tt .errs , tt . meta )
8567 }
8668
87- assert .Equal (t , tt .expectedCode , recorder .Code )
88- assert .Equal (t , tt .expectedHeader , recorder .Header ().Get ("Content-Type" ))
89- assert .JSONEq (t , tt .expectedBody , recorder .Body .String ())
90- })
91- }
92- }
93-
94- func TestWriteResponse (t * testing.T ) {
95- tests := []struct {
96- name string
97- response Response [any ]
98- expectedCode int
99- expectedBody string
100- }{
101- {
102- name : "200 OK with Body" ,
103- response : Response [any ]{Code : 200 , Message : "OK" , Body : map [string ]string {"id" : "123" }},
104- expectedCode : 200 ,
105- expectedBody : `{"code":200,"message":"OK","body":{"id":"123"}}` ,
106- },
107- {
108- name : "500 Internal Server Error without Body" ,
109- response : Response [any ]{Code : 500 , Message : "Internal Server Error" },
110- expectedCode : 500 ,
111- expectedBody : `{"code":500,"message":"Internal Server Error"}` ,
112- },
113- }
114-
115- for _ , tt := range tests {
116- t .Run (tt .name , func (t * testing.T ) {
117- recorder := httptest .NewRecorder ()
118-
119- writeResponse (recorder , & tt .response )
120-
121- assert .Equal (t , tt .expectedCode , recorder .Code )
122- assert .Equal (t , "application/json" , recorder .Header ().Get ("Content-Type" ))
123- assert .JSONEq (t , tt .expectedBody , recorder .Body .String ())
69+ assert .Equal (t , tt .expectedCode , w .Code )
70+ assert .Equal (t , "application/json" , w .Header ().Get ("Content-Type" ))
71+ assert .JSONEq (t , tt .expectedJSON , w .Body .String ())
12472 })
12573 }
12674}
0 commit comments