I'm currently trying to brainstorm and scope some possible ways to test a request to an endpoint that returns a SSE stream.
This, in part, shows my existing test-stagey for any requests that I'd like to make:
for _, test := range tests {
req, _ := http.NewRequest(
"GET",
test.route,
nil,
)
// Perform the request plain with the app.
// The -1 disables request latency.
res, err := r.Test(req, -1)
// verify that no error occured, that is not expected
assert.Equalf(t, test.expectedError, err != nil, test.description)
// As expected errors lead to broken responses, the next
// test case needs to be processed
if test.expectedError {
continue
}
// Verify if the status code is as expected
assert.Equalf(t, test.expectedCode, res.StatusCode, test.description)
// Read the response body
body, err := ioutil.ReadAll(res.Body)
defer res.Body.Close()
// Reading the response body should work everytime, such that
// the err variable should be nil
assert.Nilf(t, err, test.description)
// Verify, that the reponse body equals the expected body
if len(test.expectedBody) > 0 {
assert.Equalf(t, string(test.expectedBody), string(body), test.description)
}
}
However, when I add a route to my test runner that is to a connection that doesn't automatically close and instead is kept alive, my tests, obviously, will never finish.
Is there a way / pattern to test such keep-alive endpoints? I just really want to get the first sent event, and then close the connection.
I have tried A LOT, but nothing seems to close the damn connection programatically.
Any pro-tips on how to achieve this, would be excellent!