Skip to content

Commit

Permalink
WIP keep request header case
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Mirić committed May 20, 2020
1 parent 9586f2f commit 3e3c347
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
6 changes: 4 additions & 2 deletions js/modules/k6/http/request.go
Expand Up @@ -33,10 +33,11 @@ import (
"time"

"github.com/dop251/goja"
null "gopkg.in/guregu/null.v3"

"github.com/loadimpact/k6/js/common"
"github.com/loadimpact/k6/lib"
"github.com/loadimpact/k6/lib/netext/httpext"
null "gopkg.in/guregu/null.v3"
)

// ErrHTTPForbiddenInInitContext is used when a http requests was made in the init context
Expand Down Expand Up @@ -291,7 +292,8 @@ func (h *HTTP) parseRequest(
case "host":
result.Req.Host = str
default:
result.Req.Header.Set(key, str)
// Not using Set or Add to keep original case (see #1246)
result.Req.Header[key] = []string{str}
}
}
case "jar":
Expand Down
29 changes: 23 additions & 6 deletions js/modules/k6/http/request_test.go
Expand Up @@ -40,19 +40,20 @@ import (
"github.com/andybalholm/brotli"
"github.com/dop251/goja"
"github.com/klauspost/compress/zstd"
"github.com/loadimpact/k6/js/common"
"github.com/loadimpact/k6/lib"
"github.com/loadimpact/k6/lib/metrics"
"github.com/loadimpact/k6/lib/testutils"
"github.com/loadimpact/k6/lib/testutils/httpmultibin"
"github.com/loadimpact/k6/stats"
"github.com/mccutchen/go-httpbin/httpbin"
"github.com/oxtoacart/bpool"
"github.com/sirupsen/logrus"
logtest "github.com/sirupsen/logrus/hooks/test"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
null "gopkg.in/guregu/null.v3"

"github.com/loadimpact/k6/js/common"
"github.com/loadimpact/k6/lib"
"github.com/loadimpact/k6/lib/metrics"
"github.com/loadimpact/k6/lib/testutils"
"github.com/loadimpact/k6/lib/testutils/httpmultibin"
"github.com/loadimpact/k6/stats"
)

func assertRequestMetricsEmitted(t *testing.T, sampleContainers []stats.SampleContainer, method, url, name string, status int, group string) {
Expand Down Expand Up @@ -892,6 +893,22 @@ func TestRequestAndBatch(t *testing.T) {
assert.NoError(t, err)
assertRequestMetricsEmitted(t, stats.GetBufferedSamples(samples), "GET", sr("HTTPBIN_URL/headers"), "", 200, "")
})

t.Run("headers-keep-case", func(t *testing.T) {
tb.Mux.HandleFunc("/headers-case-sensitive", func(w http.ResponseWriter, r *http.Request) {
// r.Header already has headers transformed by CanonicalMIMEHeaderKey,
// e.g. "custom-CASE-header" is "Custom-Case-Header" here.
})
_, err := common.RunString(rt, sr(`
let res = http.request("GET", "HTTPBIN_URL/headers-case-sensitive", null, {
headers: { "custom-CASE-header": "value" },
});
if (res.status != 200) { throw new Error("wrong status: " + res.status); }
if (res.json().headers["custom-CASE-header"] != "value") { throw new Error("wrong custom-CASE-header: " + res.json().headers["custom-CASE-header"]); }
`))
assert.NoError(t, err)
assertRequestMetricsEmitted(t, stats.GetBufferedSamples(samples), "GET", sr("HTTPBIN_URL/headers-case-sensitive"), "", 200, "")
})
})

t.Run("tags", func(t *testing.T) {
Expand Down

0 comments on commit 3e3c347

Please sign in to comment.