-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpstub_example_test.go
48 lines (37 loc) · 1.23 KB
/
httpstub_example_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
package httpstub_test
import (
"encoding/json"
"net/http"
"github.com/jharlap/httpstub"
)
const (
ctJSON = "application/json"
ctXML = "application/xml"
)
func Example() {
ts := httpstub.New().WithDefaultContentType(ctJSON)
defer ts.Close()
// the default status for name requests will be 204 no content, this will match PUT, DELETE, ...
nameEndpoint := ts.Path("/user/*/name").WithStatus(http.StatusNoContent)
// GET overrides the status and body
nameEndpoint.WithMethod("GET").WithBody(`{"id":"a1","name":"Alice"}`).WithStatus(http.StatusOK)
ts.Path("/user/*/xml").WithContentType(ctXML).WithBody(`<user id="a1"><name>Alice</name></user>`)
ts.Path("/user").WithBody(`{"id":"a1","name":"Alice","gender":"f"}`)
resp, err := http.Get(ts.URL + "/user/a1/meep")
if err != nil {
panic("httpstub server misbehaved?")
}
if resp.Header.Get("content-type") != ctJSON {
panic("won't happen: the server respects your content type directions")
}
if resp.StatusCode != http.StatusOK {
panic("nor this: the default status code is OK")
}
var alice struct {
Gender string
}
json.NewDecoder(resp.Body).Decode(&alice)
if alice.Gender != "f" {
panic("note that we requested .../meep. the first matching path prefix was /user")
}
}