-
Notifications
You must be signed in to change notification settings - Fork 6
/
request_test.go
71 lines (60 loc) · 1.47 KB
/
request_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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package restit_test
import (
"encoding/json"
"io/ioutil"
"math/rand"
"net/http"
"net/http/httptest"
"testing"
"time"
restit "github.com/go-restit/restit/v2"
)
func TestRequest(t *testing.T) {
// Post is the type for test
type Post struct {
ID string
Seq int
OwnerID string
Created time.Time
Updated time.Time
}
// repeater implements of http.HandlerFunc interace
repeater := func(w http.ResponseWriter, r *http.Request) {
if r == nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("nil request"))
return
}
b, err := ioutil.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
w.Write(b)
}
p1 := Post{
Seq: rand.Int(),
OwnerID: RandString(10),
}
r, err := restit.NewRequest("GET", "/foo/bar", p1)
if err != nil {
t.Errorf("unexpected error: %#v", err.Error())
}
w := httptest.NewRecorder()
repeater(w, r)
if want, have := http.StatusOK, w.Code; want != have {
t.Errorf("expected %#v, got %#v", want, have)
}
if b, err := ioutil.ReadAll(w.Body); err != nil {
t.Errorf("unexpected error: %#v", err.Error())
} else if len(b) == 0 {
t.Error("empty w.Body")
} else if j, err := json.Marshal(p1); err != nil {
t.Errorf("unexpected error: %#v", err.Error())
} else if len(j) == 0 {
t.Error("empty json string j")
} else if want, have := string(j), string(b); want != have {
t.Errorf("\nexpected: %s\ngot: %s", want, have)
}
}