forked from billputer/go-namecheap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
namecheap_test.go
85 lines (72 loc) · 1.97 KB
/
namecheap_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
package namecheap
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"testing"
)
var (
mux *http.ServeMux
client *Client
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)
client = NewClient("anApiUser", "anToken", "anUser")
client.BaseURL = server.URL + "/"
}
func fillDefaultParams(p url.Values) url.Values {
p.Set("ApiKey", "anToken")
p.Set("ApiUser", "anApiUser")
p.Set("ClientIp", "127.0.0.1")
p.Set("UserName", "anUser")
return p
}
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 testBody(t *testing.T, r *http.Request, p url.Values) {
defer r.Body.Close()
b, err := ioutil.ReadAll(r.Body)
if err != nil {
t.Errorf("Error reading body: %v", err)
}
if p.Encode() != string(b) {
t.Errorf("Body:\n %v\nwant:\n %v", string(b), p.Encode())
}
}
func TestNewClient(t *testing.T) {
c := NewClient("anApiUser", "anToken", "anUser")
if c.BaseURL != defaultBaseURL {
t.Errorf("NewClient BaseURL = %v, want %v", c.BaseURL, defaultBaseURL)
}
}
// Verify that the MakeRequest function assembles the correct API URL
func TestMakeRequest(t *testing.T) {
c := NewClient("anApiUser", "anToken", "anUser")
c.BaseURL = "https://fake-api-server/"
requestInfo := &ApiRequest{
method: "POST",
command: "namecheap.domains.getList",
params: url.Values{},
}
req, _ := c.makeRequest(requestInfo)
// correctly assembled URL
outURL := "https://fake-api-server/"
// test that URL was correctly assembled
if req.URL.String() != outURL {
t.Errorf("NewRequest() URL = %v, want %v", req.URL, outURL)
}
correctParams := fillDefaultParams(url.Values{})
correctParams.Set("Command", "namecheap.domains.getList")
testBody(t, req, correctParams)
}