I have a HTTP handler like this:
func RouteHandler(c echo.Context) error {
outs := make([]io.Reader, 5)
for i := range outs {
outs[i] = // ... comes from a logic.
}
return c.Stream(http.StatusOK, "application/binary", io.MultiReader(outs...))
}
I intend to write a unit test for HTTP handler and investigate the returned stream of multiple files.
I have these helper type and function for my unit test:
type Handler func(echo.Context) error
// Send request to a handler. Get back response body.
func Send(req *http.Request, handler Handler) ([]byte, error) {
w := httptest.NewRecorder()
e := echo.New()
c := e.NewContext(req, w)
// Call the handler.
err := handler(c)
if err != nil {
return nil, err
}
res := w.Result()
defer res.Body.Close()
return ioutil.ReadAll(res.Body)
}
Then, I use the above type and function to send a request to my HTTP handler from within my unit test:
// From within my unit test:
// Initialize request...
var data []byte
data, err := Send(request, RouteHandler)
// How to separate the multiple files returned here?
// How to work with the returned data?
How can I separate multiple files returned by the HTTP handler? How can I work with the stream of data returned by HTTP handler?
... Possible options: write a length followed by the content of a file...
Actually, the above option commented by @CeriseLimón was already implemented and was used by the front-end.