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 a599a61
Show file tree
Hide file tree
Showing 2 changed files with 23 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
15 changes: 15 additions & 0 deletions hcloud/exp/mockutils/mockutils_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package mockutils

import (
"fmt"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"

"math/rand"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand All @@ -31,6 +34,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 +64,11 @@ 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()))

Check failure on line 69 in hcloud/exp/mockutils/mockutils_test.go

View workflow job for this annotation

GitHub Actions / lint

response body must be closed (bodyclose)
require.NoError(t, err)
assert.Equal(t, 200, resp.StatusCode)
}

func readBody(t *testing.T, resp *http.Response) string {
Expand Down

0 comments on commit a599a61

Please sign in to comment.