Table of Contents
Question content
http handler
unit test
Processing the returned file stream
Workaround
Home Backend Development Golang Unit testing an HTTP handler that returns multiple file streams

Unit testing an HTTP handler that returns multiple file streams

Feb 09, 2024 am 08:39 AM
reflow

对返回多个文件流的 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!

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve Douyin traffic limit? What should I do if the traffic is limited so that it can flow back? How to solve Douyin traffic limit? What should I do if the traffic is limited so that it can flow back? Mar 22, 2024 am 09:00 AM

How to solve Douyin traffic limit? What should I do if the traffic is limited so that it can flow back?

'Collapsed Star Railroad' Mikhail Where Are You Going Achievement Guide 'Collapsed Star Railroad' Mikhail Where Are You Going Achievement Guide May 09, 2024 pm 09:20 PM

'Collapsed Star Railroad' Mikhail Where Are You Going Achievement Guide

Is Ace Racing Post-Holiday Syndrome: The real experienced drivers have already begun to prepare for the Lantern Festival Is Ace Racing Post-Holiday Syndrome: The real experienced drivers have already begun to prepare for the Lantern Festival Feb 21, 2024 pm 06:04 PM

Is Ace Racing Post-Holiday Syndrome: The real experienced drivers have already begun to prepare for the Lantern Festival

How to recover the lost Steam mobile token? Steam Appeal Guide How to recover the lost Steam mobile token? Steam Appeal Guide Mar 14, 2024 pm 10:07 PM

How to recover the lost Steam mobile token? Steam Appeal Guide

How to apply for QQ backup mailbox with QQ mailbox? How to apply for QQ backup mailbox with QQ mailbox How to apply for QQ backup mailbox with QQ mailbox? How to apply for QQ backup mailbox with QQ mailbox Mar 05, 2024 am 09:30 AM

How to apply for QQ backup mailbox with QQ mailbox? How to apply for QQ backup mailbox with QQ mailbox

Dogelon Mars Price Prediction 2024-2030 Dogelon Mars Price Prediction 2024-2030 Mar 22, 2024 pm 03:33 PM

Dogelon Mars Price Prediction 2024-2030

Thousands of people lined up for the final beta of "Taris World". Why did it get so much attention? Thousands of people lined up for the final beta of "Taris World". Why did it get so much attention? Mar 21, 2024 pm 02:50 PM

Thousands of people lined up for the final beta of "Taris World". Why did it get so much attention?

How to get money back via WeChat transfer How to get money back via WeChat transfer May 08, 2024 pm 01:18 PM

How to get money back via WeChat transfer

See all articles