drkrsx3135168 2019-05-15 04:28
浏览 15
已采纳

最少的http服务测试而无需重新设置路由?

Say I have a very simple Web service.

func main() {
    http.HandleFunc("/", sanityTest)
    log.Fatal(http.ListenAndServe(":8000", nil))
}

If I want to test it, I could minimally just have:

func ExampleTest() {
        server := httptest.NewServer(http.DefaultServeMux)
        defer server.Close()

        resp, err := http.Get(server.URL)
        if err != nil {
                log.Fatal(err)
        }
        body, _ := ioutil.ReadAll(resp.Body)

        fmt.Println(resp.StatusCode)
        fmt.Println(resp.Header.Get("Content-Type"))
        fmt.Println(string(body))

        // Output:
        // 200
        // text/plain; charset=utf-8
        // OK

}

But that will result in a 404, since it doesn't know about the routes. So what I've seen main_test.go code do, is re-setup the handles in the test file's init, like so:

func init() {
    http.HandleFunc("/", sanityTest)
}

Which leads to duplication, and inevitably I have to create a function in main.go like:

func setupRoutes() {
        http.HandleFunc("/", sanityTest)
}

Which I find a little ugly. Am I missing a trick to instantiate the routes from main.go and avoid the init?

  • 写回答

1条回答 默认 最新

  • douyigua5381 2019-05-15 07:21
    关注

    You can re-use routes between tests and main.go file, also it's helpful if you want to mock something in your handlers (add a new argument to router() func below)

    main.go:

    func sanityTest(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "%s", "sanity test")
    }
    
    func router() *http.ServeMux {
        h := http.NewServeMux()
        h.HandleFunc("/", sanityTest)
        return h
    }
    
    func main() {
        http.ListenAndServe(":8080", router())
    }
    

    main_test.go:

    func TestSanity(t *testing.T) {
        tests := []struct {
            name string
            uri  string
            want string
        }{
            {"1", "/", "sanity test"},
        }
    
        ts := httptest.NewServer(router())
        defer ts.Close()
    
        for _, tt := range tests {
            t.Run(tt.name, func(t *testing.T) {
                url := ts.URL + tt.uri
                resp, _ := http.Get(url)
                respBody, _ := ioutil.ReadAll(resp.Body)
                resp.Body.Close()
    
                got := string(respBody)
                if got != tt.want {
                    t.Errorf("got %s, Want %s", got, tt.want)
                }
            })
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元