Skip to content

Commit

Permalink
Always set response.URL to the latest redirect even on errors fix #987 (
Browse files Browse the repository at this point in the history
  • Loading branch information
mstoykov authored Apr 10, 2019
1 parent 3506ee1 commit 20e8961
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
24 changes: 20 additions & 4 deletions js/modules/k6/http/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1348,6 +1348,11 @@ func TestErrorCodes(t *testing.T) {
defer tb.Cleanup()
sr := tb.Replacer.Replace

var connectionRefusedErrorText = "connect: connection refused"
if runtime.GOOS == "windows" {
connectionRefusedErrorText = "connectex: No connection could be made because the target machine actively refused it."
}

// Handple paths with custom logic
tb.Mux.HandleFunc("/digest-auth/failure", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(2 * time.Second)
Expand Down Expand Up @@ -1425,11 +1430,22 @@ func TestErrorCodes(t *testing.T) {
script: `let res = http.request("GET", "dafsgdhfjg/");`,
},
{
name: "Too many redirects",
status: 302,
name: "Too many redirects",
status: 302,
moreSamples: 2,
script: `
let res = http.get("HTTPBIN_URL/redirect/1", {redirects: 0});
if (res.url != "HTTPBIN_URL/redirect/1") { throw new Error("incorrect URL: " + res.url) }`,
let res = http.get("HTTPBIN_URL/relative-redirect/3", {redirects: 2});
if (res.url != "HTTPBIN_URL/relative-redirect/1") { throw new Error("incorrect URL: " + res.url) }`,
},
{
name: "Connection refused redirect",
status: 0,
moreSamples: 1,
expectedErrorMsg: `dial tcp 127.0.0.1:1: ` + connectionRefusedErrorText,
expectedErrorCode: 1210,
script: `
let res = http.get("HTTPBIN_URL/redirect-to?url=http%3A%2F%2F127.0.0.1%3A1%2Fpesho");
if (res.url != "http://127.0.0.1:1/pesho") { throw new Error("incorrect URL: " + res.url) }`,
},
}

Expand Down
1 change: 1 addition & 0 deletions lib/netext/httpext/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ func MakeRequest(ctx context.Context, preq *ParsedHTTPRequest) (*Response, error
Transport: transport,
Timeout: preq.Timeout,
CheckRedirect: func(req *http.Request, via []*http.Request) error {
resp.URL = req.URL.String()
debugResponse(state, req.Response, "RedirectResponse")

// Update active jar with cookies found in "Set-Cookie" header(s) of redirect response
Expand Down
2 changes: 2 additions & 0 deletions release notes/upcoming.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ Description of feature.
- don't make HTTP requests (#963)
- correctly open simple filenames like `"file.json"` and paths such as `"relative/path/to.txt"` as relative (to the current working directory) paths; previously they had to start with a dot (i.e. `"./relative/path/to.txt"`) for that to happen
- windows: work with paths starting with `/` or `\` as absolute from the current drive

* JS: Correctly always set `response.url` to be the URL that was ultimately fetched (i.e. after any potential redirects), even if there were non http errors. (#990)

0 comments on commit 20e8961

Please sign in to comment.