forked from masci/flickr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
response_test.go
118 lines (98 loc) · 2.8 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package flickr
import (
"encoding/xml"
"net/http"
"testing"
flickErr "github.com/masci/flickr/error"
)
func TestFlickrResponse(t *testing.T) {
failure := `<?xml version="1.0" encoding="utf-8" ?>
<rsp stat="fail">
<err code="99" msg="Insufficient permissions. Method requires read privileges; none granted." />
</rsp>
`
resp := FooResponse{}
err := xml.Unmarshal([]byte(failure), &resp)
if err != nil {
t.Error("Error unmarsshalling", failure)
}
Expect(t, resp.HasErrors(), true)
Expect(t, resp.ErrorCode(), 99)
Expect(t, resp.ErrorMsg(), "Insufficient permissions. Method requires read privileges; none granted.")
ok := `<?xml version="1.0" encoding="utf-8" ?>
<rsp stat="ok">
<user id="23148015@N00">
<username>Massimiliano Pippi</username>
</user>
<foo>Foo!</foo>
</rsp>`
resp = FooResponse{}
err = xml.Unmarshal([]byte(ok), &resp)
if err != nil {
t.Error("Error unmarsshalling", ok)
}
Expect(t, resp.HasErrors(), false)
Expect(t, resp.Foo, "Foo!")
Expect(t, resp.ErrorCode(), 0)
Expect(t, resp.ErrorMsg(), "")
resp = FooResponse{}
resp.SetErrorStatus(true)
resp.SetErrorMsg("a message")
resp.SetErrorCode(999)
Expect(t, resp.HasErrors(), true)
Expect(t, resp.ErrorMsg(), "a message")
Expect(t, resp.ErrorCode(), 999)
resp.SetErrorStatus(false)
Expect(t, resp.HasErrors(), false)
}
type FooResponse struct {
BasicResponse
Foo string `xml:"foo"`
}
func TestParseResponse(t *testing.T) {
bodyStr := `<?xml version="1.0" encoding="utf-8" ?>
<rsp stat="ok">
<user id="23148015@N00">
<username>Massimiliano Pippi</username>
</user>
<foo>Foo!</foo>
</rsp>`
flickrResp := &FooResponse{}
response := &http.Response{}
response.Body = NewFakeBody(bodyStr)
err := parseApiResponse(response, flickrResp)
Expect(t, err, nil)
Expect(t, flickrResp.Foo, "Foo!")
response = &http.Response{}
response.Body = NewFakeBody("a_non_rest_format_error")
err = parseApiResponse(response, flickrResp)
ferr, ok := err.(*flickErr.Error)
Expect(t, ok, true)
Expect(t, ferr.ErrorCode, 10)
response = &http.Response{}
response.Body = NewFakeBody(`<?xml version="1.0" encoding="utf-8" ?><rsp stat="fail"></rsp>`)
err = parseApiResponse(response, flickrResp)
//ferr, ok := err.(*flickErr.Error)
//Expect(t, ok, true)
//Expect(t, ferr.ErrorCode, 10)
}
func TestExtra(t *testing.T) {
bodyStr := `<?xml version="1.0" encoding="utf-8" ?>
<rsp stat="ok">
<user id="23148015@N00">
<username>Massimiliano Pippi</username>
</user>
<foo>Foo!</foo>
<brands>
<brand id="canon">Canon</brand>
<brand id="nikon">Nikon</brand>
<brand id="apple">Apple</brand>
</brands>
</rsp>`
flickrResp := &BasicResponse{}
response := &http.Response{}
response.Body = NewFakeBody(bodyStr)
err := parseApiResponse(response, flickrResp)
Expect(t, err, nil)
Expect(t, flickrResp.Extra != "", true)
}