Skip to content

Commit a49a9cb

Browse files
authored
Merge pull request ipfs#160 from ipfs/fix/warn-on-redir
error on redirect
2 parents 21af6be + 5d8754c commit a49a9cb

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

requestbuilder.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func (r *RequestBuilder) Send(ctx context.Context) (*Response, error) {
7878
req.Opts = r.opts
7979
req.Headers = r.headers
8080
req.Body = r.body
81-
return req.Send(r.shell.httpcli)
81+
return req.Send(&r.shell.httpcli)
8282
}
8383

8484
// Exec sends the request a request and decodes the response.

shell.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ const (
3333

3434
type Shell struct {
3535
url string
36-
httpcli *gohttp.Client
36+
httpcli gohttp.Client
3737
}
3838

3939
func NewLocalShell() *Shell {
@@ -79,11 +79,13 @@ func NewShellWithClient(url string, c *gohttp.Client) *Shell {
7979
url = host
8080
}
8181
}
82-
83-
return &Shell{
84-
url: url,
85-
httpcli: c,
82+
var sh Shell
83+
sh.url = url
84+
// We don't support redirects.
85+
sh.httpcli.CheckRedirect = func(_ *gohttp.Request, _ []*gohttp.Request) error {
86+
return fmt.Errorf("unexpected redirect")
8687
}
88+
return &sh
8789
}
8890

8991
func (s *Shell) SetTimeout(d time.Duration) {

shell_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"io"
99
"math/rand"
1010
"sort"
11+
"strings"
1112
"testing"
1213
"time"
1314

@@ -29,6 +30,17 @@ func TestAdd(t *testing.T) {
2930
is.Equal(mhash, "QmUfZ9rAdhV5ioBzXKdUTh2ZNsz9bzbkaLVyQ8uc8pj21F")
3031
}
3132

33+
func TestRedirect(t *testing.T) {
34+
is := is.New(t)
35+
s := NewShell(shellUrl)
36+
37+
err := s.
38+
Request("/version").
39+
Exec(context.Background(), nil)
40+
is.NotNil(err)
41+
is.True(strings.Contains(err.Error(), "unexpected redirect"))
42+
}
43+
3244
func TestAddWithCat(t *testing.T) {
3345
is := is.New(t)
3446
s := NewShell(shellUrl)

0 commit comments

Comments
 (0)