forked from porthos-rpc/porthos-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_response_test.go
75 lines (58 loc) · 1.44 KB
/
client_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
74
75
package porthos
import (
"testing"
)
func TestUnmarshalJSONTo(t *testing.T) {
type Test struct {
A int `json:"a"`
}
res := ClientResponse{
ContentType: "application/json",
Content: []byte("{\"a\": 1}"),
}
var test Test
err := res.UnmarshalJSONTo(&test)
if err != nil {
t.Error("Error with json response:", err)
}
if test.A != 1 {
t.Errorf("Expected json value was 1, got %d", test.A)
}
}
func TestUnmarshalJSONToInvalidContentType(t *testing.T) {
type Test struct {
A int `json:"a"`
}
res := ClientResponse{
ContentType: "text/plain",
Content: []byte("{\"a\": 1}"),
}
var test Test
err := res.UnmarshalJSONTo(&test)
if err == nil || err.Error() != "The content type of the response is not 'application/json'" {
t.Error("Content type error was expected")
}
}
func TestUnmarshalJSON(t *testing.T) {
res := ClientResponse{
ContentType: "application/json",
Content: []byte("{\"a\": 1.0}"),
}
json, err := res.UnmarshalJSON()
if err != nil {
t.Error("Error with json response:", err)
}
if json["a"] != 1.0 {
t.Errorf("Expected json value was 1, got %d", json["a"])
}
}
func TestUnmarshalJSONInvalidContentType(t *testing.T) {
res := ClientResponse{
ContentType: "text/plain",
Content: []byte("{\"a\": 1.0}"),
}
_, err := res.UnmarshalJSON()
if err == nil || err.Error() != "The content type of the response is not 'application/json'" {
t.Error("Content type error was expected")
}
}