0

HTTP handler

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...))
}

Unit test

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?

Work with returned stream of files

How can I separate multiple files returned by the HTTP handler? How can I work with the stream of data returned by HTTP handler?

Megidd
  • 7,089
  • 6
  • 65
  • 142
  • 2
    How is the client supposed to handle the files? You test the handler the same way the client would work. – JimB Feb 12 '23 at 17:50
  • 2
    To separate the files on the client, the application must delimit the files in some way. Possible options: write a length followed by the content of a file; write a separator that cannot occur in any file. – Charlie Tumahai Feb 12 '23 at 17:51
  • @CeriseLimón You are right. – Megidd Feb 13 '23 at 06:35

1 Answers1

0

... 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.

Length + content

Megidd
  • 7,089
  • 6
  • 65
  • 142