-
-
Notifications
You must be signed in to change notification settings - Fork 993
/
mock.go
163 lines (134 loc) · 4.54 KB
/
mock.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package internal
import (
"fmt"
"io"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
)
const fakeOTCToken = "62244bc21da68d03ebac94e6636ff01f"
func writeFixture(rw http.ResponseWriter, filename string) {
file, err := os.Open(filepath.Join("internal", "fixtures", filename))
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
defer func() { _ = file.Close() }()
_, _ = io.Copy(rw, file)
}
// DNSServerMock mock.
type DNSServerMock struct {
t *testing.T
server *httptest.Server
mux *http.ServeMux
}
// NewDNSServerMock create a new DNSServerMock.
func NewDNSServerMock(t *testing.T) *DNSServerMock {
t.Helper()
mux := http.NewServeMux()
return &DNSServerMock{
t: t,
server: httptest.NewServer(mux),
mux: mux,
}
}
func (m *DNSServerMock) GetServerURL() string {
return m.server.URL
}
// ShutdownServer creates the mock server.
func (m *DNSServerMock) ShutdownServer() {
m.server.Close()
}
// HandleAuthSuccessfully Handle auth successfully.
func (m *DNSServerMock) HandleAuthSuccessfully() {
m.mux.HandleFunc("/v3/auth/token", func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("X-Subject-Token", fakeOTCToken)
_, _ = fmt.Fprintf(w, `{
"token": {
"catalog": [
{
"type": "dns",
"id": "56cd81db1f8445d98652479afe07c5ba",
"name": "",
"endpoints": [
{
"url": "%s",
"region": "eu-de",
"region_id": "eu-de",
"interface": "public",
"id": "0047a06690484d86afe04877074efddf"
}
]
}
]
}}`, m.server.URL)
})
}
// HandleListZonesSuccessfully Handle list zones successfully.
func (m *DNSServerMock) HandleListZonesSuccessfully() {
m.mux.HandleFunc("/v2/zones", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(m.t, http.MethodGet, r.Method)
assert.Equal(m.t, "/v2/zones", r.URL.Path)
assert.Equal(m.t, "name=example.com.", r.URL.RawQuery)
assert.Equal(m.t, "application/json", r.Header.Get("Accept"))
writeFixture(w, "zones_GET.json")
})
}
// HandleListZonesEmpty Handle list zones empty.
func (m *DNSServerMock) HandleListZonesEmpty() {
m.mux.HandleFunc("/v2/zones", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(m.t, http.MethodGet, r.Method)
assert.Equal(m.t, "/v2/zones", r.URL.Path)
assert.Equal(m.t, "name=example.com.", r.URL.RawQuery)
assert.Equal(m.t, "application/json", r.Header.Get("Accept"))
writeFixture(w, "zones_GET_empty.json")
})
}
// HandleDeleteRecordsetsSuccessfully Handle delete recordsets successfully.
func (m *DNSServerMock) HandleDeleteRecordsetsSuccessfully() {
m.mux.HandleFunc("/v2/zones/123123/recordsets/321321", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(m.t, http.MethodDelete, r.Method)
assert.Equal(m.t, "/v2/zones/123123/recordsets/321321", r.URL.Path)
assert.Equal(m.t, "application/json", r.Header.Get("Accept"))
writeFixture(w, "zones-recordsets_DELETE.json")
})
}
// HandleListRecordsetsEmpty Handle list recordsets empty.
func (m *DNSServerMock) HandleListRecordsetsEmpty() {
m.mux.HandleFunc("/v2/zones/123123/recordsets", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(m.t, "/v2/zones/123123/recordsets", r.URL.Path)
assert.Equal(m.t, "name=_acme-challenge.example.com.&type=TXT", r.URL.RawQuery)
writeFixture(w, "zones-recordsets_GET_empty.json")
})
}
// HandleListRecordsetsSuccessfully Handle list recordsets successfully.
func (m *DNSServerMock) HandleListRecordsetsSuccessfully() {
m.mux.HandleFunc("/v2/zones/123123/recordsets", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(m.t, "application/json", r.Header.Get("Accept"))
if r.Method == http.MethodGet {
assert.Equal(m.t, "/v2/zones/123123/recordsets", r.URL.Path)
assert.Equal(m.t, "name=_acme-challenge.example.com.&type=TXT", r.URL.RawQuery)
writeFixture(w, "zones-recordsets_GET.json")
return
}
if r.Method == http.MethodPost {
assert.Equal(m.t, "application/json", r.Header.Get("Content-Type"))
raw, err := io.ReadAll(r.Body)
assert.Nil(m.t, err)
exceptedString := `{
"name": "_acme-challenge.example.com.",
"description": "Added TXT record for ACME dns-01 challenge using lego client",
"type": "TXT",
"ttl": 300,
"records": ["\"w6uP8Tcg6K2QR905Rms8iXTlksL6OD1KOWBxTK7wxPI\""]
}`
assert.JSONEq(m.t, exceptedString, string(raw))
writeFixture(w, "zones-recordsets_POST.json")
return
}
http.Error(w, fmt.Sprintf("Expected method to be 'GET' or 'POST' but got '%s'", r.Method), http.StatusBadRequest)
})
}