2020-10-08 14:52:50 +00:00
|
|
|
package svc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestClient_Send(t *testing.T) {
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
server := httptest.NewServer(mux)
|
2021-11-01 23:52:38 +00:00
|
|
|
t.Cleanup(server.Close)
|
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
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
client := NewClient("test", "secret")
|
|
|
|
client.BaseURL = server.URL
|
|
|
|
|
|
|
|
zone := "example.com"
|
|
|
|
label := "_acme-challenge"
|
|
|
|
value := "123"
|
|
|
|
|
|
|
|
err := client.Send(zone, label, value)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClient_Send_empty(t *testing.T) {
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
server := httptest.NewServer(mux)
|
2021-11-01 23:52:38 +00:00
|
|
|
t.Cleanup(server.Close)
|
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
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
client := NewClient("test", "secret")
|
|
|
|
client.BaseURL = server.URL
|
|
|
|
|
|
|
|
zone := "example.com"
|
|
|
|
label := "_acme-challenge"
|
|
|
|
value := ""
|
|
|
|
|
|
|
|
err := client.Send(zone, label, value)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|