-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocbase_test.go
138 lines (108 loc) · 3.13 KB
/
docbase_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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package docbase
import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"reflect"
"testing"
)
var (
mux *http.ServeMux
server *httptest.Server
client *Client
)
func setup() {
mux = http.NewServeMux()
server = httptest.NewServer(mux)
url, _ := url.Parse(server.URL)
client = NewClient(nil, "dummyTeam", "dummyToken")
client.BaseURL = url
}
func teardown() {
defer server.Close()
}
func testHeader(t *testing.T, r *http.Request, header string, want string) {
if got := r.Header.Get(header); got != want {
t.Errorf("NewRequest() %s header is %v, want %v", header, got, want)
}
}
func testMethod(t *testing.T, r *http.Request, want string) {
if got := r.Method; got != want {
t.Errorf("Request method: %v, want %v", got, want)
}
}
func TestNewClient(t *testing.T) {
cli := NewClient(nil, "fakeTeam", "fakeToken")
if got, want := cli.BaseURL.String(), "https://api.docbase.io/teams/fakeTeam"; got != want {
t.Errorf("NewClient BaseURL is %v, want %v", got, want)
}
if got, want := cli.Client, http.DefaultClient; got != want {
t.Errorf("NewClient Client is %v, want %v", got, want)
}
}
func TestClient_Do(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/sample", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "POST")
http.Error(w, `{
"code": 400,
"status": "bad request"
}`, 400)
})
req, _ := client.NewRequest(http.MethodPost, "/sample", nil)
_, err := client.Do(req, nil)
if err == nil {
t.Error("Do returns with expected error")
}
}
func TestClient_NewRequest(t *testing.T) {
cli := NewClient(nil, "fakeTeam", "fakeToken")
method := "POST"
inURL, outURL := "/foo", "https://api.docbase.io/teams/fakeTeam/foo"
inBody := struct{ Foo string }{Foo: "Bar"}
outBody := `{"Foo":"Bar"}`
req, err := cli.NewRequest(method, inURL, inBody)
if err != nil {
t.Errorf("err")
}
if got, want := req.Method, method; got != want {
t.Errorf("NewRequest(%q) Method is %v, want %v", method, got, want)
}
if got, want := req.URL.String(), outURL; got != want {
t.Errorf("NewRequest(%q) URL is %v, want %v", inURL, got, want)
}
body, _ := ioutil.ReadAll(req.Body)
if got, want := string(body), outBody; got != want {
t.Errorf("NewRequest(%q) Body is %v, want %v", inBody, got, want)
}
testHeader(t, req, "Content-Type", "application/json")
testHeader(t, req, "Accept", "application/json")
testHeader(t, req, "X-DocBaseToken", cli.AccessToken)
testHeader(t, req, "X-Api-Version", apiVersion)
testHeader(t, req, "USER_AGENT", userAgent)
}
func TestDo_httpError(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprint(w, `{
"error": "bad_request",
"messages": [
"Nameを入力してください"
]
}`)
})
req, _ := client.NewRequest("GET", "/", nil)
_, err := client.Do(req, nil)
errResp, ok := err.(*ErrorResponse)
if !ok {
t.Errorf("Error should be of type ErrorResponse but is %v: %+v", reflect.TypeOf(err), err)
}
if got, want := errResp.ErrorStr, "bad_request"; got != want {
t.Errorf("Request Do %v, want %v", got, want)
}
}