第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 go 中使用馬提尼測試反向代理

如何在 go 中使用馬提尼測試反向代理

Go
一只萌萌小番薯 2021-11-22 20:05:32
我正在為在 go 中作為反向代理工作的 martini 應(yīng)用程序編寫測試代碼,并想使用 測試它httptest.ResponseRecorder,但出現(xiàn)以下錯誤。[martini] PANIC: interface conversion: *httptest.ResponseRecorder is not http.CloseNotifier: missing method CloseNotifyhttptest.ResponseRecorder 沒有方法 CloseNotify()我應(yīng)該如何測試?package mainimport (        "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 回答

?
慕森王

TA貢獻(xiàn)1777條經(jīng)驗 獲得超3個贊

首先,請注意 martini 框架不再像他們的README 中所說的那樣維護(hù)。


然后,關(guān)于您的問題,這是因為 Martini 做了一些對我來說看起來很糟糕的事情:它需要一個http.ResponseWriter并假設(shè)它也是一個http.CloseNotifier,而絕對不能保證這一點。他們應(yīng)該采用一個自定義界面來包裝它們,如下所示:


type ResponseWriterCloseNotifier interface {

    http.ResponseWriter

    http.CloseNotifier

}

您可以在他們的源代碼中看到他們在自己的測試中遇到了同樣的問題,并使用了一些解決方法:https : //github.com/go-martini/martini/commit/063dfcd8b0f64f4e2c97f0bc27fa422969baa23b#L13


這是一些用它制作的工作代碼:


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")

}


查看完整回答
反對 回復(fù) 2021-11-22
  • 1 回答
  • 0 關(guān)注
  • 197 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號