2023-05-05 07:49:38 +00:00
|
|
|
package internal
|
2018-10-13 14:01:46 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
const fakeAPIKey = "asdf1234"
|
|
|
|
|
|
|
|
func TestClient_buildQuery(t *testing.T) {
|
2018-10-13 14:01:46 +00:00
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
apiKey string
|
|
|
|
baseURL string
|
|
|
|
action string
|
|
|
|
domain string
|
|
|
|
txt string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "success",
|
|
|
|
apiKey: fakeAPIKey,
|
|
|
|
action: cmdAddRecord,
|
|
|
|
domain: "domain",
|
|
|
|
txt: "TXTtxtTXT",
|
|
|
|
expected: "https://api.dreamhost.com?cmd=dns-add_record&comment=Managed%2BBy%2Blego&format=json&key=asdf1234&record=domain&type=TXT&value=TXTtxtTXT",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "Invalid base URL",
|
|
|
|
apiKey: fakeAPIKey,
|
|
|
|
baseURL: ":",
|
|
|
|
action: cmdAddRecord,
|
|
|
|
domain: "domain",
|
|
|
|
txt: "TXTtxtTXT",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
test := test
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
client := NewClient(test.apiKey)
|
2018-10-13 14:01:46 +00:00
|
|
|
if test.baseURL != "" {
|
2023-05-05 07:49:38 +00:00
|
|
|
client.BaseURL = test.baseURL
|
2018-10-13 14:01:46 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
endpoint, err := client.buildEndpoint(test.action, test.domain, test.txt)
|
2018-10-13 14:01:46 +00:00
|
|
|
|
|
|
|
if test.expected == "" {
|
|
|
|
require.Error(t, err)
|
|
|
|
} else {
|
|
|
|
require.NoError(t, err)
|
2023-05-05 07:49:38 +00:00
|
|
|
assert.Equal(t, test.expected, endpoint.String())
|
2018-10-13 14:01:46 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|