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

Commit 3215083

Browse files
authored
Query support (#44)
* feat: testclient, Query support * feat: WithQuery() * feat: MustParseQuery() * feat: httpbin, add /get * tests: add tests * feat: show type, if string is same
1 parent 748e29e commit 3215083

File tree

9 files changed

+135
-5
lines changed

9 files changed

+135
-5
lines changed

client.go

+7
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,13 @@ func WithBasePath(basePath string) func(*Config) {
212212
}
213213
}
214214

215+
// WithQuery :
216+
func WithQuery(query url.Values) func(*Config) {
217+
return func(c *Config) {
218+
c.ClientConfig.Query = query
219+
}
220+
}
221+
215222
// WithForm setup as send form-data request
216223
func WithForm(data url.Values) func(*Config) {
217224
return func(c *Config) {

httpbin/handlers.go

+16
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package httpbin
22

33
import (
4+
"encoding/json"
45
"fmt"
56
"net/http"
67
"strconv"
@@ -15,6 +16,21 @@ func WriteError(w http.ResponseWriter, error string, code int) {
1516
fmt.Fprintf(w, `{"message": %q}`, error)
1617
}
1718

19+
// Get : /get
20+
func Get(w http.ResponseWriter, r *http.Request) {
21+
w.Header().Set("Content-Type", "application/json; charset=utf-8")
22+
encoder := json.NewEncoder(w)
23+
d := map[string]interface{}{
24+
"args": r.URL.Query(),
25+
"headers": r.Header,
26+
"url": r.URL.String(),
27+
}
28+
29+
if err := encoder.Encode(d); err != nil {
30+
panic(err)
31+
}
32+
}
33+
1834
// Status : /status/{status}
1935
func Status(w http.ResponseWriter, r *http.Request) {
2036
nodes := strings.Split(strings.TrimSuffix(r.URL.Path, "/"), "/")

httpbin/httpbin.go

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ func Handler() http.Handler {
1414
// BindHandlers :
1515
func BindHandlers(mux *http.ServeMux) {
1616
mux.HandleFunc("/status/", Status)
17+
mux.HandleFunc("/get", Get)
1718
}
1819

1920
func Run(port string, mux http.Handler) error {

httpbin/integration_test.go

+60
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package httpbin_test
22

33
import (
4+
"fmt"
5+
"net/url"
46
"testing"
57

68
webtest "github.com/podhmo/go-webtest"
@@ -108,4 +110,62 @@ func TestUnit(t *testing.T) {
108110

109111
// todo: assertion db check
110112
})
113+
114+
t.Run("get", func(t *testing.T) {
115+
client = client.Bind(
116+
hook.ExpectCode(200),
117+
)
118+
119+
cases := []struct {
120+
path string
121+
query url.Values
122+
expected interface{}
123+
}{
124+
{
125+
path: "/get",
126+
expected: map[string][]string{},
127+
},
128+
{
129+
path: "/get?xxx=111",
130+
expected: map[string][]string{"xxx": []string{"111"}},
131+
},
132+
{
133+
path: "/get?xxx=111",
134+
query: webtest.MustParseQuery("yyy=222"),
135+
expected: map[string][]string{
136+
"xxx": []string{"111"},
137+
"yyy": []string{"222"},
138+
},
139+
},
140+
{
141+
path: "/get?xxx=111",
142+
query: webtest.MustParseQuery("yyy=222&xxx=333"),
143+
expected: map[string][]string{
144+
"xxx": []string{"333", "111"},
145+
"yyy": []string{"222"},
146+
},
147+
},
148+
}
149+
for i, c := range cases {
150+
c := c
151+
t.Run(fmt.Sprintf("case%d", i), func(t *testing.T) {
152+
var options []func(*webtest.Config)
153+
if c.query != nil {
154+
options = append(options, webtest.WithQuery(c.query))
155+
}
156+
got, _, teardown := client.Do(t, "GET", c.path, options...)
157+
defer teardown()
158+
159+
var data map[string]interface{}
160+
noerror.Must(t, got.ParseJSONData(&data))
161+
162+
noerror.Should(t,
163+
jsonequal.ShouldBeSame(
164+
jsonequal.From(data["args"]),
165+
jsonequal.From(c.expected),
166+
),
167+
)
168+
})
169+
}
170+
})
111171
}

jsonequal/jsonequal.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,12 @@ func defaultFail(
139139
lb []byte,
140140
rb []byte,
141141
) error {
142+
ls, rs := string(lb), string(rb)
143+
if ls == rs {
144+
msg := "not equal json\nleft (%[1]T):\n %[3]s\nright (%[2]T):\n %[4]s"
145+
return fmt.Errorf(msg, left, right, ls, rs)
146+
}
142147
// todo : more redable expression
143-
msg := "not equal json\nleft:\n %s\nright:\n %s"
144-
return fmt.Errorf(msg, string(lb), string(rb))
148+
msg := "not equal json\nleft:\n %[3]s\nright:\n %[4]s"
149+
return fmt.Errorf(msg, left, right, ls, rs)
145150
}

testclient/config.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
package testclient
22

3-
import "io"
3+
import (
4+
"io"
5+
"net/url"
6+
)
47

58
// Config :
69
type Config struct {
710
BasePath string
811
Body io.Reader
12+
Query url.Values
913

1014
Decorator RoundTripperDecorator
1115
}

testclient/recorder_client.go

+12-1
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,16 @@ func (c *RecorderClient) NewRequest(
5050
config *Config,
5151
) (*http.Request, error) {
5252
url := internal.URLJoin(config.BasePath, path)
53-
return httptest.NewRequest(method, url, config.Body), nil
53+
req := httptest.NewRequest(method, url, config.Body)
54+
55+
if config.Query != nil {
56+
q := config.Query
57+
for k, vs := range req.URL.Query() {
58+
for _, v := range vs {
59+
q.Add(k, v)
60+
}
61+
}
62+
req.URL.RawQuery = q.Encode()
63+
}
64+
return req, nil
5465
}

testclient/server_client.go

+15-1
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,19 @@ func (c *ServerClient) NewRequest(
4848
config *Config,
4949
) (*http.Request, error) {
5050
url := internal.URLJoin(c.Server.URL, internal.URLJoin(config.BasePath, path))
51-
return http.NewRequest(method, url, config.Body)
51+
req, err := http.NewRequest(method, url, config.Body)
52+
if err != nil {
53+
return req, err
54+
}
55+
56+
if config.Query != nil {
57+
q := config.Query
58+
for k, vs := range req.URL.Query() {
59+
for _, v := range vs {
60+
q.Add(k, v)
61+
}
62+
}
63+
req.URL.RawQuery = q.Encode()
64+
}
65+
return req, nil
5266
}

util.go

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package webtest
2+
3+
import "net/url"
4+
5+
// MustParseQuery :
6+
func MustParseQuery(query string) url.Values {
7+
vals, err := url.ParseQuery(query)
8+
if err != nil {
9+
panic(err)
10+
}
11+
return vals
12+
}

0 commit comments

Comments
 (0)