0
私はそうのように見える何か持っモックするhttptestを使用して、次の正しく応答
func (client *MyCustomClient) CheckURL(url string, json_response *MyCustomResponseStruct) bool {
r, err = http.Get(url)
if err != nil {
return false
}
defer r.Body.Close()
.... do stuff with json_response
そして、私のテストでは、私が持っている:
func TestCheckURL(t *test.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=UTF-8")
fmt.Fprintln(w, `{"status": "success"}`)
}))
defer ts.Close()
json_response := new(MyCustomResponseStruct)
client := NewMyCustomClient() // returns instance of MyCustomClient
done := client.CheckURL("test.com", json_response)
しかし、それがあるかのように表示されませんがHTTPテストサーバーが動作していて、実際ログ出力が示すように、test.comに送信されます。
Get http:/test.com: dial tcp X.Y.Z.A: i/o timeout
私の質問はどのようにですかo適切にhttptestパッケージを使用してこのリクエストを模倣する...私はthe docsを読んで、これは役に立ちましたSO Answerしかし、私はまだ立ち往生しています。
私はこの重要なビットがありませんでした。これは、ドキュメント/例を再読することが理にかなっています。 – thomascirca