Skip to content

Commit

Permalink
don't follow redirects (#50)
Browse files Browse the repository at this point in the history
Some requests make redirect as result and sometimes we need just
check if url is valid, but don't have to follow it
  • Loading branch information
JustSkiv authored Apr 8, 2020
1 parent 49043f6 commit 005b332
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 5 deletions.
6 changes: 3 additions & 3 deletions runner/previous_result_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import (
"encoding/json"
"net/http"
"net/http/httptest"
"path/filepath"
"testing"
)

const (
testDir = "testdata"
body = "bla"
body = "bla"
)

func Test_PreviousResult(t *testing.T) {
Expand All @@ -19,7 +19,7 @@ func Test_PreviousResult(t *testing.T) {

RunWithTesting(t, &RunWithTestingParams{
Server: srv,
TestsDir: testDir,
TestsDir: filepath.Join("testdata", "previous-result"),
})
}

Expand Down
8 changes: 7 additions & 1 deletion runner/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@ func newClient() (*http.Client, error) {
}
transport.Proxy = http.ProxyURL(proxyUrl)
}
return &http.Client{Transport: transport}, nil

return &http.Client{
Transport: transport,
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
}, nil
}

func newRequest(host string, test models.TestInterface) (*http.Request, error) {
Expand Down
2 changes: 1 addition & 1 deletion runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func (r *Runner) executeTest(v models.TestInterface, client *http.Client) (*mode
if err != nil {
return nil, err
}
resp.Body.Close()
_ = resp.Body.Close()

bodyStr := string(body)

Expand Down
24 changes: 24 additions & 0 deletions runner/runner_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package runner

import (
"net/http"
"net/http/httptest"
"path/filepath"
"testing"
)

func TestDontFollowRedirects(t *testing.T) {
srv := testServerRedirect()
defer srv.Close()

RunWithTesting(t, &RunWithTestingParams{
Server: srv,
TestsDir: filepath.Join("testdata", "dont-follow-redirects"),
})
}

func testServerRedirect() *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/redirect-url", http.StatusFound)
}))
}
4 changes: 4 additions & 0 deletions runner/testdata/dont-follow-redirects/with-redirect.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- name: "dont-follow-redirects"
method: GET
response:
302: "$matchRegexp(redirect-url)"
File renamed without changes.

0 comments on commit 005b332

Please sign in to comment.