如何使用 Go 的 httptest 模拟多个不同的 HTTP 响应?

How to simulate multiple different HTTP responses using Go's httptest?

我创建了一些 Go 函数,可以对 Internet 上的服务进行 HTTP GET 调用并解析结果。

我现在正在为这些功能编写测试用例。 在我的测试用例中,我使用 go 包 httptest 来模拟对这些外部服务的调用。下面是我的代码。为简洁起见,有意删除了错误检查。这是 go-playground.

package main

import (
    "fmt"
    "io"
    "context"
    "net/http"
    "net/http/httptest"
)

func handlerResponse() http.Handler {
    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        w.WriteHeader(http.StatusOK)
        w.Write([]byte(`{"A":"B"}`))
    })
}

func buildMyRequest(ctx context.Context, url string) *http.Request {
    request, _ := http.NewRequestWithContext(ctx, "GET", url, nil)
    return request
}

func myPrint(response *http.Response) {
    b := make([]byte, 60000)
    for {
        _, err := response.Body.Read(b)
        if err == io.EOF {
            break
        }
    }
    fmt.Println(string(b))
}

func main() {
    srv := httptest.NewServer(handlerResponse())            
    client := http.Client{}

    myResponse1, _ := client.Do(buildMyRequest(context.Background(), srv.URL))
    fmt.Println("myResponse1:")
    myPrint(myResponse1)
    
    myResponse2, _ := client.Do(buildMyRequest(context.Background(), srv.URL))
    fmt.Println("myResponse2:")
    myPrint(myResponse2)

}

这是它产生的输出:

myResponse1:
{"A":"B"}
myResponse2:
{"A":"B"}

如您所见,我创建了一些虚拟 HTTP 响应数据 {"A":"B"},当您向 srv.URL 发送 HTTP 请求时,它实际上会访问一个临时 HTTP 服务器,该服务器会使用虚拟数据进行响应.酷!

当您向 srv.URL 发送第二个 HTTP 请求时,它再次以相同的虚拟数据进行响应。但这就是我的问题出现的地方。我希望临时 HTTP 服务器 return 第二次 {"C":"D"} 和第三次 {"E":"F"} 收到一些不同的数据。

如何更改 main() 函数的第一行,以便服务器在后续 HTTP 调用中响应我所需的数据?

你可以使用如下的 hack(游乐场:here

package main

import (
    "fmt"
    "io"
    "context"
    "net/http"
    "net/http/httptest"
    "sync"
)


type responseWriter struct{
   resp map[int]string
   count int
   lock *sync.Mutex
}

func NewResponseWriter()*responseWriter{
   r := new(responseWriter)
   r.lock = new(sync.Mutex)
   r.resp = map[int]string{
    0: `{"E":"F"}`,
    1: `{"A":"B"}`,
    2: `{"C":"D"}`,
   }
   r.count = 0
   return r
}

func (r *responseWriter)GetResp()string{
   r.lock.Lock()
   defer r.lock.Unlock()
   r.count ++
   return r.resp[r.count%3]
}


func handlerResponse(rr *responseWriter) http.Handler {
    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        w.WriteHeader(http.StatusOK)
        w.Write([]byte(rr.GetResp()))
    })
}

func buildMyRequest(ctx context.Context, url string) *http.Request {
    request, _ := http.NewRequestWithContext(ctx, "GET", url, nil)
    return request
}


func myPrint(response *http.Response) {
    b := make([]byte, 60000)
    for {
        _, err := response.Body.Read(b)
        if err == io.EOF {
            break
        }
    }
    fmt.Println(string(b))
}

func main() {
        rr := NewResponseWriter()

    srv := httptest.NewServer(handlerResponse(rr))  
    client := http.Client{}

    myResponse1, err := client.Do(buildMyRequest(context.Background(), srv.URL))
    if err != nil{
       fmt.Println(err)
       return
    }
    
    defer myResponse1.Body.Close()
    fmt.Println("myResponse1:")
    myPrint(myResponse1)
    
    myResponse2, err := client.Do(buildMyRequest(context.Background(), srv.URL))
    if err != nil{
       fmt.Println(err)
       return
    }
    
    defer myResponse2.Body.Close()
    fmt.Println("myResponse2:")
    myPrint(myResponse2)
}