go-snaps icon indicating copy to clipboard operation
go-snaps copied to clipboard

[Feature Request]: Support for HTTP Response

Open amir20 opened this issue 11 months ago • 1 comments

🚀 Feature Proposal

Hello, I have been using https://github.com/beme/abide and would like to move to a new, more maintained alternative. One of the features that is very helpful is to directly test again and http response like:

rr := httptest.NewRecorder()
handler.ServeHTTP(rr, req)
abide.AssertHTTPResponse(t, t.Name(), rr.Result()) // <--

This would be very helpful. Internally, I would expect go-snaps to dump the headers and body.

Motivation

Replace abide and make testing http easier.

Example

I would expect validators to also work. Currently, I have something this:

/* snapshot: Test_handler_streamEvents_happy */
HTTP/1.1 200 OK
Connection: close
Cache-Control: no-transform
Cache-Control: no-cache
Connection: keep-alive

event: container-event
data: {"name":"start","host":"localhost","actorId":"1234","time":"0001-01-01T00:00:00Z"}

However, time can change. I would imagine this wouldn't work as-is since this is not valid JSON. But if there was a way to use regular expression that would be even better. I currently do this:

rr := httptest.NewRecorder()
handler.ServeHTTP(rr, req)
reader := strings.NewReader(regexp.MustCompile(`"time":"[^"]*"`).ReplaceAllString(rr.Body.String(), `"time":"<removed>"`))
abide.AssertReader(t, t.Name(), reader)

I would prefer to just have the snapshot library handle this.

amir20 avatar Jan 03 '25 21:01 amir20

Hey 👋 Thanks for opening this issue. I have been thinking for a while supporting some type of HTTP snapshots but I wasn't sure what a good featurset would be. I will have to do some reading/investigation to see how and what could be supported.

gkampitakis avatar Jan 04 '25 19:01 gkampitakis