diff options
author | Lewis Marshall <lewis@lmars.net> | 2017-07-25 17:51:26 +0800 |
---|---|---|
committer | Felix Lange <fjl@users.noreply.github.com> | 2017-07-25 17:51:26 +0800 |
commit | f4841ff43dda9f41a47ec94b5a5356023eec59d4 (patch) | |
tree | 230bdce1fb090599bb4ed97c5aae416534335d65 /swarm/api/http/server_test.go | |
parent | 3a678a15c92e287c90eabab75463bbed97ba832c (diff) | |
download | go-tangerine-f4841ff43dda9f41a47ec94b5a5356023eec59d4.tar go-tangerine-f4841ff43dda9f41a47ec94b5a5356023eec59d4.tar.gz go-tangerine-f4841ff43dda9f41a47ec94b5a5356023eec59d4.tar.bz2 go-tangerine-f4841ff43dda9f41a47ec94b5a5356023eec59d4.tar.lz go-tangerine-f4841ff43dda9f41a47ec94b5a5356023eec59d4.tar.xz go-tangerine-f4841ff43dda9f41a47ec94b5a5356023eec59d4.tar.zst go-tangerine-f4841ff43dda9f41a47ec94b5a5356023eec59d4.zip |
swarm/api/http: redirect root manifest requests to include trailing slash (#14806)
Diffstat (limited to 'swarm/api/http/server_test.go')
-rw-r--r-- | swarm/api/http/server_test.go | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/swarm/api/http/server_test.go b/swarm/api/http/server_test.go index 0b124a19a..14abd1df4 100644 --- a/swarm/api/http/server_test.go +++ b/swarm/api/http/server_test.go @@ -18,12 +18,16 @@ package http_test import ( "bytes" + "errors" + "fmt" "io/ioutil" "net/http" "sync" "testing" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/swarm/api" + swarm "github.com/ethereum/go-ethereum/swarm/api/client" "github.com/ethereum/go-ethereum/swarm/storage" "github.com/ethereum/go-ethereum/swarm/testutil" ) @@ -128,3 +132,61 @@ func TestBzzrGetPath(t *testing.T) { } } + +// TestBzzRootRedirect tests that getting the root path of a manifest without +// a trailing slash gets redirected to include the trailing slash so that +// relative URLs work as expected. +func TestBzzRootRedirect(t *testing.T) { + srv := testutil.NewTestSwarmServer(t) + defer srv.Close() + + // create a manifest with some data at the root path + client := swarm.NewClient(srv.URL) + data := []byte("data") + file := &swarm.File{ + ReadCloser: ioutil.NopCloser(bytes.NewReader(data)), + ManifestEntry: api.ManifestEntry{ + Path: "", + ContentType: "text/plain", + Size: int64(len(data)), + }, + } + hash, err := client.Upload(file, "") + if err != nil { + t.Fatal(err) + } + + // define a CheckRedirect hook which ensures there is only a single + // redirect to the correct URL + redirected := false + httpClient := http.Client{ + CheckRedirect: func(req *http.Request, via []*http.Request) error { + if redirected { + return errors.New("too many redirects") + } + redirected = true + expectedPath := "/bzz:/" + hash + "/" + if req.URL.Path != expectedPath { + return fmt.Errorf("expected redirect to %q, got %q", expectedPath, req.URL.Path) + } + return nil + }, + } + + // perform the GET request and assert the response + res, err := httpClient.Get(srv.URL + "/bzz:/" + hash) + if err != nil { + t.Fatal(err) + } + defer res.Body.Close() + if !redirected { + t.Fatal("expected GET /bzz:/<hash> to redirect to /bzz:/<hash>/ but it didn't") + } + gotData, err := ioutil.ReadAll(res.Body) + if err != nil { + t.Fatal(err) + } + if !bytes.Equal(gotData, data) { + t.Fatalf("expected response to equal %q, got %q", data, gotData) + } +} |