Skip to content

Commit 0a7e532

Browse files
committed
test: check request without subdomain
1 parent b16e5e2 commit 0a7e532

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

internal/api/api.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,6 @@ func (u *UpdateApi) HandleUpdateRequest(c echo.Context) error {
5252

5353
subdomains := strings.Split(request.Subdomains, ",")
5454

55-
if subdomains == nil {
56-
subdomains = []string{""}
57-
}
58-
5955
for i := range subdomains {
6056
logger.Debug().Msgf("handling request %d of %d", i+1, len(subdomains))
6157

@@ -82,9 +78,8 @@ func (u *UpdateApi) HandleUpdateRequest(c echo.Context) error {
8278
logger.Info().Int("updates", len(subdomains)).Msg("successfully created")
8379

8480
return c.String(http.StatusOK,
85-
fmt.Sprintf("created %d entries for subdomains %s on %s: %s",
81+
fmt.Sprintf("created %d entries on %s: %s",
8682
len(subdomains),
87-
request.Subdomains,
8883
request.Domain,
8984
request.IP),
9085
)

internal/api/api_test.go

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,33 @@ func TestUpdateEndpointSuccessSingleSubdomain(t *testing.T) {
141141

142142
if assert.NoError(t, updateApi.HandleUpdateRequest(c)) {
143143
assert.Equal(t, http.StatusOK, rec.Code)
144-
assert.Equal(t, "created 1 entries for subdomains bar on foo.com: 10.0.0.1", rec.Body.String())
144+
assert.Equal(t, "created 1 entries on foo.com: 10.0.0.1", rec.Body.String())
145+
}
146+
}
147+
148+
func TestUpdateEndpointSuccessNoSubdomain(t *testing.T) {
149+
e := echo.New()
150+
151+
q := make(url.Values)
152+
q.Set("domain", "foo.com")
153+
q.Set("ip", "10.0.0.1")
154+
q.Set("registrar", "cloudflare")
155+
156+
req := httptest.NewRequest(http.MethodGet, fmt.Sprintf("/?%s", q.Encode()), nil)
157+
rec := httptest.NewRecorder()
158+
c := e.NewContext(req, rec)
159+
160+
cs := mockservices.NewMockDnsUpdateService(t)
161+
cs.On("UpdateRecord", mock.Anything).Return(nil).Once()
162+
163+
sf := mockfactory.NewMockServiceFactory(t)
164+
sf.On("Find", services.Registrar("cloudflare")).Return(cs, nil).Once()
165+
166+
updateApi = NewUpdateApi(sf)
167+
168+
if assert.NoError(t, updateApi.HandleUpdateRequest(c)) {
169+
assert.Equal(t, http.StatusOK, rec.Code)
170+
assert.Equal(t, "created 1 entries on foo.com: 10.0.0.1", rec.Body.String())
145171
}
146172
}
147173

@@ -168,7 +194,7 @@ func TestUpdateEndpointSuccessThreeSubdomains(t *testing.T) {
168194

169195
if assert.NoError(t, updateApi.HandleUpdateRequest(c)) {
170196
assert.Equal(t, http.StatusOK, rec.Code)
171-
assert.Equal(t, "created 3 entries for subdomains foo,bar,baz on foo.com: 10.0.0.1", rec.Body.String())
197+
assert.Equal(t, "created 3 entries on foo.com: 10.0.0.1", rec.Body.String())
172198
}
173199
}
174200

0 commit comments

Comments
 (0)