forked from minchao/go-mitake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mitake_test.go
144 lines (114 loc) · 3.21 KB
/
mitake_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
139
140
141
142
143
144
package mitake
import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"reflect"
"strings"
"testing"
)
var (
// mux is the HTTP request multiplexer used with the test server.
mux *http.ServeMux
// client is the eztalk client being tested.
client *Client
// server is a test HTTP server used to provide mock API responses.
server *httptest.Server
)
func setup() {
// test server
mux = http.NewServeMux()
server = httptest.NewServer(mux)
// mitake client configured to use test server
baseURL, _ := url.Parse(server.URL)
client = NewClient("username", "password", nil)
client.BaseURL = baseURL
}
// teardown closes the test HTTP server.
func teardown() {
server.Close()
}
func testMethod(t *testing.T, r *http.Request, want string) {
if got := r.Method; got != want {
t.Errorf("Request method is %v, want %v", got, want)
}
}
func testINI(t *testing.T, r *http.Request, want string) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
t.Errorf("Request parameters error: %v", err)
}
defer r.Body.Close()
if got := string(body); got != want {
t.Errorf("Request parameters is %v, want %v", got, want)
}
}
func TestNewClient(t *testing.T) {
c := NewClient("username", "password", nil)
if got, want := c.BaseURL.String(), defaultBaseURL; got != want {
t.Errorf("NewClient BaseURL is %v, want %v", got, want)
}
}
func TestClient_NewRequest(t *testing.T) {
c := NewClient("username", "password", nil)
inURL, outURL := "/foo", defaultBaseURL+"foo"
inBody, outBody := "Hello, 世界", "Hello, 世界"
req, _ := c.NewRequest("GET", inURL, strings.NewReader(inBody))
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)
}
if got, want := req.Header.Get("User-Agent"), c.UserAgent; got != want {
t.Errorf("NewRequest() User-Agent is %v, want %v", got, want)
}
}
func TestClient_Do(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if m := "GET"; m != r.Method {
t.Errorf("Request method is %v, want %v", r.Method, m)
}
fmt.Fprint(w, "Hello, 世界")
})
req, _ := client.NewRequest("GET", "/", nil)
resp, err := client.Do(req)
if err != nil {
t.Errorf("Do returned unexpected error: %v", err)
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
want := "Hello, 世界"
if !reflect.DeepEqual(string(body), want) {
t.Errorf("Response body is %s, want %s", body, want)
}
}
func TestClient_Do_httpError(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Bad Request", 400)
})
req, _ := client.NewRequest("GET", "/", nil)
_, err := client.Do(req)
if err == nil {
t.Error("Expected HTTP 400 error.")
}
}
func TestClient_Do_noContent(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "")
})
req, _ := client.NewRequest("GET", "/", nil)
_, err := client.Do(req)
if err == nil {
t.Error("Expected empty body error.")
}
}