forked from imroc/req
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponse_test.go
73 lines (63 loc) · 1.87 KB
/
response_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
72
73
package req
import (
"github.com/imroc/req/v3/internal/header"
"github.com/imroc/req/v3/internal/tests"
"net/http"
"testing"
)
type User struct {
Name string `json:"name" xml:"name"`
}
type Message struct {
Message string `json:"message"`
}
func TestUnmarshalJson(t *testing.T) {
var user User
resp, err := tc().R().Get("/json")
assertSuccess(t, resp, err)
err = resp.UnmarshalJson(&user)
tests.AssertNoError(t, err)
tests.AssertEqual(t, "roc", user.Name)
}
func TestUnmarshalXml(t *testing.T) {
var user User
resp, err := tc().R().Get("/xml")
assertSuccess(t, resp, err)
err = resp.UnmarshalXml(&user)
tests.AssertNoError(t, err)
tests.AssertEqual(t, "roc", user.Name)
}
func TestUnmarshal(t *testing.T) {
var user User
resp, err := tc().R().Get("/xml")
assertSuccess(t, resp, err)
err = resp.Unmarshal(&user)
tests.AssertNoError(t, err)
tests.AssertEqual(t, "roc", user.Name)
}
func TestResponseResult(t *testing.T) {
resp, _ := tc().R().SetResult(&User{}).Get("/json")
user, ok := resp.Result().(*User)
if !ok {
t.Fatal("Response.Result() should return *User")
}
tests.AssertEqual(t, "roc", user.Name)
tests.AssertEqual(t, true, resp.TotalTime() > 0)
tests.AssertEqual(t, false, resp.ReceivedAt().IsZero())
}
func TestResponseError(t *testing.T) {
resp, _ := tc().R().SetError(&Message{}).Get("/json?error=yes")
msg, ok := resp.Error().(*Message)
if !ok {
t.Fatal("Response.Error() should return *Message")
}
tests.AssertEqual(t, "not allowed", msg.Message)
}
func TestResponseWrap(t *testing.T) {
resp, err := tc().R().Get("/json")
assertSuccess(t, resp, err)
tests.AssertEqual(t, true, resp.GetStatusCode() == http.StatusOK)
tests.AssertEqual(t, true, resp.GetStatus() == "200 OK")
tests.AssertEqual(t, true, resp.GetHeader(header.ContentType) == header.JsonContentType)
tests.AssertEqual(t, true, len(resp.GetHeaderValues(header.ContentType)) == 1)
}