Skip to content

Commit

Permalink
fix: handle different status codes inside bulk
Browse files Browse the repository at this point in the history
  • Loading branch information
cgroschupp committed Nov 26, 2024
1 parent 6fa9e06 commit 05c3b59
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
7 changes: 6 additions & 1 deletion records.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,12 @@ func (s *RecordsService) Bulk(ctx context.Context, method, domainName string, rr

defer func() { _ = resp.Body.Close() }()

if resp.StatusCode != http.StatusOK {
expectedStatusCode := http.StatusOK
if method == http.MethodPost {
expectedStatusCode = http.StatusCreated
}

if resp.StatusCode != expectedStatusCode {
return handleError(resp)
}

Expand Down
15 changes: 12 additions & 3 deletions records_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"
"net/http/httptest"
"os"
"slices"
"testing"
"time"

Expand Down Expand Up @@ -74,12 +75,16 @@ func TestRecordsService_Bulk(t *testing.T) {
client.BaseURL = server.URL

mux.HandleFunc("/domains/example.dedyn.io/rrsets/", func(rw http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodPut {
if !slices.Contains([]string{http.MethodPut, http.MethodPost, http.MethodPatch}, req.Method) {
http.Error(rw, "invalid method", http.StatusMethodNotAllowed)
return
}

rw.WriteHeader(http.StatusOK)
if req.Method == http.MethodPost {
rw.WriteHeader(http.StatusCreated)
} else {
rw.WriteHeader(http.StatusOK)
}
})

record := RRSet{
Expand All @@ -91,7 +96,11 @@ func TestRecordsService_Bulk(t *testing.T) {
TTL: 300,
}

err := client.Records.Bulk(context.Background(), http.MethodPut, "example.dedyn.io", []RRSet{record})
err := client.Records.Bulk(context.Background(), http.MethodPost, "example.dedyn.io", []RRSet{record})
require.NoError(t, err)
err = client.Records.Bulk(context.Background(), http.MethodPut, "example.dedyn.io", []RRSet{record})
require.NoError(t, err)
err = client.Records.Bulk(context.Background(), http.MethodPatch, "example.dedyn.io", []RRSet{record})
require.NoError(t, err)
}

Expand Down

0 comments on commit 05c3b59

Please sign in to comment.