2019-03-11 18:56:48 +02:00
|
|
|
package route53
|
2016-03-26 05:34:31 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2020-05-08 19:35:25 +02:00
|
|
|
// MockResponse represents a predefined response used by a mock server.
|
2016-03-26 05:34:31 +02:00
|
|
|
type MockResponse struct {
|
|
|
|
StatusCode int
|
|
|
|
Body string
|
|
|
|
}
|
|
|
|
|
2020-05-08 19:35:25 +02:00
|
|
|
// MockResponseMap maps request paths to responses.
|
2016-03-26 05:34:31 +02:00
|
|
|
type MockResponseMap map[string]MockResponse
|
|
|
|
|
2021-11-02 01:52:38 +02:00
|
|
|
func setupTest(t *testing.T, responses MockResponseMap) string {
|
2020-12-29 00:39:00 +02:00
|
|
|
t.Helper()
|
|
|
|
|
2021-11-02 01:52:38 +02:00
|
|
|
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2016-03-26 05:34:31 +02:00
|
|
|
path := r.URL.Path
|
|
|
|
resp, ok := responses[path]
|
|
|
|
if !ok {
|
2018-10-09 19:03:07 +02:00
|
|
|
resp, ok = responses[r.RequestURI]
|
|
|
|
if !ok {
|
|
|
|
msg := fmt.Sprintf("Requested path not found in response map: %s", path)
|
|
|
|
require.FailNow(t, msg)
|
|
|
|
}
|
2016-03-26 05:34:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/xml")
|
|
|
|
w.WriteHeader(resp.StatusCode)
|
2018-09-24 21:07:20 +02:00
|
|
|
_, err := w.Write([]byte(resp.Body))
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2021-11-02 01:52:38 +02:00
|
|
|
})
|
2016-03-26 05:34:31 +02:00
|
|
|
|
2021-11-02 01:52:38 +02:00
|
|
|
server := httptest.NewServer(handler)
|
2020-12-29 00:39:00 +02:00
|
|
|
t.Cleanup(server.Close)
|
|
|
|
|
2016-03-26 05:34:31 +02:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
2020-12-29 00:39:00 +02:00
|
|
|
|
|
|
|
return server.URL
|
2016-03-26 05:34:31 +02:00
|
|
|
}
|