Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(exp): allow request path matching in the want function #475

Merged
merged 1 commit into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions hcloud/exp/mockutils/mockutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)

expectedCall := expected.Method
foundCall := r.Method
if expected.Path != "" {
expectedCall += " " + expected.Path
foundCall += " " + r.RequestURI
}
require.Equal(t, expectedCall, foundCall)

if expected.Want != nil {
expected.Want(t, r)
Expand Down
16 changes: 16 additions & 0 deletions hcloud/exp/mockutils/mockutils_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package mockutils

import (
"fmt"
"io"
"math/rand"
"net/http"
"net/http/httptest"
"strings"
Expand Down Expand Up @@ -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()

Expand All @@ -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 {
Expand Down
Loading