From baa75568b4c18f19ff134470ec89b65089a15dc3 Mon Sep 17 00:00:00 2001 From: jo Date: Tue, 25 Jun 2024 17:25:11 +0200 Subject: [PATCH] fix(exp): allow request path matching in the want function --- hcloud/exp/mockutils/mockutils.go | 12 ++++++++---- hcloud/exp/mockutils/mockutils_test.go | 16 ++++++++++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/hcloud/exp/mockutils/mockutils.go b/hcloud/exp/mockutils/mockutils.go index 8c22fd8e1..f1e313e8d 100644 --- a/hcloud/exp/mockutils/mockutils.go +++ b/hcloud/exp/mockutils/mockutils.go @@ -43,10 +43,14 @@ func Handler(t *testing.T, requests []Request) http.HandlerFunc { } expected := requests[index] - require.Equal(t, - expected.Method+" "+expected.Path, - r.Method+" "+r.RequestURI, - ) + + expectedURL := expected.Method + foundURL := r.Method + if expected.Path != "" { + expectedURL += " " + expected.Path + foundURL += " " + r.RequestURI + } + require.Equal(t, expectedURL, foundURL) if expected.Want != nil { expected.Want(t, r) diff --git a/hcloud/exp/mockutils/mockutils_test.go b/hcloud/exp/mockutils/mockutils_test.go index 70f790587..63fe0a398 100644 --- a/hcloud/exp/mockutils/mockutils_test.go +++ b/hcloud/exp/mockutils/mockutils_test.go @@ -1,7 +1,9 @@ package mockutils import ( + "fmt" "io" + "math/rand" "net/http" "net/http/httptest" "strings" @@ -31,6 +33,13 @@ func TestHandler(t *testing.T) { Method: "GET", Path: "/", Status: 503, }, + { + Method: "GET", + Want: func(t *testing.T, r *http.Request) { + require.True(t, strings.HasPrefix(r.RequestURI, "/random?key=")) + }, + Status: 200, + }, })) defer server.Close() @@ -54,6 +63,13 @@ func TestHandler(t *testing.T) { assert.Equal(t, 503, resp.StatusCode) assert.Equal(t, "", resp.Header.Get("Content-Type")) assert.Equal(t, "", readBody(t, resp)) + + // Request 4 + resp, err = http.Get(fmt.Sprintf("%s/random?key=%d", server.URL, rand.Int63())) + require.NoError(t, err) + assert.Equal(t, 200, resp.StatusCode) + assert.Equal(t, "", resp.Header.Get("Content-Type")) + assert.Equal(t, "", readBody(t, resp)) } func readBody(t *testing.T, resp *http.Response) string {