Skip to content

Commit

Permalink
Fixed cookie support
Browse files Browse the repository at this point in the history
Closes #289

Closes #286
  • Loading branch information
Emily Ekberg committed Aug 1, 2017
1 parent a7b28c2 commit 810f394
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 4 deletions.
6 changes: 3 additions & 3 deletions js/common/state.go
Expand Up @@ -22,6 +22,7 @@ package common

import (
"net/http"
"net/http/cookiejar"

"github.com/loadimpact/k6/lib"
"github.com/loadimpact/k6/lib/netext"
Expand All @@ -42,9 +43,8 @@ type State struct {

// Networking equipment.
HTTPTransport http.RoundTripper

// Also expose the underlying dialer
Dialer *netext.Dialer
Dialer *netext.Dialer
CookieJar *cookiejar.Jar

// Sample buffer, emitted at the end of the iteration.
Samples []stats.Sample
Expand Down
1 change: 1 addition & 0 deletions js/modules/k6/http/http.go
Expand Up @@ -188,6 +188,7 @@ func (*HTTP) request(ctx context.Context, rt *goja.Runtime, state *common.State,
}
client := http.Client{
Transport: state.HTTPTransport,
Jar: state.CookieJar,
Timeout: timeout,
CheckRedirect: func(req *http.Request, via []*http.Request) error {
max := int(state.Options.MaxRedirects.Int64)
Expand Down
9 changes: 8 additions & 1 deletion js/runner.go
Expand Up @@ -25,6 +25,7 @@ import (
"crypto/tls"
"net"
"net/http"
"net/http/cookiejar"
"time"

"github.com/dop251/goja"
Expand Down Expand Up @@ -167,12 +168,18 @@ type VU struct {
}

func (u *VU) RunOnce(ctx context.Context) ([]stats.Sample, error) {
cookieJar, err := cookiejar.New(nil)
if err != nil {
return nil, err
}

state := &common.State{
Logger: u.Runner.Logger,
Options: u.Runner.Bundle.Options,
Group: u.Runner.defaultGroup,
HTTPTransport: u.HTTPTransport,
Dialer: u.Dialer,
CookieJar: cookieJar,
}
u.Dialer.BytesRead = &state.BytesRead
u.Dialer.BytesWritten = &state.BytesWritten
Expand All @@ -184,7 +191,7 @@ func (u *VU) RunOnce(ctx context.Context) ([]stats.Sample, error) {
u.Runtime.Set("__ITER", u.Iteration)
u.Iteration++

_, err := u.Default(goja.Undefined())
_, err = u.Default(goja.Undefined())

t := time.Now()
samples := append(state.Samples,
Expand Down
48 changes: 48 additions & 0 deletions js/runner_test.go
Expand Up @@ -512,3 +512,51 @@ func TestVUIntegrationHTTP2(t *testing.T) {
})
}
}

func TestVUIntegrationCookies(t *testing.T) {
r1, err := New(&lib.SourceData{
Filename: "/script.js",
Data: []byte(`
import http from "k6/http";
export default function() {
let preRes = http.get("https://httpbin.org/cookies");
if (preRes.status != 200) { throw new Error("wrong status (pre): " + preRes.status); }
if (preRes.json().cookies.k1 || preRes.json().cookies.k2) {
throw new Error("cookies persisted: " + preRes.body);
}
let res = http.get("https://httpbin.org/cookies/set?k2=v2&k1=v1");
if (res.status != 200) { throw new Error("wrong status: " + res.status) }
if (res.json().cookies.k1 != "v1" || res.json().cookies.k2 != "v2") {
throw new Error("wrong cookies: " + res.body);
}
}
`),
}, afero.NewMemMapFs())
if !assert.NoError(t, err) {
return
}
r1.ApplyOptions(lib.Options{
Throw: null.BoolFrom(true),
MaxRedirects: null.IntFrom(10),
})

r2, err := NewFromArchive(r1.MakeArchive())
if !assert.NoError(t, err) {
return
}

runners := map[string]*Runner{"Source": r1, "Archive": r2}
for name, r := range runners {
t.Run(name, func(t *testing.T) {
vu, err := r.NewVU()
if !assert.NoError(t, err) {
return
}
for i := 0; i < 2; i++ {
_, err = vu.RunOnce(context.Background())
assert.NoError(t, err)
}
})
}
}

0 comments on commit 810f394

Please sign in to comment.