-
HTTP handlerI 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...))
} Unit testI 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? Work with returned stream of filesHow can I separate multiple files returned by the HTTP handler? How can I work with the stream of data returned by HTTP handler? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
I have no idea how you can distinguish separate files written to stream with multireader. Multireader just concatenates reader outputs to single "stream" of bytes.
You probably could hack something together with multipart. See this https://peter.bourgon.org/blog/2019/02/12/multipart-http-responses.html but this means that you need "special" handling of response at the client side also. |
Beta Was this translation helpful? Give feedback.
I have no idea how you can distinguish separate files written to stream with multireader. Multireader just concatenates reader outputs to single "stream" of bytes.
c.Stream
is absolutely generic and it is up to you define rules to how to interpret its output.You probably could hack something together with multipart. See this https://peter.bourgon.org/blog/2019/02/12/multipart-http-responses.html but this means that you need "special" handling of response at the client side also.