duanduji2986 2015-11-28 07:25
浏览 152
已采纳

如何在Go中使用Martini测试反向代理

I'm writing test code for martini app working as a reverse proxy in go, and want to test it using httptest.ResponseRecorder, but I got an error the following.

[martini] PANIC: interface conversion: *httptest.ResponseRecorder is not http.CloseNotifier: missing method CloseNotify

httptest.ResponseRecorder has no method CloseNotify()

How should I test it?

package main

import (
        "github.com/go-martini/martini"
        "github.com/stretchr/testify/assert"
        "net/http"
        "net/http/httptest"
        "net/http/httputil"
        "net/url"
        "testing"
)

func TestReverseProxy(t *testing.T) {
        // Mock backend
        backendResponse := "I am the backend"
        backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
                w.Write([]byte(backendResponse))
        }))
        defer backend.Close()
        backendURL, _ := url.Parse(backend.URL)

        // Frontend
        m := martini.Classic()
        m.Get("/", func(w http.ResponseWriter, r *http.Request) {
                proxy := httputil.NewSingleHostReverseProxy(backendURL)
                proxy.ServeHTTP(w, r)
        })

        // Testing
        req, _ := http.NewRequest("GET", "/", nil)
        res := httptest.NewRecorder()
        m.ServeHTTP(res, req)

        assert.Equal(t, 200, res.Code, "should be equal")
}
  • 写回答

1条回答 默认 最新

  • doushang4274 2015-11-28 13:15
    关注

    First, please note that the martini framework is no longer maintained as said in their README.

    Then, about your issue, it's because Martini does something that looks pretty bad to me: it takes an http.ResponseWriter and assumes it is also an http.CloseNotifier, while there is absolutely no guarantee of this. They should take a custom interface wrapping both of them, something like that:

    type ResponseWriterCloseNotifier interface {
        http.ResponseWriter
        http.CloseNotifier
    }
    

    You can see in their source code that they had the same issue for their own tests, and used some workaround: https://github.com/go-martini/martini/commit/063dfcd8b0f64f4e2c97f0bc27fa422969baa23b#L13

    Here is some working code made with it:

    package main
    
    import (
        "net/http"
        "net/http/httptest"
        "net/http/httputil"
        "net/url"
        "testing"
    
        "github.com/go-martini/martini"
        "github.com/stretchr/testify/assert"
    )
    
    type closeNotifyingRecorder struct {
        *httptest.ResponseRecorder
        closed chan bool
    }
    
    func newCloseNotifyingRecorder() *closeNotifyingRecorder {
        return &closeNotifyingRecorder{
            httptest.NewRecorder(),
            make(chan bool, 1),
        }
    }
    
    func (c *closeNotifyingRecorder) close() {
        c.closed <- true
    }
    
    func (c *closeNotifyingRecorder) CloseNotify() <-chan bool {
        return c.closed
    }
    
    func TestReverseProxy(t *testing.T) {
        // Mock backend
        backendResponse := "I am the backend"
        backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
            w.Write([]byte(backendResponse))
        }))
        defer backend.Close()
        backendURL, _ := url.Parse(backend.URL)
    
        // Frontend
        m := martini.Classic()
        m.Get("/", func(w http.ResponseWriter, r *http.Request) {
            proxy := httputil.NewSingleHostReverseProxy(backendURL)
            proxy.ServeHTTP(w, r)
        })
    
        // Testing
        req, _ := http.NewRequest("GET", "/", nil)
        res := newCloseNotifyingRecorder()
        m.ServeHTTP(res, req)
    
        assert.Equal(t, 200, res.Code, "should be equal")
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥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核心网网元