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

assert: fix missing *http.Request.RequestURI #1495

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions assert/http_assertions.go
Expand Up @@ -16,6 +16,7 @@ func httpCode(handler http.HandlerFunc, method, url string, values url.Values) (
if err != nil {
return -1, err
}
req.RequestURI = req.URL.RequestURI()
req.URL.RawQuery = values.Encode()
handler(w, req)
return w.Code, nil
Expand Down Expand Up @@ -120,6 +121,7 @@ func HTTPBody(handler http.HandlerFunc, method, url string, values url.Values) s
if err != nil {
return ""
}
req.RequestURI = req.URL.RequestURI()
handler(w, req)
return w.Body.String()
}
Expand Down
42 changes: 42 additions & 0 deletions assert/http_assertions_test.go
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io"
"net/http"
"net/http/httptest"
"net/url"
"testing"
)
Expand Down Expand Up @@ -212,3 +213,44 @@ func TestHttpBodyWrappers(t *testing.T) {
assert.False(mockAssert.HTTPBodyNotContains(httpHelloName, "GET", "/", url.Values{"name": []string{"World"}}, "World"))
assert.True(mockAssert.HTTPBodyNotContains(httpHelloName, "GET", "/", url.Values{"name": []string{"World"}}, "world"))
}

func httpCheckRequest(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
_, _ = fmt.Fprintf(w, "URI: %s\n", r.RequestURI)
_, _ = fmt.Fprintf(w, "RawQuery: %s\n", r.URL.RawQuery)
}

func TestHTTPCheckRequest(t *testing.T) {
t.Run("by test-server", func(t *testing.T) {
assert := New(t)
ts := httptest.NewServer(http.HandlerFunc(httpCheckRequest))
defer ts.Close()
resp, err1 := ts.Client().Get(ts.URL + "/check?p1=World")
assert.NoError(err1)
hidu marked this conversation as resolved.
Show resolved Hide resolved
bf, err2 := io.ReadAll(resp.Body)
_ = resp.Body.Close()
assert.NoError(err2)
assert.Contains(string(bf), "URI: /check")
assert.Contains(string(bf), "RawQuery: p1=World")
})

t.Run("by assert", func(t *testing.T) {
assert := New(t)

mockT1 := new(testing.T)
assert.Equal(HTTPSuccess(mockT1, httpCheckRequest, "GET", "/", nil), true)
assert.False(mockT1.Failed())

values2 := url.Values{
"p1": []string{"World"},
}
body2 := HTTPBody(httpCheckRequest, "GET", "/check", values2)
assert.Contains(body2, "URI: /check")

values3 := url.Values{
"p1": []string{"World"},
}
body3 := HTTPBody(httpCheckRequest, "GET", "/", values3)
assert.Contains(body3, "RawQuery: p1=World")
})
}