This repository has been archived by the owner on Aug 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
/
dnspod_test.go
108 lines (84 loc) · 2.5 KB
/
dnspod_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
package dnspod
import (
"encoding/json"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"reflect"
"testing"
)
var (
// mux is the HTTP request multiplexer used with the test server.
mux *http.ServeMux
// client is the dnspod client being tested.
client *Client
// server is a test HTTP server used to provide mock API responses.
server *httptest.Server
)
// This method of testing http client APIs is borrowed from
// Will Norris's work in go-github @ https://github.com/google/go-github
func setup() {
mux = http.NewServeMux()
server = httptest.NewServer(mux)
params := CommonParams{LoginToken: "dnspod login token"}
client = NewClient(params)
client.BaseURL = server.URL + "/"
}
func teardown() {
server.Close()
}
func testMethod(t *testing.T, r *http.Request, want string) {
if want != r.Method {
t.Errorf("Request method = %v, want %v", r.Method, want)
}
}
func testRequestJSON(t *testing.T, r *http.Request, values map[string]interface{}) {
var dat map[string]interface{}
body, _ := ioutil.ReadAll(r.Body)
if err := json.Unmarshal(body, &dat); err != nil {
t.Errorf("Could not decode json body: %v", err)
}
if !reflect.DeepEqual(values, dat) {
t.Errorf("Request parameters = %v, want %v", dat, values)
}
}
type values map[string]string
func testFormValues(t *testing.T, r *http.Request, values values) {
want := url.Values{}
for k, v := range values {
want.Add(k, v)
}
r.ParseForm()
if !reflect.DeepEqual(want, r.Form) {
t.Errorf("Request parameters = %v, want %v", r.Form, want)
}
}
func testString(t *testing.T, test, value, want string) {
if value != want {
t.Errorf("%s returned %+v, want %+v", test, value, want)
}
}
func TestNewClient(t *testing.T) {
params := CommonParams{LoginToken: "dnspod login token"}
c := NewClient(params)
if c.BaseURL != baseURL {
t.Errorf("NewClient BaseURL = %v, want %v", c.BaseURL, baseURL)
}
}
func TestNewRequest(t *testing.T) {
params := CommonParams{LoginToken: "dnspod login token"}
c := NewClient(params)
c.BaseURL = "https://go.example.com/"
inURL, outURL := "foo", "https://go.example.com/foo"
req, _ := c.NewRequest("GET", inURL, nil)
// test that relative URL was expanded with the proper BaseURL
if req.URL.String() != outURL {
t.Errorf("makeRequest(%v) URL = %v, want %v", inURL, req.URL, outURL)
}
// test that default user-agent is attached to the request
userAgent := req.Header.Get("User-Agent")
if c.UserAgent != userAgent {
t.Errorf("makeRequest() User-Agent = %v, want %v", userAgent, c.UserAgent)
}
}