@@ -26,7 +26,8 @@ type Server struct {
2626 fakeWorkspaces map [string ]* FakeWorkspace
2727 mu * sync.Mutex
2828
29- RecordRequestsCallback func (request * Request )
29+ RequestCallback func (request * Request )
30+ ResponseCallback func (request * Request , response * EncodedResponse )
3031}
3132
3233type Request struct {
@@ -44,7 +45,7 @@ type Response struct {
4445 Body any
4546}
4647
47- type encodedResponse struct {
48+ type EncodedResponse struct {
4849 StatusCode int
4950 Headers http.Header
5051 Body []byte
@@ -66,31 +67,31 @@ func NewRequest(t testutil.TestingT, r *http.Request, fakeWorkspace *FakeWorkspa
6667 }
6768}
6869
69- func normalizeResponse (t testutil.TestingT , resp any ) encodedResponse {
70+ func normalizeResponse (t testutil.TestingT , resp any ) EncodedResponse {
7071 result := normalizeResponseBody (t , resp )
7172 if result .StatusCode == 0 {
7273 result .StatusCode = 200
7374 }
7475 return result
7576}
7677
77- func normalizeResponseBody (t testutil.TestingT , resp any ) encodedResponse {
78+ func normalizeResponseBody (t testutil.TestingT , resp any ) EncodedResponse {
7879 if isNil (resp ) {
7980 t .Errorf ("Handler must not return nil" )
80- return encodedResponse {StatusCode : 500 }
81+ return EncodedResponse {StatusCode : 500 }
8182 }
8283
8384 respBytes , ok := resp .([]byte )
8485 if ok {
85- return encodedResponse {
86+ return EncodedResponse {
8687 Body : respBytes ,
8788 Headers : getHeaders (respBytes ),
8889 }
8990 }
9091
9192 respString , ok := resp .(string )
9293 if ok {
93- return encodedResponse {
94+ return EncodedResponse {
9495 Body : []byte (respString ),
9596 Headers : getHeaders ([]byte (respString )),
9697 }
@@ -99,7 +100,7 @@ func normalizeResponseBody(t testutil.TestingT, resp any) encodedResponse {
99100 respStruct , ok := resp .(Response )
100101 if ok {
101102 if isNil (respStruct .Body ) {
102- return encodedResponse {
103+ return EncodedResponse {
103104 StatusCode : respStruct .StatusCode ,
104105 Headers : respStruct .Headers ,
105106 Body : []byte {},
@@ -108,7 +109,7 @@ func normalizeResponseBody(t testutil.TestingT, resp any) encodedResponse {
108109
109110 bytesVal , isBytes := respStruct .Body .([]byte )
110111 if isBytes {
111- return encodedResponse {
112+ return EncodedResponse {
112113 StatusCode : respStruct .StatusCode ,
113114 Headers : respStruct .Headers ,
114115 Body : bytesVal ,
@@ -117,7 +118,7 @@ func normalizeResponseBody(t testutil.TestingT, resp any) encodedResponse {
117118
118119 stringVal , isString := respStruct .Body .(string )
119120 if isString {
120- return encodedResponse {
121+ return EncodedResponse {
121122 StatusCode : respStruct .StatusCode ,
122123 Headers : respStruct .Headers ,
123124 Body : []byte (stringVal ),
@@ -127,7 +128,7 @@ func normalizeResponseBody(t testutil.TestingT, resp any) encodedResponse {
127128 respBytes , err := json .MarshalIndent (respStruct .Body , "" , " " )
128129 if err != nil {
129130 t .Errorf ("JSON encoding error: %s" , err )
130- return encodedResponse {
131+ return EncodedResponse {
131132 StatusCode : 500 ,
132133 Body : []byte ("internal error" ),
133134 }
@@ -138,7 +139,7 @@ func normalizeResponseBody(t testutil.TestingT, resp any) encodedResponse {
138139 headers = getJsonHeaders ()
139140 }
140141
141- return encodedResponse {
142+ return EncodedResponse {
142143 StatusCode : respStruct .StatusCode ,
143144 Headers : headers ,
144145 Body : respBytes ,
@@ -148,13 +149,13 @@ func normalizeResponseBody(t testutil.TestingT, resp any) encodedResponse {
148149 respBytes , err := json .MarshalIndent (resp , "" , " " )
149150 if err != nil {
150151 t .Errorf ("JSON encoding error: %s" , err )
151- return encodedResponse {
152+ return EncodedResponse {
152153 StatusCode : 500 ,
153154 Body : []byte ("internal error" ),
154155 }
155156 }
156157
157- return encodedResponse {
158+ return EncodedResponse {
158159 Body : respBytes ,
159160 Headers : getJsonHeaders (),
160161 }
@@ -253,9 +254,11 @@ func (s *Server) Handle(method, path string, handler HandlerFunc) {
253254 }
254255
255256 request := NewRequest (s .t , r , fakeWorkspace )
256- if s .RecordRequestsCallback != nil {
257- s .RecordRequestsCallback (& request )
257+
258+ if s .RequestCallback != nil {
259+ s .RequestCallback (& request )
258260 }
261+
259262 respAny := handler (request )
260263 resp := normalizeResponse (s .t , respAny )
261264
@@ -265,6 +268,10 @@ func (s *Server) Handle(method, path string, handler HandlerFunc) {
265268
266269 w .WriteHeader (resp .StatusCode )
267270
271+ if s .ResponseCallback != nil {
272+ s .ResponseCallback (& request , & resp )
273+ }
274+
268275 if _ , err := w .Write (resp .Body ); err != nil {
269276 s .t .Errorf ("Failed to write response: %s" , err )
270277 return
0 commit comments