Jesus / dropbox_api

Ruby client library for Dropbox API v2
MIT License
171 stars 113 forks source link

Cursor offset doesn't update when using `upload_session_append_v2` #24

Closed Aupajo closed 7 years ago

Aupajo commented 7 years ago

I've been experimenting with the upload session API, and I think I've found a bug not covered by the current test cases.

commit = DropboxApi::Metadata::CommitInfo.new({
  "path" => "/test.txt",
  "mode" => :add
})

# Upload the first three bytes
cursor = client.upload_session_start('abc')

# Cursor offset has been correctly set to 3 at this point
expect(cursor.offset).to eq(3)

# Upload the second three bytes
client.upload_session_append_v2(cursor, 'def')

# Cursor offset *should* be set to 6 at this point, but remains 3
# expect(cursor.offset).to eq(6)

# Attempt to commit the upload
client.upload_session_finish(cursor, commit)

This raises an error:

DropboxApi::Errors::UploadSessionOffsetError:
  lookup_failed/incorrect_offset/..., correct offset: 6

This happens because the offset of the cursor never changes from 3 to 6 after the call to upload_session_append_v2.

This hack will cause the test to pass:

cursor = client.upload_session_start('abc')
client.upload_session_append_v2(cursor, 'def')

# Manually update the offset
cursor.instance_variable_set(:@offset, 6)

client.upload_session_finish(cursor, commit)

There are test cases covering upload_session_append_v2 and upload_session_finish, but never the two together, which would otherwise reveal this problem.

I believe the correct behaviour is to have upload_session_append_v2 update the cursors offset after a successful request, and I'm working on a fix for this at the moment – let me know if you have any thoughts on this in the meantime.