lego/providers/dns/joker/internal/svc/client_test.go

89 lines
2.1 KiB
Go
Raw Permalink Normal View History

2020-10-08 14:52:50 +00:00
package svc
import (
2023-05-05 07:49:38 +00:00
"context"
2020-10-08 14:52:50 +00:00
"fmt"
2021-08-25 09:44:11 +00:00
"io"
2020-10-08 14:52:50 +00:00
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/require"
)
2023-05-05 07:49:38 +00:00
func setupTest(t *testing.T) (*Client, *http.ServeMux) {
t.Helper()
2020-10-08 14:52:50 +00:00
mux := http.NewServeMux()
server := httptest.NewServer(mux)
t.Cleanup(server.Close)
2020-10-08 14:52:50 +00:00
2023-05-05 07:49:38 +00:00
client := NewClient("test", "secret")
client.BaseURL = server.URL
client.HTTPClient = server.Client()
return client, mux
}
func TestClient_Send(t *testing.T) {
client, mux := setupTest(t)
2020-10-08 14:52:50 +00:00
mux.HandleFunc("/", 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
}
2021-08-25 09:44:11 +00:00
all, _ := io.ReadAll(req.Body)
2020-10-08 14:52:50 +00:00
if string(all) != "label=_acme-challenge&password=secret&type=TXT&username=test&value=123&zone=example.com" {
http.Error(rw, fmt.Sprintf("invalid request: %q", string(all)), http.StatusBadRequest)
return
}
_, err := rw.Write([]byte("OK: 1 inserted, 0 deleted"))
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
2021-11-03 19:39:12 +00:00
return
2020-10-08 14:52:50 +00:00
}
})
zone := "example.com"
label := "_acme-challenge"
value := "123"
2023-05-05 07:49:38 +00:00
err := client.SendRequest(context.Background(), zone, label, value)
2020-10-08 14:52:50 +00:00
require.NoError(t, err)
}
func TestClient_Send_empty(t *testing.T) {
2023-05-05 07:49:38 +00:00
client, mux := setupTest(t)
2020-10-08 14:52:50 +00:00
mux.HandleFunc("/", 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
}
2021-08-25 09:44:11 +00:00
all, _ := io.ReadAll(req.Body)
2020-10-08 14:52:50 +00:00
if string(all) != "label=_acme-challenge&password=secret&type=TXT&username=test&value=&zone=example.com" {
http.Error(rw, fmt.Sprintf("invalid request: %q", string(all)), http.StatusBadRequest)
return
}
_, err := rw.Write([]byte("OK: 1 inserted, 0 deleted"))
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
2021-11-03 19:39:12 +00:00
return
2020-10-08 14:52:50 +00:00
}
})
zone := "example.com"
label := "_acme-challenge"
value := ""
2023-05-05 07:49:38 +00:00
err := client.SendRequest(context.Background(), zone, label, value)
2020-10-08 14:52:50 +00:00
require.NoError(t, err)
}