Skip to content

Commit f749925

Browse files
authored
feat(http): support cookies in redirects (#866)
Signed-off-by: Gabriel Augendre <gabriel.augendre@ovhcloud.com>
1 parent 044ac99 commit f749925

3 files changed

Lines changed: 68 additions & 1 deletion

File tree

executors/http/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,8 @@ Example if you want to get value of `path` key of *second* element in `apis` arr
155155
```yaml
156156
result.statuscode ShouldEqual 200
157157
```
158+
159+
## Cookies
160+
161+
Cookies are automatically handled when following a redirect in a single step.
162+
They are not supported between successive steps.

executors/http/http.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"mime/multipart"
1212
"net"
1313
"net/http"
14+
"net/http/cookiejar"
1415
"net/url"
1516
"os"
1617
"path/filepath"
@@ -172,7 +173,14 @@ func (Executor) Run(ctx context.Context, step venom.TestStep) (interface{}, erro
172173
tr.Proxy = http.ProxyURL(proxyURL)
173174
}
174175

175-
client := &http.Client{Transport: tr}
176+
// cookie jar can be used with redirects in the same call
177+
// this doesn't support cross steps cookies.
178+
jar, err := cookiejar.New(nil)
179+
if err != nil {
180+
return nil, err
181+
}
182+
183+
client := &http.Client{Transport: tr, Jar: jar}
176184
if e.NoFollowRedirect {
177185
client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
178186
return http.ErrUseLastResponse

executors/http/http_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ import (
44
"context"
55
"fmt"
66
"io"
7+
"net/http"
8+
"net/http/httptest"
79
"os"
810
"os/exec"
11+
"sync/atomic"
912
"testing"
1013

1114
"github.com/stretchr/testify/require"
@@ -123,3 +126,54 @@ func TestInterpolation_without_match_Of_String(t *testing.T) {
123126
_, err := e.getRequest(ctx, "../../")
124127
require.Errorf(t, err, "unable to interpolate file due to unresolved variables {{.name}}")
125128
}
129+
130+
func TestCookieRedirect(t *testing.T) {
131+
callCount := atomic.Int32{}
132+
ctx := context.Background()
133+
134+
mux := http.NewServeMux()
135+
mux.HandleFunc("GET /set", func(w http.ResponseWriter, r *http.Request) {
136+
http.SetCookie(w, &http.Cookie{
137+
Name: "some-cookie",
138+
Value: "some-value",
139+
Path: "/",
140+
MaxAge: 100,
141+
Secure: false,
142+
HttpOnly: true,
143+
})
144+
w.Header().Set("Location", "/get")
145+
w.WriteHeader(http.StatusSeeOther)
146+
})
147+
148+
mux.HandleFunc("GET /get", func(w http.ResponseWriter, r *http.Request) {
149+
callCount.Add(1)
150+
cookie, err := r.Cookie("some-cookie")
151+
if err != nil {
152+
w.WriteHeader(http.StatusBadRequest)
153+
return
154+
}
155+
if cookie.Value != "some-value" {
156+
w.WriteHeader(http.StatusBadRequest)
157+
return
158+
}
159+
w.WriteHeader(http.StatusOK)
160+
})
161+
162+
srv := httptest.NewServer(mux)
163+
t.Cleanup(srv.Close)
164+
165+
e := &Executor{}
166+
res, err := e.Run(ctx, venom.TestStep{
167+
"method": http.MethodGet,
168+
"url": srv.URL,
169+
"path": "/set",
170+
})
171+
require.NoError(t, err)
172+
173+
result, ok := res.(Result)
174+
require.True(t, ok)
175+
176+
require.Equal(t, result.StatusCode, http.StatusOK)
177+
178+
require.Equal(t, int32(1), callCount.Load())
179+
}

0 commit comments

Comments
 (0)