-
Notifications
You must be signed in to change notification settings - Fork 3
/
main_test.go
49 lines (40 loc) · 888 Bytes
/
main_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
package rclient
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
)
type person struct {
Name string
Age int
}
func readPerson(t *testing.T, r *http.Request) person {
var p person
read(t, r, &p)
return p
}
func newClientAndServer(t *testing.T, handler http.HandlerFunc, options ...ClientOption) (*RestClient, *httptest.Server) {
server := httptest.NewServer(handler)
client := NewRestClient(server.URL, options...)
return client, server
}
func write(t *testing.T, w http.ResponseWriter, status int, body interface{}) {
b, err := json.Marshal(body)
if err != nil {
t.Fatal(err)
}
w.WriteHeader(status)
fmt.Fprintln(w, string(b))
}
func read(t *testing.T, r *http.Request, v interface{}) {
b, err := ioutil.ReadAll(r.Body)
if err != nil {
t.Fatal(err)
}
if err := json.Unmarshal(b, v); err != nil {
t.Fatal(err)
}
}