From 2cee83a25edc1e722a6c9470916eaecbbab8987b Mon Sep 17 00:00:00 2001 From: "Morrison-Reed Elliot (BEG/EVS1-NA)" Date: Tue, 1 Dec 2020 11:02:05 -0500 Subject: [PATCH 1/5] added relative url support --- client.go | 21 +++++++++++++-------- config.go | 2 ++ 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/client.go b/client.go index b5923d1..4b95cc4 100644 --- a/client.go +++ b/client.go @@ -5,6 +5,7 @@ import ( "io/ioutil" "net/http" netUrl "net/url" + "path" "strconv" ) @@ -97,15 +98,19 @@ func (c *Client) CreateUpload(u *Upload) (*Uploader, error) { return nil, ErrUrlNotRecognized } - newUrl, err := netUrl.Parse(url) - if err != nil { - return nil, ErrUrlNotRecognized - } - if newUrl.Scheme == "" { - newUrl.Scheme = baseUrl.Scheme - url = newUrl.String() + if c.Config.RelativeURL { + baseUrl.Path = path.Join(baseUrl.Path, url) + url = baseUrl.String() + } else { + newUrl, err := netUrl.Parse(url) + if err != nil { + return nil, ErrUrlNotRecognized + } + if newUrl.Scheme == "" { + newUrl.Scheme = baseUrl.Scheme + url = newUrl.String() + } } - if c.Config.Resume { c.Config.Store.Set(u.Fingerprint, url) } diff --git a/config.go b/config.go index da96962..1ff1915 100644 --- a/config.go +++ b/config.go @@ -12,6 +12,8 @@ type Config struct { Resume bool // OverridePatchMethod allow to by pass proxies sendind a POST request instead of PATCH. OverridePatchMethod bool + // RelativeURL allows support for a relative Location header in the creation response + RelativeURL bool // Store map an upload's fingerprint with the corresponding upload URL. // If Resume is true the Store is required. Store Store From 5a6cccee8e9db81e4d5cc6b1a070ba35a755997c Mon Sep 17 00:00:00 2001 From: "Morrison-Reed Elliot (BEG/EVS1-NA)" Date: Tue, 1 Dec 2020 15:07:04 -0500 Subject: [PATCH 2/5] added test for RelativeURL flag --- client_test.go | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/client_test.go b/client_test.go index bf2108f..4eccdcd 100644 --- a/client_test.go +++ b/client_test.go @@ -264,7 +264,6 @@ func (s *UploadTestSuite) TestConcurrentUploads() { } func (s *UploadTestSuite) TestResumeUpload() { - ctx, cancel := context.WithCancel(context.Background()) defer cancel() @@ -325,6 +324,25 @@ func (s *UploadTestSuite) TestResumeUpload() { s.EqualValues(1048576*150, fi.Size) } +func (s *UploadTestSuite) TestCreateUploadRelativeURL() { + srv := httptest.NewServer(http.HandlerFunc( + func(rw http.ResponseWriter, r *http.Request) { + rw.Header().Add("Location", "xyz") + rw.WriteHeader(http.StatusCreated) + }, + )) + defer srv.Close() + + cfg := DefaultConfig() + cfg.RelativeURL = true + + client, err := NewClient(srv.URL, cfg) + s.NoError(err) + upload, err := client.CreateUpload(NewUploadFromBytes([]byte("test"))) + s.NoError(err) + s.Equal(srv.URL + "/xyz", upload.url) +} + func TestUploadTestSuite(t *testing.T) { suite.Run(t, new(UploadTestSuite)) } From b805c9296ed1f05bc6ad9f6a374a3cce49fb144d Mon Sep 17 00:00:00 2001 From: "Morrison-Reed Elliot (BEG/EVS1-NA)" Date: Tue, 1 Dec 2020 15:22:46 -0500 Subject: [PATCH 3/5] use missing scheme to autodetect relative path --- client.go | 15 +++++---------- client_test.go | 5 +---- config.go | 2 -- 3 files changed, 6 insertions(+), 16 deletions(-) diff --git a/client.go b/client.go index 4b95cc4..b5f259e 100644 --- a/client.go +++ b/client.go @@ -98,18 +98,13 @@ func (c *Client) CreateUpload(u *Upload) (*Uploader, error) { return nil, ErrUrlNotRecognized } - if c.Config.RelativeURL { + newUrl, err := netUrl.Parse(url) + if err != nil { + return nil, ErrUrlNotRecognized + } + if newUrl.Scheme == "" { baseUrl.Path = path.Join(baseUrl.Path, url) url = baseUrl.String() - } else { - newUrl, err := netUrl.Parse(url) - if err != nil { - return nil, ErrUrlNotRecognized - } - if newUrl.Scheme == "" { - newUrl.Scheme = baseUrl.Scheme - url = newUrl.String() - } } if c.Config.Resume { c.Config.Store.Set(u.Fingerprint, url) diff --git a/client_test.go b/client_test.go index 4eccdcd..dc2647c 100644 --- a/client_test.go +++ b/client_test.go @@ -333,10 +333,7 @@ func (s *UploadTestSuite) TestCreateUploadRelativeURL() { )) defer srv.Close() - cfg := DefaultConfig() - cfg.RelativeURL = true - - client, err := NewClient(srv.URL, cfg) + client, err := NewClient(srv.URL, DefaultConfig()) s.NoError(err) upload, err := client.CreateUpload(NewUploadFromBytes([]byte("test"))) s.NoError(err) diff --git a/config.go b/config.go index 1ff1915..da96962 100644 --- a/config.go +++ b/config.go @@ -12,8 +12,6 @@ type Config struct { Resume bool // OverridePatchMethod allow to by pass proxies sendind a POST request instead of PATCH. OverridePatchMethod bool - // RelativeURL allows support for a relative Location header in the creation response - RelativeURL bool // Store map an upload's fingerprint with the corresponding upload URL. // If Resume is true the Store is required. Store Store From 2ef7a1ec4d09f71b0cb180eb6d8a21eb5a747917 Mon Sep 17 00:00:00 2001 From: "Morrison-Reed Elliot (BEG/EVS1-NA)" Date: Tue, 1 Dec 2020 15:26:03 -0500 Subject: [PATCH 4/5] remove unrelated change to TestResumeUpload --- client_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/client_test.go b/client_test.go index dc2647c..e479a59 100644 --- a/client_test.go +++ b/client_test.go @@ -264,6 +264,7 @@ func (s *UploadTestSuite) TestConcurrentUploads() { } func (s *UploadTestSuite) TestResumeUpload() { + ctx, cancel := context.WithCancel(context.Background()) defer cancel() From 0888ace2bfc61244522d41a47073a965d8278447 Mon Sep 17 00:00:00 2001 From: "Morrison-Reed Elliot (BEG/EVS1-NA)" Date: Tue, 1 Dec 2020 15:26:18 -0500 Subject: [PATCH 5/5] go fmt --- client_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/client_test.go b/client_test.go index e479a59..2ed41a3 100644 --- a/client_test.go +++ b/client_test.go @@ -264,7 +264,7 @@ func (s *UploadTestSuite) TestConcurrentUploads() { } func (s *UploadTestSuite) TestResumeUpload() { - + ctx, cancel := context.WithCancel(context.Background()) defer cancel() @@ -338,7 +338,7 @@ func (s *UploadTestSuite) TestCreateUploadRelativeURL() { s.NoError(err) upload, err := client.CreateUpload(NewUploadFromBytes([]byte("test"))) s.NoError(err) - s.Equal(srv.URL + "/xyz", upload.url) + s.Equal(srv.URL+"/xyz", upload.url) } func TestUploadTestSuite(t *testing.T) {