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 AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

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)

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

The Lantern Festival is coming soon. Ace Racing has specially prepared a Lantern Festival riddle guessing activity for everyone. If you guess the riddles and successfully answer 6 questions correctly, you can receive gold coin rewards. If you accumulate a certain number of riddles, you can also get generous rewards. Specific details Let’s take a look at the details of this event. Is Ace Racing Post-Holiday Syndrome: The real experienced drivers have already begun to prepare for the Lantern Festival. Hey, dear drivers, they all say that the Spring Festival is not the end until after the Lantern Festival. We welcome the lantern festival, eat glutinous rice balls, and set off fireworks...the Festival of Speed ​​is everywhere In the festive and lively atmosphere of the festival, Xili has also prepared some interesting lantern riddles that "only real experienced drivers" can get for everyone to warm up in advance. Let's guess together~ (PS: Drivers can go to today's public account Tweet to see the answer!) How about it, riders?

'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

Collapse Star Dome Railway Where Are You Going Mikhail achievement guide. With the update to version 2.2 of Honkai Dome Railway, there are a lot of new content in the game that can be experienced. I believe that many friends have encountered some difficulties when completing the achievement "Where Are You Going, Mikhail?" I don’t know how to complete it, so today I will take you through the detailed process. Guide to the Collapsed Star Railroad Where Are You Going Mikhail 1. When we inherited the capabilities of the Tongtun Pioneers and solved the crisis in Sinokonni, everything settled and we returned to the top of Flowing Dream Reef. The transfer point is the one marked in the picture below; 2. After reaching it, go straight forward, look at Mikhail again, and investigate the balcony in front of him; 3. After completing the investigation, you can obtain the achievement Mikhail

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

With the popularity of Douyin, the problems commonly faced by users are also gradually increasing. The most worrying one is the problem of Douyin's traffic limit. Douyin's traffic limit may lead to a significant reduction in the number of views, likes and comments on users' videos, thereby affecting users' income and exposure opportunities. 1. How to solve Douyin’s traffic limit? 1. Improve content quality The core competitiveness of Douyin lies in content. Only high-quality content can attract more users. Therefore, improving content quality is the key to solving Douyin’s current limiting problem. Creators need to focus on content innovation, attract users with unique perspectives and creativity, and at the same time ensure that the content is interesting, educational and practical. Only in this way can the user experience be continuously improved and user stickiness increased. 2. Adjust the release time. Douyin’s traffic distribution has a time pattern.

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

ELON peaked at $0.0000005196 in 2023 and has been declining ever since. Our Dogelon Mars price prediction estimates the price by the end of 2024 at $0.0000001409. According to our Dogelon Mars price prediction for 2025, ELON is trading at $0.0000004709.

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

The article brought to you today is about QQ mailbox software. Do you know how to apply for QQ backup mailbox with QQ mailbox? The following content brings you the method of applying for QQ backup mailbox with QQ mailbox. Let us take a look below. . After logging in to your QQ mailbox, you can look for an icon with a triangle logo on the homepage of your mailbox. If you can't find the icon, you can view detailed annotations on the image to help locate it. After finding it, click the 3-corner logo, and then click the Apply for Backup Email button. After clicking, we click the email account button in the upper right corner of the page that just came out. After clicking, we enter the new registration email name, password, mobile phone number and other operations and then click the registration button. After the registration is completed, we return to the email page just now and click on the box that just popped up to enter the account number.

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

On March 18, the dual-end interactive mobile game "Taris World" independently developed by Beijing Ledong Excellence and distributed by Tencent started its final test on the eve of the eve. "Taris World" has more or less features from occupation to gameplay. It pays tribute to the classics, and I believe that players who like MMO games can find a new world again. Looking back on the overseas testing in November last year, the super high game quality attracted a large number of overseas players and many MMORPG anchors to rush to play. After the final test was launched on the eve of the national server at 10 o'clock on March 18, a large number of players flocked to the game. In "Taris World", even if some players did not qualify for the test, tens of thousands of people lined up in just a few minutes. Nowadays, the popularity of the final test on the eve of the national server can be said to be even greater. (Picture source comes from the Internet) Since "

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

When using the Steam mobile token, I find that the token has disappeared. What should I do? If there is no token, the security verification of the Steam account cannot be performed. How do we retrieve the Steam mobile token? The editor below will teach you how to appeal for lost Steam mobile token. The appeal tutorial is as follows: 1. Enter the Steam customer service page steam customer service and select the option in the red box 2. Select the corresponding content according to your own situation. If your account has been stolen or your password has been lost, generally select the option in the first red box. 3. Select the option in the red box 4. Enter your account name (login ID, not personal nickname), bound email address, or bound mobile phone number, and then click

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

1. Open the WeChat app, find the transfer message that needs to be returned, and click to enter. 2. In the transfer details interface, find and click the [Return] option. 3. In the pop-up window, click the [Return] button to return the transferred money.

See all articles