Skip to content

Commit

Permalink
fix(exp): allow request path matching in the want function
Browse files Browse the repository at this point in the history
  • Loading branch information
jooola committed Jun 25, 2024
1 parent 7ac80d7 commit baa7556
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
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,
)

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)
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

0 comments on commit baa7556

Please sign in to comment.