2021-04-25 11:37:35 +02:00
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
2023-05-05 09:49:38 +02:00
|
|
|
"context"
|
2021-04-25 11:37:35 +02:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2023-05-05 09:49:38 +02:00
|
|
|
func setupTest(t *testing.T, body string) *Client {
|
2021-04-25 11:37:35 +02:00
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
server := httptest.NewServer(mux)
|
|
|
|
t.Cleanup(server.Close)
|
|
|
|
|
|
|
|
mux.HandleFunc("/host", func(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
_, _ = fmt.Fprintln(rw, body)
|
|
|
|
})
|
|
|
|
|
|
|
|
client, err := NewClient("foo", "secret")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
client.baseURL = server.URL
|
|
|
|
|
|
|
|
return client
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClient_SetRecord(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
response string
|
|
|
|
assert require.ErrorAssertionFunc
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "success",
|
|
|
|
response: `{"message":"OK","result":200}`,
|
|
|
|
assert: require.NoError,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "failure",
|
|
|
|
response: `{"message":"Not Found : the information you requested was not found.","result":404}`,
|
|
|
|
assert: require.Error,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2023-05-05 09:49:38 +02:00
|
|
|
client := setupTest(t, test.response)
|
2021-04-25 11:37:35 +02:00
|
|
|
|
2023-05-05 09:49:38 +02:00
|
|
|
err := client.SetRecord(context.Background(), "example.com", "txttxttxt", 10)
|
2021-04-25 11:37:35 +02:00
|
|
|
test.assert(t, err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|