Skip to content

Commit

Permalink
added form-urlencoded test case
Browse files Browse the repository at this point in the history
  • Loading branch information
flrdv committed Oct 7, 2024
1 parent c325e4f commit 207b8f0
Showing 1 changed file with 44 additions and 4 deletions.
48 changes: 44 additions & 4 deletions indigo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/indigo-web/indigo/http/cookie"
"github.com/indigo-web/indigo/http/headers"
"github.com/indigo-web/indigo/http/method"
"github.com/indigo-web/indigo/http/mime"
"github.com/indigo-web/indigo/http/status"
"github.com/indigo-web/indigo/internal/httptest"
"github.com/indigo-web/indigo/router/inbuilt/middleware"
Expand Down Expand Up @@ -77,14 +78,14 @@ func getInbuiltRouter() *inbuilt.Router {
})

r.Get("/query", func(request *http.Request) *http.Response {
params, err := request.Query.Unwrap()
params, err := request.Query.Cook()
if err != nil {
return http.Error(request, err)
}

var buff []byte

for _, pair := range params.Unwrap() {
for _, pair := range params.Expose() {
buff = append(buff, pair.Key+":"+pair.Value+"."...)
}

Expand Down Expand Up @@ -131,8 +132,7 @@ func getInbuiltRouter() *inbuilt.Router {
}

buff := make([]byte, 0, 512)

for _, c := range jar.Unwrap() {
for _, c := range jar.Expose() {
buff = append(buff, fmt.Sprintf("%s=%s\n", c.Key, c.Value)...)
}

Expand All @@ -141,6 +141,20 @@ func getInbuiltRouter() *inbuilt.Router {
Cookie(cookie.New("men", "in black"))
})

r.Get("/form-urlencoded", func(request *http.Request) *http.Response {
form, err := request.Body.Form()
if err != nil {
return http.Error(request, err)
}

buff := make([]byte, 0, 512)
for _, pair := range form.Expose() {
buff = append(buff, fmt.Sprintf("%s=%s\n", pair.Key, pair.Value)...)
}

return http.Bytes(request, buff)
})

return r
}

Expand Down Expand Up @@ -567,6 +581,32 @@ func TestFirstPhase(t *testing.T) {
require.Equal(t, []string{"hello=world", "men=in black"}, resp.Header.Values("Set-Cookie"))
})

t.Run("form data", func(t *testing.T) {
request := &stdhttp.Request{
Method: stdhttp.MethodGet,
URL: &url.URL{
Scheme: "http",
Host: addr,
Path: "/form-urlencoded",
},
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
Header: stdhttp.Header{
"Content-Type": {mime.FormUrlencoded},
},
Host: addr,
RemoteAddr: addr,
Body: io.NopCloser(strings.NewReader("hello=world&my+name=Paul&a%2bb=5")),
}
resp, err := stdhttp.DefaultClient.Do(request)
require.NoError(t, err)
require.Equal(t, stdhttp.StatusOK, resp.StatusCode)
body, err := io.ReadAll(resp.Body)
require.NoError(t, err)
require.Equal(t, "hello=world\nmy name=Paul\na+b=5\n", string(body))
})

t.Run("chunked body", func(t *testing.T) {
request := "POST /body-reader HTTP/1.1\r\n" +
"Connection: close\r\n" +
Expand Down

0 comments on commit 207b8f0

Please sign in to comment.