doushuzd3033 2015-10-12 01:10
浏览 37
已采纳

去关闭僵局

Trying to mock http response in Go test. The code snippet below never terminates if I run it with

go test example.com/auth/...

package auth_test
import (
    "testing"
    "net/http/httptest"
    "net/http"
)

func TestAuthorization(t *testing.T) {
    t.Log("Should return 401 when Gateway returns 401")
    {
        url := oneOffUrlWithResponseCode(http.StatusUnauthorized)
        request, _ := http.NewRequest("GET", url, nil)
        response, _ := http.DefaultClient.Do(request)

        if response.StatusCode != http.StatusUnauthorized {
            t.Fatalf("Response should be 401 (Unauthorized)")
        }
    }
}

func oneOffUrlWithResponseCode(responseCode int) string {
    var server *httptest.Server
    server = httptest.NewServer(http.HandlerFunc(func(response http.ResponseWriter, request *http.Request) {
        defer server.Close()
        response.WriteHeader(responseCode)
    }))
    return server.URL
}

However, if I comment out this line

defer server.Close()

everything works fine.

Ideally, I do not want to "leak" *httptest.Server outside of oneOffUrlWithResponseCode function and obviously close it after first request.

Why it never terminates? What am I doing wrong? What is the right thing to do?

  • 写回答

1条回答 默认 最新

  • dongzi1397 2015-10-12 01:36
    关注

    The program doesn't terminate because of a deadlock (and it has nothing to do with closures). You cannot call Close inside a handler because internally Close waits for all handlers to finish.

    The easiest way to fix it is to "leak" httptest.Server outside of oneOffUrlWithResponseCode:

    func TestAuthorization(t *testing.T) {
        ...
        server := oneOffUrlWithResponseCode(http.StatusUnauthorized)
        defer server.Close()
        request, _ := http.NewRequest("GET", server.URL, nil)
        ...
    }
    
    func oneOffUrlWithResponseCode(responseCode int) *httptest.Server {
        return httptest.NewServer(http.HandlerFunc(func(response http.ResponseWriter, request *http.Request) {
            response.WriteHeader(responseCode)
        }))
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 高维数据处理方法求指导
  • ¥100 数字取证课程 关于FAT文件系统的操作
  • ¥15 如何使用js实现打印时每页设置统一的标题
  • ¥15 安装TIA PortalV15.1报错
  • ¥15 能把水桶搬到饮水机的机械设计
  • ¥15 Android Studio中如何把H5逻辑放在Assets 文件夹中以实现将h5代码打包为apk
  • ¥15 使用小程序wx.createWebAudioContext()开发节拍器
  • ¥15 关于#爬虫#的问题:请问HMDB代谢物爬虫的那个工具可以提供一下吗
  • ¥15 vue3+electron打包获取本地视频属性,文件夹里面有ffprobe.exe 文件还会报错这是什么原因呢?
  • ¥20 用51单片机控制急停。