acc: Fix RecordRequests to support requests without body (#2333)

## Changes
Do not paste request body into output if it's not a valid JSON.

## Tests
While working on #2334 I found that if I try to record a test that calls
/api/2.0/preview/scim/v2/Me which has no request body, it crashes.
This commit is contained in:
Denis Bilenko 2025-02-11 11:50:52 +01:00 committed by GitHub
parent 8d849fe868
commit 878fa80322
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 11 additions and 4 deletions

View File

@ -34,7 +34,8 @@ type Request struct {
Headers http.Header `json:"headers,omitempty"`
Method string `json:"method"`
Path string `json:"path"`
Body any `json:"body"`
Body any `json:"body,omitempty"`
RawBody string `json:"raw_body,omitempty"`
}
func New(t testutil.TestingT) *Server {
@ -119,13 +120,19 @@ func (s *Server) Handle(pattern string, handler HandlerFunc) {
}
}
s.Requests = append(s.Requests, Request{
req := Request{
Headers: headers,
Method: r.Method,
Path: r.URL.Path,
Body: json.RawMessage(body),
})
}
if json.Valid(body) {
req.Body = json.RawMessage(body)
} else {
req.RawBody = string(body)
}
s.Requests = append(s.Requests, req)
}
w.Header().Set("Content-Type", "application/json")