-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nicovideo_user_response_test.go
71 lines (67 loc) · 1.93 KB
/
nicovideo_user_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
package nico
import (
"context"
"io"
"net/http"
"net/http/httptest"
"testing"
)
func TestGetUserInfo(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
userID := r.URL.Query().Get("user_id")
if userID != "2525" {
_, err := io.WriteString(w, `<?xml version="1.0" encoding="UTF-8"?>
<nicovideo_user_response status="fail"><error><code>NOT_FOUND</code><description>user not found</description></error></nicovideo_user_response>`)
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
return
}
_, err := io.WriteString(w, `<?xml version="1.0" encoding="UTF-8"?>
<nicovideo_user_response status="ok">
<user>
<id>2525</id>
<nickname>foo</nickname>
<thumbnail_url>http://example.com/icon.jpg</thumbnail_url>
</user>
</nicovideo_user_response>`)
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
}))
defer ts.Close()
c := &Client{ceBaseRawurl: ts.URL}
_, err := c.GetUserInfo(context.Background(), 0)
if err == nil {
t.Fatalf("should be fail: %v", err)
}
if err.Error() != "fail: NOT_FOUND: user not found" {
t.Fatalf("want %q but %q", "fail: NOT_FOUND: user not found", err)
}
uie, ok := err.(UserInfoError)
if !ok {
t.Fatalf("should be assertion to UserInfoError: %T", err)
}
if uie.Status != "fail" {
t.Fatalf("want %q but %q", "fail", uie.Status)
}
if uie.Code != "NOT_FOUND" {
t.Fatalf("want %q but %q", "NOT_FOUND", uie.Code)
}
if uie.Description != "user not found" {
t.Fatalf("want %q but %q", "user not found", uie.Description)
}
ui, err := c.GetUserInfo(context.Background(), 2525)
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
if ui.ID != 2525 {
t.Fatalf("want %d but %d", 2525, ui.ID)
}
if ui.Nickname != "foo" {
t.Fatalf("want %q but %q", "foo", ui.Nickname)
}
if ui.ThumbnailURL != "http://example.com/icon.jpg" {
t.Fatalf("want %q but %q", "http://example.com/icon.jpg", ui.ThumbnailURL)
}
}