Skip to content
This repository was archived by the owner on Sep 27, 2020. It is now read-only.

Commit b72ccc0

Browse files
authored
Rewrite try (#57)
* tests: add without-webtest * tests: update tests * feat: rewrite try * tests: fix tests * fat: try package
1 parent 3692f0b commit b72ccc0

File tree

8 files changed

+185
-85
lines changed

8 files changed

+185
-85
lines changed

httpbin/integration_test.go

+19-25
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/podhmo/go-webtest/httpbin/httpbintest"
1111
"github.com/podhmo/go-webtest/jsonequal"
1212
"github.com/podhmo/go-webtest/tripperware"
13+
"github.com/podhmo/go-webtest/try"
1314
"github.com/podhmo/noerror"
1415
)
1516

@@ -91,42 +92,35 @@ func TestIt(t *testing.T) {
9192

9293
t.Run("auth", func(t *testing.T) {
9394
cases := []struct {
94-
user string
95-
pass string
96-
code int
97-
assertion webtest.Assertion
95+
code int
96+
user string
97+
pass string
9898
}{
9999
{
100100
code: 200,
101-
user: "user", pass: "pass",
102-
assertion: func(t *testing.T, got webtest.Response) {
103-
noerror.Should(t,
104-
jsonequal.ShouldBeSame(
105-
jsonequal.From(got.JSONData()),
106-
jsonequal.FromString(`{"authenticated": true, "user": "user"}`),
107-
),
108-
)
109-
},
101+
user: "user",
102+
pass: "pass",
110103
},
111104
{
112105
code: 401,
113-
user: "user", pass: "another",
106+
user: "user",
107+
pass: "another",
114108
},
115109
}
116-
117110
for _, c := range cases {
118111
c := c
119112
t.Run(fmt.Sprintf("%d", c.code), func(t *testing.T) {
120-
webtest.
121-
Try(t, c.assertion).
122-
With(client.Get("/auth/basic-auth/user/pass",
123-
webtest.WithTripperware(
124-
tripperware.ExpectCode(t, c.code),
125-
),
126-
webtest.WithModifyRequest(func(req *http.Request) {
127-
req.SetBasicAuth(c.user, c.pass)
128-
}),
129-
))
113+
var want interface{}
114+
115+
try.It{
116+
Code: c.code,
117+
Want: &want,
118+
}.With(t, client,
119+
"GET", "/auth/basic-auth/user/pass",
120+
webtest.WithModifyRequest(func(req *http.Request) {
121+
req.SetBasicAuth(c.user, c.pass)
122+
}),
123+
)
130124
})
131125
}
132126
})
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"modifiedAt": "2019-09-07T22:12:19.402752889+09:00",
3+
"data": {
4+
"request": {
5+
"method": "GET",
6+
"path": "/auth/basic-auth/user/pass"
7+
},
8+
"response": {
9+
"data": {
10+
"authenticated": false
11+
},
12+
"statusCode": 401
13+
}
14+
}
15+
}

integration_test.go

+69-17
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,17 @@ import (
44
"bytes"
55
"encoding/json"
66
"fmt"
7+
"io/ioutil"
78
"net/http"
9+
"net/http/httptest"
10+
"reflect"
811
"testing"
912

1013
webtest "github.com/podhmo/go-webtest"
1114
"github.com/podhmo/go-webtest/jsonequal"
15+
"github.com/podhmo/go-webtest/snapshot"
1216
"github.com/podhmo/go-webtest/tripperware"
17+
"github.com/podhmo/go-webtest/try"
1318
"github.com/podhmo/noerror"
1419
)
1520

@@ -38,21 +43,68 @@ func Add(w http.ResponseWriter, req *http.Request) {
3843
}
3944

4045
func TestHandler(t *testing.T) {
41-
c := webtest.NewClientFromHandler(http.HandlerFunc(Add))
42-
var want interface{}
43-
got, err := c.Post("/",
44-
webtest.WithJSON(bytes.NewBufferString(`{"values": [1,2,3]}`)),
45-
webtest.WithTripperware(
46-
tripperware.ExpectCode(t, 200),
47-
tripperware.GetExpectedDataFromSnapshot(t, &want),
48-
),
49-
)
50-
51-
noerror.Must(t, err)
52-
noerror.Should(t,
53-
jsonequal.ShouldBeSame(
54-
jsonequal.From(got.JSONData()),
55-
jsonequal.From(want),
56-
),
57-
)
46+
t.Run("plain", func(t *testing.T) {
47+
w := httptest.NewRecorder()
48+
req := httptest.NewRequest("POST", "/", bytes.NewBufferString(`{"values": [1,2,3]}`))
49+
req.Header.Set("Content-Type", "application/json")
50+
51+
Add(w, req)
52+
res := w.Result()
53+
54+
if res.StatusCode != 200 {
55+
b, err := ioutil.ReadAll(res.Body)
56+
t.Fatalf("status code, want 200, but got %d\n response:%s", res.StatusCode, string(b))
57+
if err != nil {
58+
t.Fatal(err)
59+
}
60+
}
61+
62+
var got interface{}
63+
decoder := json.NewDecoder(res.Body)
64+
if err := decoder.Decode(&got); err != nil {
65+
t.Fatal(err)
66+
}
67+
defer res.Body.Close()
68+
69+
want := snapshot.Take(t, &got)
70+
if !reflect.DeepEqual(want, got) {
71+
t.Errorf(`want %s, but got %s`, want, got)
72+
}
73+
})
74+
75+
t.Run("webtest", func(t *testing.T) {
76+
c := webtest.NewClientFromHandler(http.HandlerFunc(Add))
77+
var want interface{}
78+
got, err := c.Post("/",
79+
webtest.WithJSON(bytes.NewBufferString(`{"values": [1,2,3]}`)),
80+
webtest.WithTripperware(
81+
tripperware.ExpectCode(t, 200),
82+
tripperware.GetExpectedDataFromSnapshot(t, &want),
83+
),
84+
)
85+
86+
noerror.Must(t, err)
87+
noerror.Should(t,
88+
jsonequal.ShouldBeSame(
89+
jsonequal.From(got.JSONData()),
90+
jsonequal.From(want),
91+
),
92+
)
93+
})
94+
95+
t.Run("try", func(t *testing.T) {
96+
c := webtest.NewClientFromHandler(http.HandlerFunc(Add))
97+
98+
var want interface{}
99+
try.It{
100+
Code: 200,
101+
Want: &want,
102+
ModifyResponse: func(res webtest.Response) (got interface{}) {
103+
return res.JSONData()
104+
},
105+
}.With(t, c,
106+
"POST", "/",
107+
webtest.WithJSON(bytes.NewBufferString(`{"values": [1,2,3]}`)),
108+
)
109+
})
58110
}

testdata/TestHandler/plain.golden

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"modifiedAt": "2019-09-07T20:28:44.213057069+09:00",
3+
"data": {
4+
"result": 6
5+
}
6+
}

testdata/TestHandler/try.golden

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"modifiedAt": "2019-09-07T21:40:30.70331035+09:00",
3+
"data": {
4+
"request": {
5+
"method": "POST",
6+
"path": "/"
7+
},
8+
"response": {
9+
"data": {
10+
"result": 6
11+
},
12+
"statusCode": 200
13+
}
14+
}
15+
}

testdata/TestHandler.golden testdata/TestHandler/webtest.golden

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"modifiedAt": "2019-09-07T18:11:14.953764663+09:00",
2+
"modifiedAt": "2019-09-07T20:03:26.062536108+09:00",
33
"data": {
44
"request": {
55
"body": {

try.go

-42
This file was deleted.

try/try.go

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package try
2+
3+
import (
4+
"testing"
5+
6+
webtest "github.com/podhmo/go-webtest"
7+
"github.com/podhmo/go-webtest/jsonequal"
8+
"github.com/podhmo/go-webtest/tripperware"
9+
"github.com/podhmo/noerror"
10+
)
11+
12+
// It :
13+
type It struct {
14+
_Required_Sentinel struct{}
15+
16+
Code int
17+
Want *interface{}
18+
ModifyResponse func(res webtest.Response) (got interface{})
19+
}
20+
21+
// With :
22+
func (it It) With(
23+
t *testing.T,
24+
c *webtest.Client,
25+
method string,
26+
path string,
27+
options ...webtest.Option,
28+
) {
29+
t.Helper()
30+
needSnapshot := it.Want != nil
31+
32+
if it.Code != 0 {
33+
options = append(options, webtest.WithTripperware(tripperware.ExpectCode(t, it.Code)))
34+
}
35+
if needSnapshot {
36+
options = append(options, webtest.WithTripperware(tripperware.GetExpectedDataFromSnapshot(t, it.Want)))
37+
}
38+
39+
got, err := c.Do(method, path, options...)
40+
noerror.Must(t, err)
41+
defer func() { noerror.Must(t, got.Close()) }()
42+
43+
if needSnapshot {
44+
noerror.Must(t, noerror.NotEqual(nil).Actual(it.Want))
45+
46+
var actual interface{}
47+
if it.ModifyResponse != nil {
48+
actual = it.ModifyResponse(got)
49+
} else {
50+
actual = got.JSONData()
51+
}
52+
53+
noerror.Should(t,
54+
jsonequal.ShouldBeSame(
55+
jsonequal.From(actual),
56+
jsonequal.From(it.Want),
57+
),
58+
)
59+
}
60+
}

0 commit comments

Comments
 (0)