@@ -12,115 +12,48 @@ type TestResponse struct {
1212 Key string `json:"key"`
1313}
1414
15- func TestResponse_Marshal (t * testing.T ) {
16- tests := []struct {
17- name string
18- response Response [any ]
19- expected string
20- }{
21- {
22- name : "Basic Response" ,
23- response : Response [any ]{Code : 200 , Message : "OK" },
24- expected : `{"code":200,"message":"OK"}` ,
25- },
26- {
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"}}` ,
30- },
31- {
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-
4615func 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" ,
64- },
65- {
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" ,
73- },
74- }
75-
76- for _ , tt := range tests {
77- t .Run (tt .name , func (t * testing.T ) {
78- recorder := httptest .NewRecorder ()
79-
80- switch body := tt .body .(type ) {
81- case * TestResponse :
82- SendResponse [TestResponse ](recorder , tt .message , tt .code , body )
83- default :
84- SendResponse (recorder , tt .message , tt .code , & tt .body )
85- }
86-
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 ) {
9516 tests := []struct {
9617 name string
97- response Response [any ]
18+ code int
19+ data any
20+ errs []Error
21+ meta * Meta
9822 expectedCode int
99- expectedBody string
23+ expectedJSON string
10024 }{
10125 {
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"}}` ,
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"}}` ,
10632 },
10733 {
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"}` ,
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"}]}` ,
11240 },
11341 }
11442
11543 for _ , tt := range tests {
11644 t .Run (tt .name , func (t * testing.T ) {
117- recorder := httptest .NewRecorder ()
45+ w := httptest .NewRecorder ()
11846
119- writeResponse (recorder , & tt .response )
47+ switch data := tt .data .(type ) {
48+ case TestResponse :
49+ SendResponse [TestResponse ](w , tt .code , data , tt .errs , tt .meta )
50+ default :
51+ SendResponse [any ](w , tt .code , tt .data , tt .errs , tt .meta )
52+ }
12053
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 ())
54+ assert .Equal (t , tt .expectedCode , w .Code )
55+ assert .Equal (t , "application/json" , w .Header ().Get ("Content-Type" ))
56+ assert .JSONEq (t , tt .expectedJSON , w .Body .String ())
12457 })
12558 }
12659}
0 commit comments