Skip to content

Commit 5a12337

Browse files
authored
Merge pull request #3 from ice-cronus/master
no-op body closer
2 parents 646b373 + 87bcf10 commit 5a12337

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

client.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func (c *Client) WithContext(ctx context.Context) *Client {
7373
}
7474

7575
// GetUpload obtains an upload by location. Fills `u` variable with upload info.
76-
// Returns http response from server (with closed body) and error (if any).
76+
// Returns http response from server and error (if any).
7777
//
7878
// For regular upload we fill in just a remote offset and set Partial flag. For final concatenated uploads we also
7979
// may set upload size (if server provided). Also, we may set remote offset to OffsetUnknown for concatenated final
@@ -143,7 +143,7 @@ func (c *Client) GetUpload(u *Upload, location string) (response *http.Response,
143143
}
144144

145145
// CreateUpload creates upload on the server. Fills `u` with upload that was created.
146-
// Returns http response from server (with closed body) and error (if any).
146+
// Returns http response from server and error (if any).
147147
//
148148
// Server must support "creation" extension. We create an upload with given size and metadata.
149149
// If Partial flag is true, we create a partial upload. Metadata map keys must not contain spaces.
@@ -264,7 +264,8 @@ func (c *Client) CreateUploadWithData(u *Upload, data []byte, remoteSize int64,
264264
}
265265

266266
// DeleteUpload deletes an upload. Receives `u` with upload to be deleted. Returns http response from server
267-
// (with closed body) and error (if any).
267+
//
268+
// and error (if any).
268269
//
269270
// Server must support "termination" extension to be able to delete uploads.
270271
//
@@ -303,7 +304,8 @@ func (c *Client) DeleteUpload(u Upload) (response *http.Response, err error) {
303304

304305
// ConcatenateUploads makes a request to concatenate the partial uploads created before into one final upload. Fills
305306
// `final` with upload that was created. Returns http response from server
306-
// (with closed body) and error (if any).
307+
//
308+
// and error (if any).
307309
//
308310
// Server must support "concatenation" extension for this feature. Typically, partial uploads must be fully uploaded
309311
// to the server, but if server supports "concatenation-unfinished" extension, it may accept unfinished uploads.
@@ -364,7 +366,8 @@ func (c *Client) ConcatenateUploads(final *Upload, partials []Upload, meta map[s
364366

365367
// ConcatenateStreams makes a request to concatenate partial uploads from given streams into one final upload. Final
366368
// Upload object will be filled with location of a created final upload. Returns http response from server
367-
// (with closed body) and error (if any).
369+
//
370+
// and error (if any).
368371
//
369372
// Server must support "concatenation" extension for this feature. Streams with pointers that not point to an end of
370373
// streams are treated as unfinished -- server must support "concatenation-unfinished" in this case.
@@ -390,7 +393,7 @@ func (c *Client) ConcatenateStreams(final *Upload, streams []*UploadStream, meta
390393
}
391394

392395
// UpdateCapabilities gathers server capabilities and updates Capabilities client variable. Returns http response
393-
// from server (with closed body) and error (if any).
396+
// from server and error (if any).
394397
func (c *Client) UpdateCapabilities() (response *http.Response, err error) {
395398
var req *http.Request
396399
if req, err = c.GetRequest(http.MethodOptions, c.BaseURL.String(), nil, c, c.client); err != nil {
@@ -436,7 +439,18 @@ func (c *Client) tusRequest(ctx context.Context, req *http.Request) (response *h
436439
if err == nil && response.StatusCode == http.StatusPreconditionFailed {
437440
versions := response.Header.Get("Tus-Version")
438441
err = ErrProtocol.WithText(fmt.Sprintf("request protocol version %q, server supported versions are %q", c.ProtocolVersion, versions))
442+
return
439443
}
444+
if response != nil && response.Body != nil {
445+
var bodyBytes []byte
446+
bodyBytes, err = io.ReadAll(response.Body)
447+
if err != nil {
448+
return
449+
}
450+
response.Body.Close() // Close the original body
451+
response.Body = io.NopCloser(bytes.NewReader(bodyBytes))
452+
}
453+
440454
return
441455
}
442456

client_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,13 @@ var _ = Describe("Client", func() {
7373
Context("tusRequest", func() {
7474
Context("happy path", func() {
7575
It("should make a request, return response", func() {
76-
srvMock.AddMocks(tRequest(http.MethodGet, "/foo", tusHeaders).Reply(tReply(reply.OK())))
76+
srvMock.AddMocks(tRequest(http.MethodGet, "/foo", tusHeaders).Reply(tReply(reply.OK()).Body([]byte("test"))))
7777
req, err := http.NewRequest(http.MethodGet, srvMock.URL()+"/foo", nil)
7878
Ω(err).Should(Succeed())
79-
80-
Ω(testClient.tusRequest(context.Background(), req)).ShouldNot(BeNil())
79+
resp, err := testClient.tusRequest(context.Background(), req)
80+
Ω(err).Should(Succeed())
81+
Ω(resp).ShouldNot(BeNil())
82+
Ω(resp).Should(HaveHTTPBody([]byte("test")))
8183
})
8284
When("OPTIONS request", func() {
8385
It("should not set Tus-Resumable header", func() {

0 commit comments

Comments
 (0)