2019-03-11 16:56:48 +00:00
|
|
|
package internal
|
2018-07-01 00:13:22 +00:00
|
|
|
|
|
|
|
import (
|
2023-05-05 07:49:38 +00:00
|
|
|
"context"
|
2018-07-01 00:13:22 +00:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2023-05-05 07:49:38 +00:00
|
|
|
"net/url"
|
2018-07-01 00:13:22 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2021-11-01 23:52:38 +00:00
|
|
|
func setupTest(t *testing.T, responseBody string, statusCode int) *Client {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
handler := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
2018-07-01 00:13:22 +00:00
|
|
|
w.WriteHeader(statusCode)
|
2021-11-01 23:52:38 +00:00
|
|
|
_, _ = fmt.Fprintln(w, responseBody)
|
|
|
|
})
|
|
|
|
|
|
|
|
server := httptest.NewServer(handler)
|
|
|
|
t.Cleanup(server.Close)
|
|
|
|
|
|
|
|
client, err := NewClient("A", "B")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
client.HTTPClient = server.Client()
|
2023-05-05 07:49:38 +00:00
|
|
|
client.BaseURL, _ = url.Parse(server.URL)
|
2021-11-01 23:52:38 +00:00
|
|
|
|
|
|
|
return client
|
2018-07-01 00:13:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestChangeResourceRecordSets(t *testing.T) {
|
|
|
|
responseBody := `<?xml version="1.0" encoding="UTF-8"?>
|
|
|
|
<ChangeResourceRecordSetsResponse xmlns="https://route53.amazonaws.com/doc/2012-12-12/">
|
|
|
|
<ChangeInfo>
|
|
|
|
<Id>xxxxx</Id>
|
|
|
|
<Status>INSYNC</Status>
|
|
|
|
<SubmittedAt>2015-08-05T00:00:00.000Z</SubmittedAt>
|
|
|
|
</ChangeInfo>
|
|
|
|
</ChangeResourceRecordSetsResponse>
|
|
|
|
`
|
2018-10-12 17:29:18 +00:00
|
|
|
|
2021-11-01 23:52:38 +00:00
|
|
|
client := setupTest(t, responseBody, http.StatusOK)
|
2018-07-01 00:13:22 +00:00
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
res, err := client.ChangeResourceRecordSets(context.Background(), "example.com", ChangeResourceRecordSetsRequest{})
|
2018-07-01 00:13:22 +00:00
|
|
|
require.NoError(t, err)
|
2021-11-01 23:52:38 +00:00
|
|
|
|
2018-07-01 00:13:22 +00:00
|
|
|
assert.Equal(t, "xxxxx", res.ChangeInfo.ID)
|
|
|
|
assert.Equal(t, "INSYNC", res.ChangeInfo.Status)
|
|
|
|
assert.Equal(t, "2015-08-05T00:00:00.000Z", res.ChangeInfo.SubmittedAt)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestChangeResourceRecordSetsErrors(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
responseBody string
|
|
|
|
statusCode int
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "API error",
|
|
|
|
responseBody: `<?xml version="1.0" encoding="UTF-8"?>
|
|
|
|
<ErrorResponse>
|
|
|
|
<Error>
|
|
|
|
<Type>Sender</Type>
|
|
|
|
<Code>AuthFailed</Code>
|
|
|
|
<Message>The request signature we calculated does not match the signature you provided.</Message>
|
|
|
|
</Error>
|
|
|
|
</ErrorResponse>
|
|
|
|
`,
|
|
|
|
statusCode: http.StatusUnauthorized,
|
2023-05-05 07:49:38 +00:00
|
|
|
expected: "Sender(AuthFailed): The request signature we calculated does not match the signature you provided.",
|
2018-07-01 00:13:22 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "response body error",
|
|
|
|
responseBody: "foo",
|
|
|
|
statusCode: http.StatusOK,
|
2023-05-05 07:49:38 +00:00
|
|
|
expected: "unable to unmarshal response: [status code: 200] body: foo error: EOF",
|
2018-07-01 00:13:22 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "error message error",
|
|
|
|
responseBody: "foo",
|
|
|
|
statusCode: http.StatusInternalServerError,
|
2023-05-05 07:49:38 +00:00
|
|
|
expected: "unexpected status code: [status code: 500] body: foo",
|
2018-07-01 00:13:22 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
2021-11-01 23:52:38 +00:00
|
|
|
client := setupTest(t, test.responseBody, test.statusCode)
|
2018-07-01 00:13:22 +00:00
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
res, err := client.ChangeResourceRecordSets(context.Background(), "example.com", ChangeResourceRecordSetsRequest{})
|
2018-07-01 00:13:22 +00:00
|
|
|
assert.Nil(t, res)
|
|
|
|
assert.EqualError(t, err, test.expected)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetChange(t *testing.T) {
|
|
|
|
responseBody := `<?xml version="1.0" encoding="UTF-8"?>
|
|
|
|
<GetChangeResponse xmlns="https://route53.amazonaws.com/doc/2012-12-12/">
|
|
|
|
<ChangeInfo>
|
|
|
|
<Id>xxxxx</Id>
|
|
|
|
<Status>INSYNC</Status>
|
|
|
|
<SubmittedAt>2015-08-05T00:00:00.000Z</SubmittedAt>
|
|
|
|
</ChangeInfo>
|
|
|
|
</GetChangeResponse>
|
|
|
|
`
|
|
|
|
|
2021-11-01 23:52:38 +00:00
|
|
|
client := setupTest(t, responseBody, http.StatusOK)
|
2018-07-01 00:13:22 +00:00
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
res, err := client.GetChange(context.Background(), "12345")
|
2018-07-01 00:13:22 +00:00
|
|
|
require.NoError(t, err)
|
2021-11-01 23:52:38 +00:00
|
|
|
|
2018-07-01 00:13:22 +00:00
|
|
|
assert.Equal(t, "xxxxx", res.ChangeInfo.ID)
|
|
|
|
assert.Equal(t, "INSYNC", res.ChangeInfo.Status)
|
|
|
|
assert.Equal(t, "2015-08-05T00:00:00.000Z", res.ChangeInfo.SubmittedAt)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetChangeErrors(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
responseBody string
|
|
|
|
statusCode int
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "API error",
|
|
|
|
responseBody: `<?xml version="1.0" encoding="UTF-8"?>
|
|
|
|
<ErrorResponse>
|
|
|
|
<Error>
|
|
|
|
<Type>Sender</Type>
|
|
|
|
<Code>AuthFailed</Code>
|
|
|
|
<Message>The request signature we calculated does not match the signature you provided.</Message>
|
|
|
|
</Error>
|
|
|
|
</ErrorResponse>
|
|
|
|
`,
|
|
|
|
statusCode: http.StatusUnauthorized,
|
2023-05-05 07:49:38 +00:00
|
|
|
expected: "Sender(AuthFailed): The request signature we calculated does not match the signature you provided.",
|
2018-07-01 00:13:22 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "response body error",
|
|
|
|
responseBody: "foo",
|
|
|
|
statusCode: http.StatusOK,
|
2023-05-05 07:49:38 +00:00
|
|
|
expected: "unable to unmarshal response: [status code: 200] body: foo error: EOF",
|
2018-07-01 00:13:22 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "error message error",
|
|
|
|
responseBody: "foo",
|
|
|
|
statusCode: http.StatusInternalServerError,
|
2023-05-05 07:49:38 +00:00
|
|
|
expected: "unexpected status code: [status code: 500] body: foo",
|
2018-07-01 00:13:22 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
2021-11-01 23:52:38 +00:00
|
|
|
client := setupTest(t, test.responseBody, test.statusCode)
|
2018-07-01 00:13:22 +00:00
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
res, err := client.GetChange(context.Background(), "12345")
|
2018-07-01 00:13:22 +00:00
|
|
|
assert.Nil(t, res)
|
|
|
|
assert.EqualError(t, err, test.expected)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|