Skip to content

Commit

Permalink
Test both JSON and string error responses
Browse files Browse the repository at this point in the history
  • Loading branch information
joeig committed Jul 26, 2024
1 parent f72db53 commit c0427d6
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions powerdns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,15 @@ func initialisePowerDNSTestClient() *Client {
}

func registerDoMockResponder() {
httpmock.RegisterResponder("GET", fmt.Sprintf("%s/servers/doesntExist", generateTestAPIURL()),
httpmock.RegisterResponder("GET", fmt.Sprintf("%s/servers/doesnt-exist", generateTestAPIURL()),
func(req *http.Request) (*http.Response, error) {
if res := verifyAPIKey(req); res != nil {
return res, nil
}
return httpmock.NewStringResponse(http.StatusNotFound, "Not Found"), nil
},
)

httpmock.RegisterResponder("GET", fmt.Sprintf("%s/servers/localhost", generateTestAPIURL()),
func(req *http.Request) (*http.Response, error) {
return verifyAPIKey(req), nil
Expand Down Expand Up @@ -109,7 +110,7 @@ func TestDo(t *testing.T) {

t.Run("TestStringErrorResponse", func(t *testing.T) {
p := initialisePowerDNSTestClient()
req, _ := p.newRequest(context.Background(), "GET", "servers/doesntExist", nil, nil)
req, _ := p.newRequest(context.Background(), "GET", "servers/doesnt-exist", nil, nil)
if _, err := p.do(req, nil); err == nil {
t.Error("err is nil")
}
Expand All @@ -124,7 +125,17 @@ func TestDo(t *testing.T) {
})
t.Run("TestErrorHandling", func(t *testing.T) {
p := initialisePowerDNSTestClient()
req, _ := p.newRequest(context.Background(), "GET", "servers/doesntExist", nil, nil)
req, _ := p.newRequest(context.Background(), "GET", "servers/doesnt-exist", nil, nil)
_, err := p.do(req, nil)
wantResultBeforePowerDNSAuth49 := "Not Found"
wantResultFromPowerDNSAuth49 := "Method Not Allowed"
if err.Error() != wantResultBeforePowerDNSAuth49 && err.Error() != wantResultFromPowerDNSAuth49 {
t.Error("Error response does not result into an error with correct message.", err.Error())
}
})
t.Run("TestJSONErrorHandling", func(t *testing.T) {
p := initialisePowerDNSTestClient()
req, _ := p.newRequest(context.Background(), "GET", "server", nil, nil)
_, err := p.do(req, nil)
wantResultBeforePowerDNSAuth49 := "Not Found"
wantResultFromPowerDNSAuth49 := "Method Not Allowed"
Expand Down

0 comments on commit c0427d6

Please sign in to comment.