Last active
January 16, 2022 08:47
-
-
Save yukihir0/8b58666582edf78b6a8c to your computer and use it in GitHub Desktop.
GolangでHTTPサーバのモック使ってテストを実行する。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"github.com/jarcoal/httpmock" | |
"io/ioutil" | |
"net/http" | |
"testing" | |
) | |
func TestHttpMockSample(t *testing.T) { | |
httpmock.Activate() | |
defer httpmock.DeactivateAndReset() | |
httpmock.RegisterResponder( | |
"GET", | |
"http://www.google.co.jp", | |
httpmock.NewStringResponder(200, "this is a mock.")) | |
res, _ := http.Get("http://www.google.co.jp") | |
body, _ := ioutil.ReadAll(res.Body) | |
defer res.Body.Close() | |
expected := "this is a mock." | |
ret := string(body) | |
if ret != expected { | |
t.Errorf("expected %s, but got %s\n", expected, ret) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment