Skip to content

Commit

Permalink
bonsai
Browse files Browse the repository at this point in the history
  • Loading branch information
k1LoW committed Oct 10, 2022
1 parent b7e3900 commit 17244ef
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@ import (
)

func TestGet(t *testing.T) {
r := httpstub.NewRouter(t)
r.Method(http.MethodGet).Path("/api/v1/users/1").Header("Content-Type", "application/json").ResponseString(http.StatusOK, `{"name":"alice"}`)
ts := r.Server()
ts := httpstub.NewServer(t)
t.Cleanup(func() {
ts.Close()
})
ts.Method(http.MethodGet).Path("/api/v1/users/1").Header("Content-Type", "application/json").ResponseString(http.StatusOK, `{"name":"alice"}`)
tc := ts.Client()

res, err := tc.Get("https://example.com/api/v1/users/1")
Expand All @@ -42,13 +41,13 @@ func TestGet(t *testing.T) {
if got != want {
t.Errorf("got %v\nwant %v", got, want)
}
if len(r.Requests()) != 1 {
t.Errorf("got %v\nwant %v", len(r.Requests()), 1)
if len(ts.Requests()) != 1 {
t.Errorf("got %v\nwant %v", len(ts.Requests()), 1)
}
}
```

or use `NewServer(t *testing.T)` (is syntax sugar)
or

``` go
package myapp
Expand All @@ -62,11 +61,12 @@ import (
)

func TestGet(t *testing.T) {
ts := httpstub.NewServer(t)
r := httpstub.NewRouter(t)
r.Method(http.MethodGet).Path("/api/v1/users/1").Header("Content-Type", "application/json").ResponseString(http.StatusOK, `{"name":"alice"}`)
ts := r.Server()
t.Cleanup(func() {
ts.Close()
})
ts.Method(http.MethodGet).Path("/api/v1/users/1").Header("Content-Type", "application/json").ResponseString(http.StatusOK, `{"name":"alice"}`)
tc := ts.Client()

res, err := tc.Get("https://example.com/api/v1/users/1")
Expand All @@ -85,8 +85,8 @@ func TestGet(t *testing.T) {
if got != want {
t.Errorf("got %v\nwant %v", got, want)
}
if len(ts.Requests()) != 1 {
t.Errorf("got %v\nwant %v", len(ts.Requests()), 1)
if len(r.Requests()) != 1 {
t.Errorf("got %v\nwant %v", len(r.Requests()), 1)
}
}
```
Expand Down

0 comments on commit 17244ef

Please sign in to comment.