-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_test.go
138 lines (102 loc) · 3.46 KB
/
client_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 porkbun
import (
"context"
"errors"
"fmt"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
)
func TestPorkbun_NewClient(t *testing.T) {
var httpClient HTTPClient
httpClient = http.DefaultClient
client := NewClient(&Options{
HttpClient: &httpClient,
ApiKey: "1234",
SecretApiKey: "5678",
UserAgent: "CustomAgent/1",
IPv4Only: true,
})
assert.Equal(t, ipv4OnlyBaseURL, client.baseURL)
assert.Equal(t, "1234", client.apiKey)
assert.Equal(t, "5678", client.secret)
assert.Equal(t, "CustomAgent/1", client.userAgent)
}
func TestPorkbun_NewRequest(t *testing.T) {
client := NewClient(&Options{})
req, _ := client.newRequest("POST", "/somepath", nil)
assert.Equal(t, defaultBaseURL+"/somepath", req.URL.String())
}
func TestPorkbun_NewRequest_UserAgent(t *testing.T) {
client := NewClient(&Options{
UserAgent: "UserAgent/23",
})
req, _ := client.newRequest("POST", "/somepath", nil)
assert.Equal(t, defaultBaseURL+"/somepath", req.URL.String())
assert.Equal(t, "UserAgent/23 "+defaultUserAgent, req.Header.Get("User-Agent"))
}
func TestPorkbun_NewRequest_InvalidMethod(t *testing.T) {
client := NewClient(&Options{})
_, err := client.newRequest("💩", "/", nil)
assert.Error(t, err)
}
func TestPorkbun_MakeRequest_InvalidMethod(t *testing.T) {
client := NewClient(&Options{})
_, err := client.makeRequest(context.Background(), "💩", "/", nil, nil)
assert.Error(t, err)
}
type InvalidObject struct{}
func (o *InvalidObject) MarshalJSON() ([]byte, error) {
return nil, errors.New("Invalid Object")
}
func TestPorkbun_MakeRequest_InvalidPayload(t *testing.T) {
client := NewClient(&Options{})
_, err := client.makeRequest(context.Background(), "POST", "/", &InvalidObject{}, nil)
assert.Error(t, err)
}
func TestPorkbun_MakeRequest_404NotFound(t *testing.T) {
setupMockServer(false)
defer teardownMockServer()
mux.HandleFunc("/test404", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
})
_, err := client.makeRequest(context.Background(), "GET", "/test404", nil, nil)
assert.Error(t, err)
assert.IsType(t, &ErrorResponse{}, err)
}
func TestPorkbun_MakeRequest_500InternalServerError(t *testing.T) {
setupMockServer(false)
defer teardownMockServer()
mux.HandleFunc("/test500", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
})
_, err := client.makeRequest(context.Background(), "GET", "/test500", nil, nil)
assert.Error(t, err)
assert.IsType(t, &ErrorResponse{}, err)
}
func TestPorkbun_MakeRequest_NilObject(t *testing.T) {
setupMockServer(true)
defer teardownMockServer()
mux.HandleFunc("/somepath", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, `{"status":"SUCCESS"}`)
})
resp, err := client.makeRequest(context.Background(), "POST", "/somepath", nil, nil)
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)
}
func TestPorkbun_Request_ReadBodyError(t *testing.T) {
setupMockServer(true)
defer teardownMockServer()
mux.HandleFunc("/somepath", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.(http.Flusher).Flush() // Simulate incomplete response
server.CloseClientConnections()
})
req, err := client.newRequest("POST", "/somepath", nil)
assert.NoError(t, err)
var obj map[string]interface{}
_, err = client.request(context.Background(), req, &obj)
assert.Error(t, err)
assert.Contains(t, err.Error(), "unexpected EOF")
}