-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_test.go
112 lines (80 loc) · 2.71 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
package freemyip
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"github.com/stretchr/testify/require"
)
func setupTest(t *testing.T) (*Client, *http.ServeMux) {
t.Helper()
mux := http.NewServeMux()
server := httptest.NewServer(mux)
t.Cleanup(server.Close)
client := New("token", true)
client.baseURL, _ = url.Parse(server.URL)
return client, mux
}
func testHandler(body string) http.HandlerFunc {
return func(rw http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodGet {
http.Error(rw, fmt.Sprintf("unsupported method: %s", req.Method), http.StatusMethodNotAllowed)
return
}
_, err := fmt.Fprintln(rw, body)
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
}
}
func TestClient_UpdateDomain(t *testing.T) {
client, mux := setupTest(t)
mux.HandleFunc("/", testHandler("OK\nIP 81.220.38.196 didn't change. No need to update record."))
_, err := client.UpdateDomain(context.Background(), "example", "")
require.NoError(t, err)
}
func TestClient_UpdateDomain_error(t *testing.T) {
client, mux := setupTest(t)
mux.HandleFunc("/", testHandler("ERROR\nToken not provided"))
_, err := client.UpdateDomain(context.Background(), "example", "")
require.Error(t, err)
}
func TestClient_DeleteDomain(t *testing.T) {
client, mux := setupTest(t)
mux.HandleFunc("/", testHandler("OK\nmessage"))
_, err := client.DeleteDomain(context.Background(), "example")
require.NoError(t, err)
}
func TestClient_DeleteDomain_error(t *testing.T) {
client, mux := setupTest(t)
mux.HandleFunc("/", testHandler("ERROR\nToken not provided"))
_, err := client.DeleteDomain(context.Background(), "example")
require.Error(t, err)
}
func TestClient_EditTXTRecord(t *testing.T) {
client, mux := setupTest(t)
mux.HandleFunc("/", testHandler("OK\nUpdated TXT for domain example.freemyip.com to: value"))
_, err := client.EditTXTRecord(context.Background(), "example", "value")
require.NoError(t, err)
}
func TestClient_EditTXTRecord_error(t *testing.T) {
client, mux := setupTest(t)
mux.HandleFunc("/", testHandler("ERROR\nToken not provided"))
_, err := client.EditTXTRecord(context.Background(), "example", "value")
require.Error(t, err)
}
func TestClient_DeleteTXTRecord(t *testing.T) {
client, mux := setupTest(t)
mux.HandleFunc("/", testHandler("OK\nUpdated TXT for domain example.freemyip.com to: null"))
_, err := client.DeleteTXTRecord(context.Background(), "example")
require.NoError(t, err)
}
func TestClient_DeleteTXTRecord_error(t *testing.T) {
client, mux := setupTest(t)
mux.HandleFunc("/", testHandler("ERROR\nToken not provided"))
_, err := client.DeleteTXTRecord(context.Background(), "example")
require.Error(t, err)
}