This repository has been archived by the owner on Sep 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintegration_test.go
108 lines (94 loc) · 2.43 KB
/
integration_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package webtest_test
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"reflect"
"testing"
webtest "github.com/podhmo/go-webtest"
"github.com/podhmo/go-webtest/jsonequal"
"github.com/podhmo/go-webtest/snapshot"
"github.com/podhmo/go-webtest/tripperware"
"github.com/podhmo/go-webtest/try"
"github.com/podhmo/noerror"
)
type Input struct {
Values []int
}
func handleAdd(w http.ResponseWriter, req *http.Request) {
var data Input
decoder := json.NewDecoder(req.Body)
if err := decoder.Decode(&data); err != nil {
w.WriteHeader(400)
fmt.Fprintf(w, `{"error": %q}`, err.Error())
return
}
var n int
for _, v := range data.Values {
n += v
}
w.Header().Set("Content-Type", "application/json")
encoder := json.NewEncoder(w)
if err := encoder.Encode(map[string]int{"result": n}); err != nil {
panic(err)
}
}
func TestHandler(t *testing.T) {
t.Run("plain", func(t *testing.T) {
rec := httptest.NewRecorder()
req := httptest.NewRequest("POST", "/", bytes.NewBufferString(`{"values": [1,2,3]}`))
req.Header.Set("Content-Type", "application/json")
handleAdd(rec, req)
res := rec.Result()
if res.StatusCode != 200 {
b, _ := ioutil.ReadAll(res.Body)
t.Fatalf("status code, want 200, but got %d\n response:%s", res.StatusCode, string(b))
}
var got interface{}
decoder := json.NewDecoder(res.Body)
if err := decoder.Decode(&got); err != nil {
t.Fatal(err)
}
defer res.Body.Close()
want := snapshot.Take(t, &got)
if !reflect.DeepEqual(want, got) {
t.Errorf(`want %s, but got %s`, want, got)
}
})
t.Run("webtest", func(t *testing.T) {
c := webtest.NewClientFromHandler(http.HandlerFunc(handleAdd))
var want interface{}
got, err := c.Post("/",
webtest.WithJSONString(`{"values": [1,2,3]}`),
webtest.WithTripperware(
tripperware.ExpectCode(t, 200),
tripperware.GetExpectedDataFromSnapshot(t, &want),
),
)
noerror.Must(t, err)
defer func() { noerror.Must(t, got.Close()) }()
noerror.Should(t,
jsonequal.ShouldBeSame(
jsonequal.From(got.JSONData()),
jsonequal.From(want),
),
)
})
t.Run("try", func(t *testing.T) {
c := webtest.NewClientFromHandler(http.HandlerFunc(handleAdd))
var want interface{}
try.It{
Code: 200,
Want: &want,
ModifyResponse: func(res webtest.Response) (got interface{}) {
return res.JSONData()
},
}.With(t, c,
"POST", "/",
webtest.WithJSONString(`{"values": [1,2,3]}`),
)
})
}