测试http.Response.Body已关闭

I'm searching for an easy way to test that http.Response.Body.Close() was called.

I already tried this by counting the number of open connections to the server (https://golang.org/src/net/http/serve_test.go - countCloseListener), but it seems that it only is testable on the client side, via rewriting the Roundtripper.

So the following example works, but I'm wondering whether there is an easier way for such a common task...

package closing

import "net/http"

type MyClient struct {
    Client http.Client
}

func (c *MyClient) Closing() (err error) {
    res, err := c.Client.Get("http://localhost")
    defer res.Body.Close()
    return
}

func (c *MyClient) NotClosing() (err error) {
    _, err = c.Client.Get("http://localhost")
    return
}

https://github.com/elgohr/golang-test-body-close/blob/master/performsHttpRequest.go

package closing_test

import (
    "github.com/elgohr/closing"
    "io"
    "io/ioutil"
    "net/http"
    "strings"
    "testing"
)

func TestShouldBeClosedWhenClosed(t *testing.T) {
    tripper := NewFakeRoundTripper()
    cl := closing.MyClient{
        Client: http.Client{
            Transport: tripper,
        },
    }

    if err := cl.Closing(); err != nil {
        t.Error(err)
    }

    if !tripper.Body.Closed {
        t.Error("Should be closed, but wasn't")
    }
}

func TestShouldBeOpenWhenNotClosed(t *testing.T) {
    tripper := NewFakeRoundTripper()
    cl := closing.MyClient{
        Client: http.Client{
            Transport: tripper,
        },
    }

    if err := cl.NotClosing(); err != nil {
        t.Error(err)
    }

    if tripper.Body.Closed {
        t.Error("Should be open, but wasn't")
    }
}

func NewFakeRoundTripper() *FakeRoundTripper {
    return &FakeRoundTripper{
        Body: &FakeReadCloser{
            ReadCloser: ioutil.NopCloser(strings.NewReader("content")),
        },
    }
}

type FakeRoundTripper struct {
    Body *FakeReadCloser
}

func (r *FakeRoundTripper) RoundTrip(*http.Request) (*http.Response, error) {
    return &http.Response{
        Body: r.Body,
    }, nil
}

type FakeReadCloser struct {
    io.ReadCloser
    Closed bool
}

func (r *FakeReadCloser) Close() error {
    r.Closed = true
    return r.ReadCloser.Close()
}

https://github.com/elgohr/golang-test-body-close/blob/master/performsHttpRequest_test.go

doubiantiao4994
doubiantiao4994 抱歉,不同意。顺便说一句,我们可以回到主题吗?您有更好的解决方案吗?
大约一年之前 回复
dtnnpt11795
dtnnpt11795 仅关闭它就足够了。
大约一年之前 回复
doubai9014
doubai9014 我并不是说每个人都在做TDD;但是,如果这样做的话,这是一项常见的任务,因为它需要按照规范说明关闭。
大约一年之前 回复
doubei3312
doubei3312 您为什么认为测试Close被称为“一项常见任务”?
大约一年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问