回答編集履歴
2
リクエストURLチェック版を追記
answer
CHANGED
@@ -24,4 +24,37 @@
|
|
24
24
|
t.Errorf("got %v want %s", actual, expected)
|
25
25
|
}
|
26
26
|
}
|
27
|
+
```
|
28
|
+
|
29
|
+
リクエストURLチェック追加版
|
30
|
+
参考: [Goで外部リクエストが関わる処理をテストする](http://qiita.com/taizo/items/32d895e35397336bf285)
|
31
|
+
|
32
|
+
```go
|
33
|
+
package main
|
34
|
+
|
35
|
+
import (
|
36
|
+
"fmt"
|
37
|
+
"net/http"
|
38
|
+
"net/http/httptest"
|
39
|
+
"testing"
|
40
|
+
)
|
41
|
+
|
42
|
+
func TestFetch(t *testing.T) {
|
43
|
+
path := "/path/to/ip"
|
44
|
+
expected := "1.2.3.4"
|
45
|
+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
46
|
+
if g, w := r.URL.Path, path; g != w {
|
47
|
+
t.Errorf("request got path %s, want %s", g, w)
|
48
|
+
}
|
49
|
+
fmt.Fprint(w, expected)
|
50
|
+
}))
|
51
|
+
defer ts.Close()
|
52
|
+
actual, err := Fetch(ts.URL + path)
|
53
|
+
if err != nil {
|
54
|
+
t.Errorf("%s", err)
|
55
|
+
}
|
56
|
+
if actual != expected {
|
57
|
+
t.Errorf("got %v want %s", actual, expected)
|
58
|
+
}
|
59
|
+
}
|
27
60
|
```
|
1
コードブロックの言語名修正
answer
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
[httptestパッケージのExamples](https://golang.org/pkg/net/http/httptest/#example_Server)の通りに作ればテストが通りましたよ。
|
2
2
|
|
3
|
-
```go
|
3
|
+
```go
|
4
4
|
package main
|
5
5
|
|
6
6
|
import (
|