Skip to content

Commit

Permalink
feat(tdhttp): add TestAPI's DefaultRequestParams & AddDefaultRequestP…
Browse files Browse the repository at this point in the history
…arams

Closes #257.

Signed-off-by: Maxime Soulé <btik-git@scoubidou.com>
  • Loading branch information
maxatome committed Jan 15, 2024
1 parent 866bc2a commit 6aec0d8
Show file tree
Hide file tree
Showing 4 changed files with 467 additions and 48 deletions.
73 changes: 45 additions & 28 deletions helpers/tdhttp/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,39 @@ import (
)

func newRequest(method string, target string, body io.Reader, headersQueryParams []any) (*http.Request, error) {
header, qp, cookies, err := requestParams(headersQueryParams)
if err != nil {
return nil, err
}

// Parse path even when no query params to have consistent error
// messages when using query params or not
u, err := url.Parse(target)
if err != nil {
return nil, errors.New(color.Bad("target is not a valid path: %s", err))
}
if len(qp) > 0 {
if u.RawQuery != "" {
u.RawQuery += "&"
}
u.RawQuery += qp.Encode()
target = u.String()
}

req := httptest.NewRequest(method, target, body)

for k, v := range header {
req.Header[k] = append(req.Header[k], v...)
}

for _, c := range cookies {
req.AddCookie(c)
}

return req, nil
}

func requestParams(headersQueryParams []any) (http.Header, url.Values, []*http.Cookie, error) {
header := http.Header{}
qp := url.Values{}
var cookies []*http.Cookie
Expand All @@ -37,7 +70,7 @@ func newRequest(method string, target string, body io.Reader, headersQueryParams
if i < len(headersQueryParams) {
var ok bool
if val, ok = headersQueryParams[i].(string); !ok {
return nil, errors.New(color.Bad(
return nil, nil, nil, errors.New(color.Bad(
`header "%s" should have a string value, not a %T (@ headersQueryParams[%d])`,
cur, headersQueryParams[i], i))
}
Expand All @@ -56,6 +89,9 @@ func newRequest(method string, target string, body io.Reader, headersQueryParams
case http.Cookie:
cookies = append(cookies, &cur)

case []*http.Cookie:
cookies = append(cookies, cur...)

case url.Values:
for k, v := range cur {
qp[k] = append(qp[k], v...)
Expand All @@ -64,43 +100,24 @@ func newRequest(method string, target string, body io.Reader, headersQueryParams
case Q:
err := cur.AddTo(qp)
if err != nil {
return nil, errors.New(color.Bad(
return nil, nil, nil, errors.New(color.Bad(
"headersQueryParams... tdhttp.Q bad parameter: %s (@ headersQueryParams[%d])",
err, i))
}

default:
return nil, errors.New(color.Bad(
"headersQueryParams... can only contains string, http.Header, http.Cookie, url.Values and tdhttp.Q, not %T (@ headersQueryParams[%d])",
return nil, nil, nil, errors.New(color.Bad(
"headersQueryParams... can only contains string, http.Header, ([]*|*|)http.Cookie, url.Values and tdhttp.Q, not %T (@ headersQueryParams[%d])",
cur, i))
}
}

// Parse path even when no query params to have consistent error
// messages when using query params or not
u, err := url.Parse(target)
if err != nil {
return nil, errors.New(color.Bad("target is not a valid path: %s", err))
if len(header) == 0 {
header = nil
}
if len(qp) > 0 {
if u.RawQuery != "" {
u.RawQuery += "&"
}
u.RawQuery += qp.Encode()
target = u.String()
}

req := httptest.NewRequest(method, target, body)

for k, v := range header {
req.Header[k] = append(req.Header[k], v...)
if len(qp) == 0 {
qp = nil
}

for _, c := range cookies {
req.AddCookie(c)
}

return req, nil
return header, qp, cookies, nil
}

// BasicAuthHeader returns a new [http.Header] with only Authorization
Expand Down
28 changes: 17 additions & 11 deletions helpers/tdhttp/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,15 @@ func TestNewRequest(tt *testing.T) {
req := tdhttp.NewRequest("GET", "/path", nil,
&http.Cookie{Name: "cook1", Value: "val1"},
http.Cookie{Name: "cook2", Value: "val2"},
[]*http.Cookie{
{Name: "cook3", Value: "val3"},
{Name: "cook4", Value: "val4"},
},
)

t.Cmp(req.Header, http.Header{"Cookie": []string{"cook1=val1; cook2=val2"}})
t.Cmp(req.Header, http.Header{
"Cookie": {"cook1=val1; cook2=val2; cook3=val3; cook4=val4"},
})
})

t.Run("NewRequest header flattened", func(t *td.T) {
Expand Down Expand Up @@ -135,47 +141,47 @@ func TestNewRequest(tt *testing.T) {
t.Run("NewRequest panics", func(t *td.T) {
t.CmpPanic(
func() { tdhttp.NewRequest("GET", "/path", nil, "H", "V", true) },
td.HasPrefix("headersQueryParams... can only contains string, http.Header, http.Cookie, url.Values and tdhttp.Q, not bool (@ headersQueryParams[2])"))
td.HasPrefix("headersQueryParams... can only contains string, http.Header, ([]*|*|)http.Cookie, url.Values and tdhttp.Q, not bool (@ headersQueryParams[2])"))

t.CmpPanic(
func() { tdhttp.NewRequest("GET", "/path", nil, "H1", true) },
td.HasPrefix(`header "H1" should have a string value, not a bool (@ headersQueryParams[1])`))

t.CmpPanic(
func() { tdhttp.Get("/path", true) },
td.HasPrefix("headersQueryParams... can only contains string, http.Header, http.Cookie, url.Values and tdhttp.Q, not bool (@ headersQueryParams[0])"))
td.HasPrefix("headersQueryParams... can only contains string, http.Header, ([]*|*|)http.Cookie, url.Values and tdhttp.Q, not bool (@ headersQueryParams[0])"))

t.CmpPanic(
func() { tdhttp.Head("/path", true) },
td.HasPrefix("headersQueryParams... can only contains string, http.Header, http.Cookie, url.Values and tdhttp.Q, not bool (@ headersQueryParams[0])"))
td.HasPrefix("headersQueryParams... can only contains string, http.Header, ([]*|*|)http.Cookie, url.Values and tdhttp.Q, not bool (@ headersQueryParams[0])"))

t.CmpPanic(
func() { tdhttp.Options("/path", nil, true) },
td.HasPrefix("headersQueryParams... can only contains string, http.Header, http.Cookie, url.Values and tdhttp.Q, not bool (@ headersQueryParams[0])"))
td.HasPrefix("headersQueryParams... can only contains string, http.Header, ([]*|*|)http.Cookie, url.Values and tdhttp.Q, not bool (@ headersQueryParams[0])"))

t.CmpPanic(
func() { tdhttp.Post("/path", nil, true) },
td.HasPrefix("headersQueryParams... can only contains string, http.Header, http.Cookie, url.Values and tdhttp.Q, not bool (@ headersQueryParams[0])"))
td.HasPrefix("headersQueryParams... can only contains string, http.Header, ([]*|*|)http.Cookie, url.Values and tdhttp.Q, not bool (@ headersQueryParams[0])"))

t.CmpPanic(
func() { tdhttp.PostForm("/path", nil, true) },
td.HasPrefix("headersQueryParams... can only contains string, http.Header, http.Cookie, url.Values and tdhttp.Q, not bool (@ headersQueryParams[0])"))
td.HasPrefix("headersQueryParams... can only contains string, http.Header, ([]*|*|)http.Cookie, url.Values and tdhttp.Q, not bool (@ headersQueryParams[0])"))

t.CmpPanic(
func() { tdhttp.PostMultipartFormData("/path", &tdhttp.MultipartBody{}, true) },
td.HasPrefix("headersQueryParams... can only contains string, http.Header, http.Cookie, url.Values and tdhttp.Q, not bool (@ headersQueryParams[0])"))
td.HasPrefix("headersQueryParams... can only contains string, http.Header, ([]*|*|)http.Cookie, url.Values and tdhttp.Q, not bool (@ headersQueryParams[0])"))

t.CmpPanic(
func() { tdhttp.Patch("/path", nil, true) },
td.HasPrefix("headersQueryParams... can only contains string, http.Header, http.Cookie, url.Values and tdhttp.Q, not bool (@ headersQueryParams[0])"))
td.HasPrefix("headersQueryParams... can only contains string, http.Header, ([]*|*|)http.Cookie, url.Values and tdhttp.Q, not bool (@ headersQueryParams[0])"))

t.CmpPanic(
func() { tdhttp.Put("/path", nil, true) },
td.HasPrefix("headersQueryParams... can only contains string, http.Header, http.Cookie, url.Values and tdhttp.Q, not bool (@ headersQueryParams[0])"))
td.HasPrefix("headersQueryParams... can only contains string, http.Header, ([]*|*|)http.Cookie, url.Values and tdhttp.Q, not bool (@ headersQueryParams[0])"))

t.CmpPanic(
func() { tdhttp.Delete("/path", nil, true) },
td.HasPrefix("headersQueryParams... can only contains string, http.Header, http.Cookie, url.Values and tdhttp.Q, not bool (@ headersQueryParams[0])"))
td.HasPrefix("headersQueryParams... can only contains string, http.Header, ([]*|*|)http.Cookie, url.Values and tdhttp.Q, not bool (@ headersQueryParams[0])"))

// Bad target
t.CmpPanic(
Expand Down

0 comments on commit 6aec0d8

Please sign in to comment.