2021-02-14 15:33:52 +00:00
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
2021-05-31 21:42:24 +00:00
|
|
|
"context"
|
2021-02-14 15:33:52 +00:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestClient_UpdateTxtRecord(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
code string
|
|
|
|
expected assert.ErrorAssertionFunc
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
code: codeGood,
|
|
|
|
expected: assert.NoError,
|
|
|
|
},
|
|
|
|
{
|
2021-05-24 00:04:42 +00:00
|
|
|
code: codeNoChg + ` "0123456789abcdef"`,
|
2021-02-14 15:33:52 +00:00
|
|
|
expected: assert.NoError,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
code: codeAbuse,
|
|
|
|
expected: assert.Error,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
code: codeBadAgent,
|
|
|
|
expected: assert.Error,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
code: codeBadAuth,
|
|
|
|
expected: assert.Error,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
code: codeNoHost,
|
|
|
|
expected: assert.Error,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
code: codeNotFqdn,
|
|
|
|
expected: assert.Error,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
test := test
|
|
|
|
t.Run(test.code, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
handler := http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
if req.Method != http.MethodPost {
|
|
|
|
http.Error(rw, fmt.Sprintf("unsupported method: %s", req.Method), http.StatusMethodNotAllowed)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := req.ParseForm(); err != nil {
|
|
|
|
http.Error(rw, "failed to parse form data", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.PostForm.Encode() != "hostname=_acme-challenge.example.com&password=secret&txt=foo" {
|
|
|
|
http.Error(rw, "invalid form data", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, _ = rw.Write([]byte(test.code))
|
|
|
|
})
|
|
|
|
|
|
|
|
server := httptest.NewServer(handler)
|
|
|
|
t.Cleanup(server.Close)
|
|
|
|
|
|
|
|
client := NewClient(map[string]string{"example.com": "secret"})
|
|
|
|
client.baseURL = server.URL
|
2023-05-05 07:49:38 +00:00
|
|
|
client.HTTPClient = server.Client()
|
2021-02-14 15:33:52 +00:00
|
|
|
|
2022-10-11 12:40:00 +00:00
|
|
|
err := client.UpdateTxtRecord(context.Background(), "_acme-challenge.example.com", "foo")
|
2021-02-14 15:33:52 +00:00
|
|
|
test.expected(t, err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|