Skip to content

Commit 0eac4b3

Browse files
authored
tests: improve function naming (#2586)
1 parent 8b40479 commit 0eac4b3

File tree

31 files changed

+103
-102
lines changed

31 files changed

+103
-102
lines changed

platform/tester/servermock/handler_file.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ type ResponseFromFileHandler struct {
1515
filename string
1616
}
1717

18+
// ResponseFromFile creates a [ResponseFromFileHandler] using a filename.
1819
func ResponseFromFile(filename string) *ResponseFromFileHandler {
1920
return &ResponseFromFileHandler{
2021
statusCode: http.StatusOK,
@@ -23,10 +24,16 @@ func ResponseFromFile(filename string) *ResponseFromFileHandler {
2324
}
2425
}
2526

27+
// ResponseFromFixture creates a [ResponseFromFileHandler] using a filename from the `fixtures` directory.
2628
func ResponseFromFixture(filename string) *ResponseFromFileHandler {
2729
return ResponseFromFile(filepath.Join("fixtures", filename))
2830
}
2931

32+
// ResponseFromInternal creates a [ResponseFromFileHandler] using a filename from the `internal/fixtures` directory.
33+
func ResponseFromInternal(filename string) *ResponseFromFileHandler {
34+
return ResponseFromFile(filepath.Join("internal", "fixtures", filename))
35+
}
36+
3037
func (h *ResponseFromFileHandler) ServeHTTP(rw http.ResponseWriter, _ *http.Request) {
3138
for k, values := range h.headers {
3239
for _, v := range values {

platform/tester/servermock/link_request_body.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@ func CheckRequestBodyFromFile(filename string) *RequestBodyLink {
2727
return &RequestBodyLink{filename: filename}
2828
}
2929

30+
// CheckRequestBodyFromFixture creates a [RequestBodyLink] initialized with the provided request body file from the `fixtures` directory.
31+
func CheckRequestBodyFromFixture(filename string) *RequestBodyLink {
32+
return CheckRequestBodyFromFile(filepath.Join("fixtures", filename))
33+
}
34+
35+
// CheckRequestBodyFromInternal creates a [RequestBodyLink] initialized with the provided request body file from the `internal/fixtures directory.
36+
func CheckRequestBodyFromInternal(filename string) *RequestBodyLink {
37+
return CheckRequestBodyFromFile(filepath.Join("internal", "fixtures", filename))
38+
}
39+
3040
func (l *RequestBodyLink) Bind(next http.Handler) http.Handler {
3141
return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
3242
if req.ContentLength == 0 {
@@ -45,7 +55,7 @@ func (l *RequestBodyLink) Bind(next http.Handler) http.Handler {
4555
expectedRaw := slices.Clone(l.body)
4656

4757
if l.filename != "" {
48-
expectedRaw, err = os.ReadFile(filepath.Join("fixtures", l.filename))
58+
expectedRaw, err = os.ReadFile(l.filename)
4959
if err != nil {
5060
http.Error(rw, err.Error(), http.StatusInternalServerError)
5161
return

platform/tester/servermock/link_request_body_json.go

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@ import (
1414

1515
// RequestBodyJSONLink validates JSON request bodies.
1616
type RequestBodyJSONLink struct {
17-
body []byte
18-
filename string
19-
directory string
20-
data any
17+
body []byte
18+
filename string
19+
data any
2120
}
2221

2322
// CheckRequestJSONBody creates a [RequestBodyJSONLink] initialized with a string.
@@ -33,11 +32,20 @@ func CheckRequestJSONBodyFromStruct(data any) *RequestBodyJSONLink {
3332
// CheckRequestJSONBodyFromFile creates a [RequestBodyJSONLink] initialized with the provided request body file.
3433
func CheckRequestJSONBodyFromFile(filename string) *RequestBodyJSONLink {
3534
return &RequestBodyJSONLink{
36-
filename: filename,
37-
directory: "fixtures",
35+
filename: filename,
3836
}
3937
}
4038

39+
// CheckRequestJSONBodyFromFixture creates a [RequestBodyJSONLink] initialized with the provided request body file from the `fixtures` directory.
40+
func CheckRequestJSONBodyFromFixture(filename string) *RequestBodyJSONLink {
41+
return CheckRequestJSONBodyFromFile(filepath.Join("fixtures", filename))
42+
}
43+
44+
// CheckRequestJSONBodyFromInternal creates a [RequestBodyJSONLink] initialized with the provided request body file from the `internal/fixtures` directory.
45+
func CheckRequestJSONBodyFromInternal(filename string) *RequestBodyJSONLink {
46+
return CheckRequestJSONBodyFromFile(filepath.Join("internal", "fixtures", filename))
47+
}
48+
4149
func (l *RequestBodyJSONLink) Bind(next http.Handler) http.Handler {
4250
return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
4351
if req.ContentLength == 0 {
@@ -59,7 +67,7 @@ func (l *RequestBodyJSONLink) Bind(next http.Handler) http.Handler {
5967

6068
switch {
6169
case l.filename != "":
62-
expectedRaw, err = os.ReadFile(filepath.Join(l.directory, l.filename))
70+
expectedRaw, err = os.ReadFile(l.filename)
6371
if err != nil {
6472
http.Error(rw, err.Error(), http.StatusInternalServerError)
6573
return
@@ -101,9 +109,3 @@ func (l *RequestBodyJSONLink) Bind(next http.Handler) http.Handler {
101109
next.ServeHTTP(rw, req)
102110
})
103111
}
104-
105-
func (l *RequestBodyJSONLink) WithDirectory(directory string) *RequestBodyJSONLink {
106-
l.directory = directory
107-
108-
return l
109-
}

providers/dns/acmedns/internal/http_storage_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func TestHTTPStorage_FetchAll_error(t *testing.T) {
9898
func TestHTTPStorage_Put(t *testing.T) {
9999
storage := mockBuilder().
100100
Route("POST /example.com", nil,
101-
servermock.CheckRequestJSONBodyFromFile("request-body.json")).
101+
servermock.CheckRequestJSONBodyFromFixture("request-body.json")).
102102
Build(t)
103103

104104
account := goacmedns.Account{
@@ -137,7 +137,7 @@ func TestHTTPStorage_Put_CNAME_created(t *testing.T) {
137137
Route("POST /example.com",
138138
servermock.Noop().
139139
WithStatusCode(http.StatusCreated),
140-
servermock.CheckRequestJSONBodyFromFile("request-body.json")).
140+
servermock.CheckRequestJSONBodyFromFixture("request-body.json")).
141141
Build(t)
142142

143143
account := goacmedns.Account{

providers/dns/allinkl/internal/client_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func setupClient(server *httptest.Server) (*Client, error) {
2020
func TestClient_GetDNSSettings(t *testing.T) {
2121
client := servermock.NewBuilder[*Client](setupClient).
2222
Route("POST /", servermock.ResponseFromFixture("get_dns_settings.xml"),
23-
servermock.CheckRequestBodyFromFile("get_dns_settings-request.xml").
23+
servermock.CheckRequestBodyFromFixture("get_dns_settings-request.xml").
2424
IgnoreWhitespace()).
2525
Build(t)
2626

@@ -99,7 +99,7 @@ func TestClient_GetDNSSettings(t *testing.T) {
9999
func TestClient_AddDNSSettings(t *testing.T) {
100100
client := servermock.NewBuilder[*Client](setupClient).
101101
Route("POST /", servermock.ResponseFromFixture("add_dns_settings.xml"),
102-
servermock.CheckRequestBodyFromFile("add_dns_settings-request.xml").
102+
servermock.CheckRequestBodyFromFixture("add_dns_settings-request.xml").
103103
IgnoreWhitespace()).
104104
Build(t)
105105

@@ -119,7 +119,7 @@ func TestClient_AddDNSSettings(t *testing.T) {
119119
func TestClient_DeleteDNSSettings(t *testing.T) {
120120
client := servermock.NewBuilder[*Client](setupClient).
121121
Route("POST /", servermock.ResponseFromFixture("delete_dns_settings.xml"),
122-
servermock.CheckRequestBodyFromFile("delete_dns_settings-request.xml").
122+
servermock.CheckRequestBodyFromFixture("delete_dns_settings-request.xml").
123123
IgnoreWhitespace()).
124124
Build(t)
125125

providers/dns/arvancloud/internal/client_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func TestClient_CreateRecord(t *testing.T) {
4848
Route("POST /cdn/4.0/domains/"+domain+"/dns-records",
4949
servermock.ResponseFromFixture("create_txt_record.json").
5050
WithStatusCode(http.StatusCreated),
51-
servermock.CheckRequestJSONBodyFromFile("create_record-request.json")).
51+
servermock.CheckRequestJSONBodyFromFixture("create_record-request.json")).
5252
Build(t)
5353

5454
record := DNSRecord{

providers/dns/autodns/internal/client_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func TestClient_AddTxtRecords(t *testing.T) {
2828
client := mockBuilder().
2929
Route("POST /zone/example.com/_stream",
3030
servermock.ResponseFromFixture("add_record.json"),
31-
servermock.CheckRequestJSONBodyFromFile("add_record-request.json"),
31+
servermock.CheckRequestJSONBodyFromFixture("add_record-request.json"),
3232
servermock.CheckHeader().
3333
With("X-Domainrobot-Context", "123")).
3434
Build(t)
@@ -58,7 +58,7 @@ func TestClient_RemoveTXTRecords(t *testing.T) {
5858
client := mockBuilder().
5959
Route("POST /zone/example.com/_stream",
6060
servermock.ResponseFromFixture("remove_record.json"),
61-
servermock.CheckRequestJSONBodyFromFile("remove_record-request.json"),
61+
servermock.CheckRequestJSONBodyFromFixture("remove_record-request.json"),
6262
servermock.CheckHeader().
6363
With("X-Domainrobot-Context", "123")).
6464
Build(t)

providers/dns/checkdomain/internal/client_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func TestClient_CheckNameservers(t *testing.T) {
5959
func TestClient_CreateRecord(t *testing.T) {
6060
client := mockBuilder().
6161
Route("POST /v1/domains/1/nameservers/records", nil,
62-
servermock.CheckRequestJSONBodyFromFile("create_record-request.json")).
62+
servermock.CheckRequestJSONBodyFromFixture("create_record-request.json")).
6363
Build(t)
6464

6565
record := &Record{
@@ -110,7 +110,7 @@ func TestClient_DeleteTXTRecord(t *testing.T) {
110110
},
111111
})).
112112
Route("PUT /v1/domains/1/nameservers/records", nil,
113-
servermock.CheckRequestJSONBodyFromFile("delete_txt_record-request.json")).
113+
servermock.CheckRequestJSONBodyFromFixture("delete_txt_record-request.json")).
114114
Build(t)
115115

116116
info := dns01.GetChallengeInfo(domainName, "abc")

providers/dns/civo/civo_test.go

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"fmt"
55
"net/http/httptest"
66
"net/url"
7-
"path/filepath"
87
"testing"
98
"time"
109

@@ -155,14 +154,13 @@ func TestDNSProvider_Present(t *testing.T) {
155154
provider := mockBuilder().
156155
// https://www.civo.com/api/dns#list-domain-names
157156
Route("GET /dns",
158-
responseFromFixture("list_domain_names.json"),
157+
servermock.ResponseFromInternal("list_domain_names.json"),
159158
servermock.CheckQueryParameter().Strict().
160159
With("region", "LON1")).
161160
// https://www.civo.com/api/dns#create-a-new-dns-record
162161
Route("POST /dns/7088fcea-7658-43e6-97fa-273f901978fd/records",
163-
responseFromFixture("create_dns_record.json"),
164-
servermock.CheckRequestJSONBodyFromFile("create_dns_record-request.json").
165-
WithDirectory(filepath.Join("internal", "fixtures"))).
162+
servermock.ResponseFromInternal("create_dns_record.json"),
163+
servermock.CheckRequestJSONBodyFromInternal("create_dns_record-request.json")).
166164
Build(t)
167165

168166
err := provider.Present("example.com", "abd", "123d==")
@@ -173,25 +171,21 @@ func TestDNSProvider_CleanUp(t *testing.T) {
173171
provider := mockBuilder().
174172
// https://www.civo.com/api/dns#list-domain-names
175173
Route("GET /dns",
176-
responseFromFixture("list_domain_names.json"),
174+
servermock.ResponseFromInternal("list_domain_names.json"),
177175
servermock.CheckQueryParameter().
178176
With("region", "LON1")).
179177
// https://www.civo.com/api/dns#list-dns-records
180178
Route("GET /dns/7088fcea-7658-43e6-97fa-273f901978fd/records",
181-
responseFromFixture("list_dns_records.json"),
179+
servermock.ResponseFromInternal("list_dns_records.json"),
182180
servermock.CheckQueryParameter().Strict().
183181
With("region", "LON1")).
184182
// https://www.civo.com/api/dns#deleting-a-dns-record
185183
Route("DELETE /dns/edc5dacf-a2ad-4757-41ee-c12f06259c70/records/76cc107f-fbef-4e2b-b97f-f5d34f4075d3",
186-
responseFromFixture("delete_dns_record.json"),
184+
servermock.ResponseFromInternal("delete_dns_record.json"),
187185
servermock.CheckQueryParameter().Strict().
188186
With("region", "LON1")).
189187
Build(t)
190188

191189
err := provider.CleanUp("example.com", "abd", "123d==")
192190
require.NoError(t, err)
193191
}
194-
195-
func responseFromFixture(filename string) *servermock.ResponseFromFileHandler {
196-
return servermock.ResponseFromFile(filepath.Join("internal", "fixtures", filename))
197-
}

providers/dns/civo/internal/client_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func TestClient_CreateDNSRecord(t *testing.T) {
108108
client := mockBuilder().
109109
Route("POST /dns/7088fcea-7658-43e6-97fa-273f901978fd/records",
110110
servermock.ResponseFromFixture("create_dns_record.json"),
111-
servermock.CheckRequestJSONBodyFromFile("create_dns_record-request.json")).
111+
servermock.CheckRequestJSONBodyFromFixture("create_dns_record-request.json")).
112112
Build(t)
113113

114114
record := Record{

0 commit comments

Comments
 (0)