lego/providers/dns/lightsail/mock_server_test.go

45 lines
929 B
Go
Raw Normal View History

2019-03-11 16:56:48 +00:00
package lightsail
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/stretchr/testify/require"
)
2020-05-08 17:35:25 +00:00
// MockResponse represents a predefined response used by a mock server.
type MockResponse struct {
StatusCode int
Body string
}
2020-12-28 22:39:00 +00:00
func newMockServer(t *testing.T, responses map[string]MockResponse) string {
t.Helper()
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path
resp, ok := responses[path]
if !ok {
msg := fmt.Sprintf("Requested path not found in response map: %s", path)
require.FailNow(t, msg)
}
w.Header().Set("Content-Type", "application/xml")
w.WriteHeader(resp.StatusCode)
_, err := w.Write([]byte(resp.Body))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}))
2020-12-28 22:39:00 +00:00
t.Cleanup(server.Close)
time.Sleep(100 * time.Millisecond)
2020-12-28 22:39:00 +00:00
return server.URL
}