Unit testing an HTTP handler that returns multiple file streams

WBOY
Release: 2024-02-09 08:39:18
forward
679 people have browsed it

对返回多个文件流的 HTTP 处理程序进行单元测试

#php editor Strawberry will introduce in this article how to unit test an HTTP handler that returns multiple file streams. During the development process, we often encounter situations where multiple file streams need to be returned, such as image compression, file merging, etc. However, unit testing in this case is not that easy to implement. In this article, we'll explore how to use the appropriate tools and techniques to write effective unit tests to ensure that our HTTP handlers return multiple file streams correctly.

Question content

http handler

I have an 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...))
}
Copy after login

unit test

I'm planning to write a unit test for an http handler and investigate the return stream for multiple files.

My unit tests have these helper types and functions:

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)
}
Copy after login

I then send a request to the http handler from the unit test using the above types and functions:

// 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?
Copy after login

Processing the returned file stream

How to separate multiple files returned by http handler? How to use the data stream returned by http handler?

Workaround

...Possible options: write length followed by file content...

Actually, the above option commented by @CeriseLimón is already implemented and used by the frontend.

The above is the detailed content of Unit testing an HTTP handler that returns multiple file streams. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!