PageRenderTime 28ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/client/storage/dropbox/test/src/client_test.coffee

https://bitbucket.org/coderaiser/cloudcmd
CoffeeScript | 1492 lines | 1294 code | 164 blank | 34 comment | 48 complexity | 56e31d39f4b077f7f3a890f03ad58074 MD5 | raw file
Possible License(s): MIT, Apache-2.0, MPL-2.0-no-copyleft-exception, BSD-3-Clause

Large files files are truncated, but you can click here to view the full file

  1. buildClientTests = (clientKeys) ->
  2. # Creates the global client.
  3. setupClient = (test, done) ->
  4. # Should only be used for fixture teardown.
  5. test.__client = new Dropbox.Client clientKeys
  6. done()
  7. # Creates the test directory.
  8. setupDirectory = (test, done) ->
  9. # True if running on node.js
  10. test.node_js = module? and module?.exports? and require?
  11. # All test data should go here.
  12. test.testFolder = '/js tests.' + Math.random().toString(36)
  13. test.__client.mkdir test.testFolder, (error, stat) ->
  14. expect(error).to.equal null
  15. done()
  16. # Creates the binary image file in the test directory.
  17. setupImageFile = (test, done) ->
  18. test.imageFile = "#{test.testFolder}/test-binary-image.png"
  19. test.imageFileData = testImageBytes
  20. setupImageFileUsingArrayBuffer test, (success) ->
  21. if success
  22. return done()
  23. setupImageFileUsingBlob test, (success) ->
  24. if success
  25. return done()
  26. setupImageFileUsingString test, done
  27. # Standard-compliant browsers write via XHR#send(ArrayBufferView).
  28. setupImageFileUsingArrayBuffer = (test, done) ->
  29. if Uint8Array? and (not test.node_js)
  30. testImageServerOn()
  31. xhr = new Dropbox.Xhr 'GET', testImageUrl
  32. xhr.setResponseType('arraybuffer').prepare().send (error, buffer) =>
  33. testImageServerOff()
  34. expect(error).to.equal null
  35. test.__client.writeFile test.imageFile, buffer, (error, stat) ->
  36. expect(error).to.equal null
  37. # Some browsers will send the '[object Uint8Array]' string instead of
  38. # the ArrayBufferView.
  39. if stat.size is buffer.byteLength
  40. test.imageFileTag = stat.versionTag
  41. done true
  42. else
  43. done false
  44. else
  45. done false
  46. # Fallback to XHR#send(Blob).
  47. setupImageFileUsingBlob = (test, done) ->
  48. if Blob? and (not test.node_js)
  49. testImageServerOn()
  50. xhr = new Dropbox.Xhr 'GET', testImageUrl
  51. xhr.setResponseType('arraybuffer').prepare().send (error, buffer) =>
  52. testImageServerOff()
  53. expect(error).to.equal null
  54. blob = new Blob [buffer], type: 'image/png'
  55. test.__client.writeFile test.imageFile, blob, (error, stat) ->
  56. expect(error).to.equal null
  57. if stat.size is blob.size
  58. test.imageFileTag = stat.versionTag
  59. done true
  60. else
  61. done false
  62. else
  63. done false
  64. # Last resort: send a string that will get crushed by encoding errors.
  65. setupImageFileUsingString = (test, done) ->
  66. test.__client.writeFile(test.imageFile, test.imageFileData,
  67. { binary: true },
  68. (error, stat) ->
  69. expect(error).to.equal null
  70. test.imageFileTag = stat.versionTag
  71. done()
  72. )
  73. # Creates the plaintext file in the test directory.
  74. setupTextFile = (test, done) ->
  75. test.textFile = "#{test.testFolder}/test-file.txt"
  76. test.textFileData = "Plaintext test file #{Math.random().toString(36)}.\n"
  77. test.__client.writeFile(test.textFile, test.textFileData,
  78. (error, stat) ->
  79. expect(error).to.equal null
  80. test.textFileTag = stat.versionTag
  81. done()
  82. )
  83. # Global (expensive) fixtures.
  84. before (done) ->
  85. @timeout 10 * 1000
  86. setupClient this, =>
  87. setupDirectory this, =>
  88. setupImageFile this, =>
  89. setupTextFile this, ->
  90. done()
  91. # Teardown for global fixtures.
  92. after (done) ->
  93. @__client.remove @testFolder, (error, stat) =>
  94. @test.error(new Error(error)) if error
  95. done()
  96. # Per-test (cheap) fixtures.
  97. beforeEach ->
  98. @client = new Dropbox.Client clientKeys
  99. describe 'URLs for custom API server', ->
  100. it 'computes the other URLs correctly', ->
  101. client = new Dropbox.Client
  102. key: clientKeys.key,
  103. secret: clientKeys.secret,
  104. server: 'https://api.sandbox.dropbox-proxy.com'
  105. expect(client.apiServer).to.equal(
  106. 'https://api.sandbox.dropbox-proxy.com')
  107. expect(client.authServer).to.equal(
  108. 'https://www.sandbox.dropbox-proxy.com')
  109. expect(client.fileServer).to.equal(
  110. 'https://api-content.sandbox.dropbox-proxy.com')
  111. describe '#normalizePath', ->
  112. it "doesn't touch relative paths", ->
  113. expect(@client.normalizePath('aa/b/cc/dd')).to.equal 'aa/b/cc/dd'
  114. it 'removes the leading / from absolute paths', ->
  115. expect(@client.normalizePath('/aaa/b/cc/dd')).to.equal 'aaa/b/cc/dd'
  116. it 'removes multiple leading /s from absolute paths', ->
  117. expect(@client.normalizePath('///aa/b/ccc/dd')).to.equal 'aa/b/ccc/dd'
  118. describe '#urlEncodePath', ->
  119. it 'encodes each segment separately', ->
  120. expect(@client.urlEncodePath('a b+c/d?e"f/g&h')).to.
  121. equal "a%20b%2Bc/d%3Fe%22f/g%26h"
  122. it 'normalizes paths', ->
  123. expect(@client.urlEncodePath('///a b+c/g&h')).to.
  124. equal "a%20b%2Bc/g%26h"
  125. describe '#dropboxUid', ->
  126. it 'matches the uid in the credentials', ->
  127. expect(@client.dropboxUid()).to.equal clientKeys.uid
  128. describe '#getUserInfo', ->
  129. it 'returns reasonable information', (done) ->
  130. @client.getUserInfo (error, userInfo, rawUserInfo) ->
  131. expect(error).to.equal null
  132. expect(userInfo).to.be.instanceOf Dropbox.UserInfo
  133. expect(userInfo.uid).to.equal clientKeys.uid
  134. expect(rawUserInfo).not.to.be.instanceOf Dropbox.UserInfo
  135. expect(rawUserInfo).to.have.property 'uid'
  136. done()
  137. describe 'with httpCache', ->
  138. beforeEach ->
  139. @xhr = null
  140. @client.onXhr.addListener (xhr) =>
  141. @xhr = xhr
  142. it 'uses Authorization headers', (done) ->
  143. @client.getUserInfo httpCache: true, (error, userInfo, rawUserInfo) =>
  144. if Dropbox.Xhr.ieXdr # IE's XDR doesn't do headers
  145. expect(@xhr.url).to.contain 'oauth_nonce'
  146. else
  147. expect(@xhr.headers).to.have.key 'Authorization'
  148. expect(error).to.equal null
  149. expect(userInfo).to.be.instanceOf Dropbox.UserInfo
  150. expect(userInfo.uid).to.equal clientKeys.uid
  151. expect(rawUserInfo).not.to.be.instanceOf Dropbox.UserInfo
  152. expect(rawUserInfo).to.have.property 'uid'
  153. done()
  154. describe '#mkdir', ->
  155. afterEach (done) ->
  156. return done() unless @newFolder
  157. @client.remove @newFolder, (error, stat) -> done()
  158. it 'creates a folder in the test folder', (done) ->
  159. @newFolder = "#{@testFolder}/test'folder"
  160. @client.mkdir @newFolder, (error, stat) =>
  161. expect(error).to.equal null
  162. expect(stat).to.be.instanceOf Dropbox.Stat
  163. expect(stat.path).to.equal @newFolder
  164. expect(stat.isFolder).to.equal true
  165. @client.stat @newFolder, (error, stat) =>
  166. expect(error).to.equal null
  167. expect(stat.isFolder).to.equal true
  168. done()
  169. describe '#readFile', ->
  170. it 'reads a text file', (done) ->
  171. @client.readFile @textFile, (error, data, stat) =>
  172. expect(error).to.equal null
  173. expect(data).to.equal @textFileData
  174. unless Dropbox.Xhr.ieXdr # IE's XDR doesn't do headers.
  175. expect(stat).to.be.instanceOf Dropbox.Stat
  176. expect(stat.path).to.equal @textFile
  177. expect(stat.isFile).to.equal true
  178. done()
  179. it 'reads the beginning of a text file', (done) ->
  180. return done() if Dropbox.Xhr.ieXdr # IE's XDR doesn't do headers.
  181. @client.readFile @textFile, start: 0, length: 10, (error, data, stat) =>
  182. expect(error).to.equal null
  183. expect(data).to.equal @textFileData.substring(0, 10)
  184. expect(stat).to.be.instanceOf Dropbox.Stat
  185. expect(stat.path).to.equal @textFile
  186. expect(stat.isFile).to.equal true
  187. done()
  188. it 'reads the middle of a text file', (done) ->
  189. return done() if Dropbox.Xhr.ieXdr # IE's XDR doesn't do headers.
  190. @client.readFile @textFile, start: 8, length: 10, (error, data, stat) =>
  191. expect(error).to.equal null
  192. expect(data).to.equal @textFileData.substring(8, 18)
  193. expect(stat).to.be.instanceOf Dropbox.Stat
  194. expect(stat.path).to.equal @textFile
  195. expect(stat.isFile).to.equal true
  196. done()
  197. it 'reads the end of a text file via the start: option', (done) ->
  198. return done() if Dropbox.Xhr.ieXdr # IE's XDR doesn't do headers.
  199. @client.readFile @textFile, start: 10, (error, data, stat) =>
  200. expect(error).to.equal null
  201. expect(data).to.equal @textFileData.substring(10)
  202. expect(stat).to.be.instanceOf Dropbox.Stat
  203. expect(stat.path).to.equal @textFile
  204. expect(stat.isFile).to.equal true
  205. done()
  206. it 'reads the end of a text file via the length: option', (done) ->
  207. return done() if Dropbox.Xhr.ieXdr # IE's XDR doesn't do headers.
  208. @client.readFile @textFile, length: 10, (error, data, stat) =>
  209. expect(error).to.equal null
  210. expect(data).to.
  211. equal @textFileData.substring(@textFileData.length - 10)
  212. expect(stat).to.be.instanceOf Dropbox.Stat
  213. expect(stat.path).to.equal @textFile
  214. expect(stat.isFile).to.equal true
  215. done()
  216. it 'reads a binary file into a string', (done) ->
  217. @client.readFile @imageFile, binary: true, (error, data, stat) =>
  218. expect(error).to.equal null
  219. expect(data).to.equal @imageFileData
  220. unless Dropbox.Xhr.ieXdr # IE's XDR doesn't do headers.
  221. expect(stat).to.be.instanceOf Dropbox.Stat
  222. expect(stat.path).to.equal @imageFile
  223. expect(stat.isFile).to.equal true
  224. done()
  225. it 'reads a binary file into a Blob', (done) ->
  226. return done() unless Blob?
  227. @client.readFile @imageFile, blob: true, (error, blob, stat) =>
  228. expect(error).to.equal null
  229. expect(blob).to.be.instanceOf Blob
  230. unless Dropbox.Xhr.ieXdr # IE's XDR doesn't do headers.
  231. expect(stat).to.be.instanceOf Dropbox.Stat
  232. expect(stat.path).to.equal @imageFile
  233. expect(stat.isFile).to.equal true
  234. reader = new FileReader
  235. reader.onloadend = =>
  236. return unless reader.readyState == FileReader.DONE
  237. buffer = reader.result
  238. view = new Uint8Array buffer
  239. length = buffer.byteLength
  240. bytes = (String.fromCharCode view[i] for i in [0...length]).
  241. join('')
  242. expect(bytes).to.equal @imageFileData
  243. done()
  244. reader.readAsArrayBuffer blob
  245. it 'reads a binary file into an ArrayBuffer', (done) ->
  246. return done() unless ArrayBuffer?
  247. @client.readFile @imageFile, arrayBuffer: true, (error, buffer, stat) =>
  248. expect(error).to.equal null
  249. expect(buffer).to.be.instanceOf ArrayBuffer
  250. unless Dropbox.Xhr.ieXdr # IE's XDR doesn't do headers.
  251. expect(stat).to.be.instanceOf Dropbox.Stat
  252. expect(stat.path).to.equal @imageFile
  253. expect(stat.isFile).to.equal true
  254. view = new Uint8Array buffer
  255. length = buffer.byteLength
  256. bytes = (String.fromCharCode view[i] for i in [0...length]).
  257. join('')
  258. expect(bytes).to.equal @imageFileData
  259. done()
  260. describe 'with an onXhr listener', ->
  261. beforeEach ->
  262. @listenerXhr = null
  263. @callbackCalled = false
  264. it 'calls the listener with a Dropbox.Xhr argument', (done) ->
  265. @client.onXhr.addListener (xhr) =>
  266. expect(xhr).to.be.instanceOf Dropbox.Xhr
  267. @listenerXhr = xhr
  268. true
  269. @client.readFile @textFile, (error, data, stat) =>
  270. expect(error).to.equal null
  271. expect(data).to.equal @textFileData
  272. done() if @listenerXhr
  273. it 'calls the listener before firing the XHR', (done) ->
  274. @client.onXhr.addListener (xhr) =>
  275. unless Dropbox.Xhr.ieXdr # IE's XHR doesn't have readyState
  276. expect(xhr.xhr.readyState).to.equal 1
  277. expect(@callbackCalled).to.equal false
  278. @listenerXhr = xhr
  279. true
  280. @client.readFile @textFile, (error, data, stat) =>
  281. @callbackCalled = true
  282. expect(@listenerXhr).to.be.instanceOf Dropbox.Xhr
  283. expect(error).to.equal null
  284. expect(data).to.equal @textFileData
  285. done() if @listenerXhr
  286. it 'does not send the XHR if the listener cancels the event', (done) ->
  287. @client.onXhr.addListener (xhr) =>
  288. expect(@callbackCalled).to.equal false
  289. @listenerXhr = xhr
  290. # NOTE: if the client calls send(), a DOM error will fail the test
  291. xhr.send()
  292. false
  293. @client.readFile @textFile, (error, data, stat) =>
  294. @callbackCalled = true
  295. expect(@listenerXhr).to.be.instanceOf Dropbox.Xhr
  296. done() if @listenerXhr
  297. describe 'with httpCache', ->
  298. beforeEach ->
  299. @xhr = null
  300. @client.onXhr.addListener (xhr) =>
  301. @xhr = xhr
  302. it 'reads a text file using Authorization headers', (done) ->
  303. @client.readFile @textFile, httpCache: true, (error, data, stat) =>
  304. if Dropbox.Xhr.ieXdr # IE's XDR doesn't do headers
  305. expect(@xhr.url).to.contain 'oauth_nonce'
  306. else
  307. expect(@xhr.headers).to.have.key 'Authorization'
  308. expect(error).to.equal null
  309. expect(data).to.equal @textFileData
  310. unless Dropbox.Xhr.ieXdr # IE's XDR doesn't do headers.
  311. expect(stat).to.be.instanceOf Dropbox.Stat
  312. expect(stat.path).to.equal @textFile
  313. expect(stat.isFile).to.equal true
  314. done()
  315. describe '#writeFile', ->
  316. afterEach (done) ->
  317. return done() unless @newFile
  318. @client.remove @newFile, (error, stat) -> done()
  319. it 'writes a new text file', (done) ->
  320. @newFile = "#{@testFolder}/another text file.txt"
  321. @newFileData = "Another plaintext file #{Math.random().toString(36)}."
  322. @client.writeFile @newFile, @newFileData, (error, stat) =>
  323. expect(error).to.equal null
  324. expect(stat).to.be.instanceOf Dropbox.Stat
  325. expect(stat.path).to.equal @newFile
  326. expect(stat.isFile).to.equal true
  327. @client.readFile @newFile, (error, data, stat) =>
  328. expect(error).to.equal null
  329. expect(data).to.equal @newFileData
  330. unless Dropbox.Xhr.ieXdr # IE's XDR doesn't do headers.
  331. expect(stat).to.be.instanceOf Dropbox.Stat
  332. expect(stat.path).to.equal @newFile
  333. expect(stat.isFile).to.equal true
  334. done()
  335. it 'writes a new empty file', (done) ->
  336. @newFile = "#{@testFolder}/another text file.txt"
  337. @newFileData = ''
  338. @client.writeFile @newFile, @newFileData, (error, stat) =>
  339. expect(error).to.equal null
  340. expect(stat).to.be.instanceOf Dropbox.Stat
  341. expect(stat.path).to.equal @newFile
  342. expect(stat.isFile).to.equal true
  343. @client.readFile @newFile, (error, data, stat) =>
  344. expect(error).to.equal null
  345. expect(data).to.equal @newFileData
  346. unless Dropbox.Xhr.ieXdr # IE's XDR doesn't do headers.
  347. expect(stat).to.be.instanceOf Dropbox.Stat
  348. expect(stat.path).to.equal @newFile
  349. expect(stat.isFile).to.equal true
  350. done()
  351. it 'writes a Blob to a binary file', (done) ->
  352. return done() unless Blob? and ArrayBuffer?
  353. @newFile = "#{@testFolder}/test image from blob.png"
  354. newBuffer = new ArrayBuffer @imageFileData.length
  355. newBytes = new Uint8Array newBuffer
  356. for i in [0...@imageFileData.length]
  357. newBytes[i] = @imageFileData.charCodeAt i
  358. @newBlob = new Blob [newBytes], type: 'image/png'
  359. if @newBlob.size isnt newBuffer.byteLength
  360. @newBlob = new Blob [newBuffer], type: 'image/png'
  361. @client.writeFile @newFile, @newBlob, (error, stat) =>
  362. expect(error).to.equal null
  363. expect(stat).to.be.instanceOf Dropbox.Stat
  364. expect(stat.path).to.equal @newFile
  365. expect(stat.isFile).to.equal true
  366. @client.readFile @newFile, arrayBuffer: true,
  367. (error, buffer, stat) =>
  368. expect(error).to.equal null
  369. expect(buffer).to.be.instanceOf ArrayBuffer
  370. expect(stat).to.be.instanceOf Dropbox.Stat
  371. expect(stat.path).to.equal @newFile
  372. expect(stat.isFile).to.equal true
  373. view = new Uint8Array buffer
  374. length = buffer.byteLength
  375. bytes = (String.fromCharCode view[i] for i in [0...length]).
  376. join('')
  377. expect(bytes).to.equal @imageFileData
  378. done()
  379. it 'writes a File to a binary file', (done) ->
  380. return done() unless File? and Blob? and ArrayBuffer?
  381. @newFile = "#{@testFolder}/test image from blob.png"
  382. newBuffer = new ArrayBuffer @imageFileData.length
  383. newBytes = new Uint8Array newBuffer
  384. for i in [0...@imageFileData.length]
  385. newBytes[i] = @imageFileData.charCodeAt i
  386. newBlob = new Blob [newBytes], type: 'image/png'
  387. # Called when we have a File wrapping newBlob.
  388. actualTestCase = (file) =>
  389. @newFileObject = file
  390. @client.writeFile @newFile, @newFileObject, (error, stat) =>
  391. expect(error).to.equal null
  392. expect(stat).to.be.instanceOf Dropbox.Stat
  393. expect(stat.path).to.equal @newFile
  394. expect(stat.isFile).to.equal true
  395. @client.readFile @newFile, arrayBuffer: true,
  396. (error, buffer, stat) =>
  397. expect(error).to.equal null
  398. expect(buffer).to.be.instanceOf ArrayBuffer
  399. expect(stat).to.be.instanceOf Dropbox.Stat
  400. expect(stat.path).to.equal @newFile
  401. expect(stat.isFile).to.equal true
  402. view = new Uint8Array buffer
  403. length = buffer.byteLength
  404. bytes = (String.fromCharCode view[i] for i in [0...length]).
  405. join('')
  406. expect(bytes).to.equal @imageFileData
  407. done()
  408. # TODO(pwnall): use lighter method of constructing a File, when available
  409. # http://crbug.com/164933
  410. return done() if typeof webkitRequestFileSystem is 'undefined'
  411. webkitRequestFileSystem window.TEMPORARY, 1024 * 1024, (fileSystem) ->
  412. # NOTE: the File name is different from the uploaded file name, to
  413. # catch bugs such as http://crbug.com/165095
  414. fileSystem.root.getFile 'test image file.png',
  415. create: true, exclusive: false, (fileEntry) ->
  416. fileEntry.createWriter (fileWriter) ->
  417. fileWriter.onwriteend = ->
  418. fileEntry.file (file) ->
  419. actualTestCase file
  420. fileWriter.write newBlob
  421. it 'writes an ArrayBuffer to a binary file', (done) ->
  422. return done() unless ArrayBuffer?
  423. @newFile = "#{@testFolder}/test image from arraybuffer.png"
  424. @newBuffer = new ArrayBuffer @imageFileData.length
  425. newBytes = new Uint8Array @newBuffer
  426. for i in [0...@imageFileData.length]
  427. newBytes[i] = @imageFileData.charCodeAt i
  428. @client.writeFile @newFile, @newBuffer, (error, stat) =>
  429. expect(error).to.equal null
  430. expect(stat).to.be.instanceOf Dropbox.Stat
  431. expect(stat.path).to.equal @newFile
  432. expect(stat.isFile).to.equal true
  433. @client.readFile @newFile, arrayBuffer: true,
  434. (error, buffer, stat) =>
  435. expect(error).to.equal null
  436. expect(buffer).to.be.instanceOf ArrayBuffer
  437. expect(stat).to.be.instanceOf Dropbox.Stat
  438. expect(stat.path).to.equal @newFile
  439. expect(stat.isFile).to.equal true
  440. view = new Uint8Array buffer
  441. length = buffer.byteLength
  442. bytes = (String.fromCharCode view[i] for i in [0...length]).
  443. join('')
  444. expect(bytes).to.equal @imageFileData
  445. done()
  446. it 'writes an ArrayBufferView to a binary file', (done) ->
  447. return done() unless ArrayBuffer?
  448. @newFile = "#{@testFolder}/test image from arraybufferview.png"
  449. @newBytes = new Uint8Array @imageFileData.length
  450. for i in [0...@imageFileData.length]
  451. @newBytes[i] = @imageFileData.charCodeAt i
  452. @client.writeFile @newFile, @newBytes, (error, stat) =>
  453. expect(error).to.equal null
  454. expect(stat).to.be.instanceOf Dropbox.Stat
  455. expect(stat.path).to.equal @newFile
  456. expect(stat.isFile).to.equal true
  457. @client.readFile @newFile, arrayBuffer: true,
  458. (error, buffer, stat) =>
  459. expect(error).to.equal null
  460. expect(buffer).to.be.instanceOf ArrayBuffer
  461. expect(stat).to.be.instanceOf Dropbox.Stat
  462. expect(stat.path).to.equal @newFile
  463. expect(stat.isFile).to.equal true
  464. view = new Uint8Array buffer
  465. length = buffer.byteLength
  466. bytes = (String.fromCharCode view[i] for i in [0...length]).
  467. join('')
  468. expect(bytes).to.equal @imageFileData
  469. done()
  470. describe '#resumableUploadStep + #resumableUploadFinish', ->
  471. beforeEach ->
  472. if ArrayBuffer? # IE9 and below doesn't have ArrayBuffer
  473. @length1 = Math.ceil @imageFileData.length / 3
  474. @length2 = @imageFileData.length - @length1
  475. @buffer1 = new ArrayBuffer @length1
  476. @view1 = new Uint8Array @buffer1
  477. for i in [0...@length1]
  478. @view1[i] = @imageFileData.charCodeAt i
  479. @buffer2 = new ArrayBuffer @length2
  480. @view2 = new Uint8Array @buffer2
  481. for i in [0...@length2]
  482. @view2[i] = @imageFileData.charCodeAt @length1 + i
  483. if Blob? # node.js and IE9 and below don't have Blob
  484. @blob1 = new Blob [@view1], type: 'image/png'
  485. if @blob1.size isnt @buffer1.byteLength
  486. @blob1 = new Blob [@buffer1], type: 'image/png'
  487. @blob2 = new Blob [@view2], type: 'image/png'
  488. if @blob2.size isnt @buffer2.byteLength
  489. @blob2 = new Blob [@buffer2], type: 'image/png'
  490. afterEach (done) ->
  491. @timeout 20 * 1000 # This sequence is slow on the current API server.
  492. return done() unless @newFile
  493. @client.remove @newFile, (error, stat) -> done()
  494. it 'writes a text file in two stages', (done) ->
  495. @timeout 20 * 1000 # This sequence is slow on the current API server.
  496. @newFile = "#{@testFolder}/test resumable upload.txt"
  497. line1 = "This is the first fragment\n"
  498. line2 = "This is the second fragment\n"
  499. @client.resumableUploadStep line1, null, (error, cursor1) =>
  500. expect(error).to.equal null
  501. expect(cursor1).to.be.instanceOf Dropbox.UploadCursor
  502. expect(cursor1.offset).to.equal line1.length
  503. @client.resumableUploadStep line2, cursor1, (error, cursor2) =>
  504. expect(error).to.equal null
  505. expect(cursor2).to.be.instanceOf Dropbox.UploadCursor
  506. expect(cursor2.offset).to.equal line1.length + line2.length
  507. expect(cursor2.tag).to.equal cursor1.tag
  508. @client.resumableUploadFinish @newFile, cursor2, (error, stat) =>
  509. expect(error).to.equal null
  510. expect(stat).to.be.instanceOf Dropbox.Stat
  511. expect(stat.path).to.equal @newFile
  512. expect(stat.isFile).to.equal true
  513. @client.readFile @newFile, (error, data, stat) =>
  514. expect(error).to.equal null
  515. expect(data).to.equal line1 + line2
  516. unless Dropbox.Xhr.ieXdr # IE's XDR doesn't do headers.
  517. expect(stat).to.be.instanceOf Dropbox.Stat
  518. expect(stat.path).to.equal @newFile
  519. expect(stat.isFile).to.equal true
  520. done()
  521. it 'writes a binary file using two ArrayBuffers', (done) ->
  522. return done() unless @buffer1
  523. @timeout 20 * 1000 # This sequence is slow on the current API server.
  524. @newFile = "#{@testFolder}/test resumable arraybuffer upload.png"
  525. @client.resumableUploadStep @buffer1, null, (error, cursor1) =>
  526. expect(error).to.equal null
  527. expect(cursor1).to.be.instanceOf Dropbox.UploadCursor
  528. expect(cursor1.offset).to.equal @length1
  529. @client.resumableUploadStep @buffer2, cursor1, (error, cursor2) =>
  530. expect(error).to.equal null
  531. expect(cursor2).to.be.instanceOf Dropbox.UploadCursor
  532. expect(cursor2.offset).to.equal @length1 + @length2
  533. expect(cursor2.tag).to.equal cursor1.tag
  534. @client.resumableUploadFinish @newFile, cursor2, (error, stat) =>
  535. expect(error).to.equal null
  536. expect(stat).to.be.instanceOf Dropbox.Stat
  537. expect(stat.path).to.equal @newFile
  538. expect(stat.isFile).to.equal true
  539. @client.readFile @newFile, arrayBuffer: true,
  540. (error, buffer, stat) =>
  541. expect(error).to.equal null
  542. expect(buffer).to.be.instanceOf ArrayBuffer
  543. expect(stat).to.be.instanceOf Dropbox.Stat
  544. expect(stat.path).to.equal @newFile
  545. expect(stat.isFile).to.equal true
  546. view = new Uint8Array buffer
  547. length = buffer.byteLength
  548. bytes = (String.fromCharCode view[i] for i in [0...length]).
  549. join('')
  550. expect(bytes).to.equal @imageFileData
  551. done()
  552. it 'writes a binary file using two ArrayBufferViews', (done) ->
  553. return done() unless @view1
  554. @timeout 20 * 1000 # This sequence is slow on the current API server.
  555. @newFile = "#{@testFolder}/test resumable arraybuffer upload.png"
  556. @client.resumableUploadStep @buffer1, null, (error, cursor1) =>
  557. expect(error).to.equal null
  558. expect(cursor1).to.be.instanceOf Dropbox.UploadCursor
  559. expect(cursor1.offset).to.equal @length1
  560. @client.resumableUploadStep @buffer2, cursor1, (error, cursor2) =>
  561. expect(error).to.equal null
  562. expect(cursor2).to.be.instanceOf Dropbox.UploadCursor
  563. expect(cursor2.offset).to.equal @length1 + @length2
  564. expect(cursor2.tag).to.equal cursor1.tag
  565. @client.resumableUploadFinish @newFile, cursor2, (error, stat) =>
  566. expect(error).to.equal null
  567. expect(stat).to.be.instanceOf Dropbox.Stat
  568. expect(stat.path).to.equal @newFile
  569. expect(stat.isFile).to.equal true
  570. @client.readFile @newFile, arrayBuffer: true,
  571. (error, buffer, stat) =>
  572. expect(error).to.equal null
  573. expect(buffer).to.be.instanceOf ArrayBuffer
  574. expect(stat).to.be.instanceOf Dropbox.Stat
  575. expect(stat.path).to.equal @newFile
  576. expect(stat.isFile).to.equal true
  577. view = new Uint8Array buffer
  578. length = buffer.byteLength
  579. bytes = (String.fromCharCode view[i] for i in [0...length]).
  580. join('')
  581. expect(bytes).to.equal @imageFileData
  582. done()
  583. it 'writes a binary file using two Blobs', (done) ->
  584. return done() unless @blob1
  585. @timeout 20 * 1000 # This sequence is slow on the current API server.
  586. @newFile = "#{@testFolder}/test resumable blob upload.png"
  587. @client.resumableUploadStep @blob1, null, (error, cursor1) =>
  588. expect(error).to.equal null
  589. expect(cursor1).to.be.instanceOf Dropbox.UploadCursor
  590. expect(cursor1.offset).to.equal @length1
  591. @client.resumableUploadStep @blob2, cursor1, (error, cursor2) =>
  592. expect(error).to.equal null
  593. expect(cursor2).to.be.instanceOf Dropbox.UploadCursor
  594. expect(cursor2.offset).to.equal @length1 + @length2
  595. expect(cursor2.tag).to.equal cursor1.tag
  596. @client.resumableUploadFinish @newFile, cursor2, (error, stat) =>
  597. expect(error).to.equal null
  598. expect(stat).to.be.instanceOf Dropbox.Stat
  599. expect(stat.path).to.equal @newFile
  600. expect(stat.isFile).to.equal true
  601. @client.readFile @newFile, arrayBuffer: true,
  602. (error, buffer, stat) =>
  603. expect(error).to.equal null
  604. expect(buffer).to.be.instanceOf ArrayBuffer
  605. expect(stat).to.be.instanceOf Dropbox.Stat
  606. expect(stat.path).to.equal @newFile
  607. expect(stat.isFile).to.equal true
  608. view = new Uint8Array buffer
  609. length = buffer.byteLength
  610. bytes = (String.fromCharCode view[i] for i in [0...length]).
  611. join('')
  612. expect(bytes).to.equal @imageFileData
  613. done()
  614. it 'recovers from out-of-sync correctly', (done) ->
  615. # IE's XDR doesn't return anything on errors, so we can't do recovery.
  616. return done() if Dropbox.Xhr.ieXdr
  617. @timeout 20 * 1000 # This sequence is slow on the current API server.
  618. @newFile = "#{@testFolder}/test resumable upload out of sync.txt"
  619. line1 = "This is the first fragment\n"
  620. line2 = "This is the second fragment\n"
  621. @client.resumableUploadStep line1, null, (error, cursor1) =>
  622. expect(error).to.equal null
  623. expect(cursor1).to.be.instanceOf Dropbox.UploadCursor
  624. expect(cursor1.offset).to.equal line1.length
  625. cursor1.offset += 10
  626. @client.resumableUploadStep line2, cursor1, (error, cursor2) =>
  627. expect(error).to.equal null
  628. expect(cursor2).to.be.instanceOf Dropbox.UploadCursor
  629. expect(cursor2.offset).to.equal line1.length
  630. expect(cursor2.tag).to.equal cursor1.tag
  631. @client.resumableUploadStep line2, cursor2, (error, cursor3) =>
  632. expect(error).to.equal null
  633. expect(cursor3).to.be.instanceOf Dropbox.UploadCursor
  634. expect(cursor3.offset).to.equal line1.length + line2.length
  635. expect(cursor3.tag).to.equal cursor1.tag
  636. @client.resumableUploadFinish @newFile, cursor3, (error, stat) =>
  637. expect(error).to.equal null
  638. expect(stat).to.be.instanceOf Dropbox.Stat
  639. expect(stat.path).to.equal @newFile
  640. expect(stat.isFile).to.equal true
  641. @client.readFile @newFile, (error, data, stat) =>
  642. expect(error).to.equal null
  643. expect(data).to.equal line1 + line2
  644. unless Dropbox.Xhr.ieXdr # IE's XDR doesn't do headers.
  645. expect(stat).to.be.instanceOf Dropbox.Stat
  646. expect(stat.path).to.equal @newFile
  647. expect(stat.isFile).to.equal true
  648. done()
  649. it 'reports errors correctly', (done) ->
  650. @newFile = "#{@testFolder}/test resumable upload error.txt"
  651. badCursor = new Dropbox.UploadCursor 'trollcursor'
  652. badCursor.offset = 42
  653. @client.resumableUploadStep @textFileData, badCursor, (error, cursor) =>
  654. expect(cursor).to.equal undefined
  655. expect(error).to.be.instanceOf Dropbox.ApiError
  656. unless Dropbox.Xhr.ieXdr # IE's XDR doesn't do status codes.
  657. expect(error.status).to.equal 404
  658. done()
  659. describe '#stat', ->
  660. it 'retrieves a Stat for a file', (done) ->
  661. @client.stat @textFile, (error, stat) =>
  662. expect(error).to.equal null
  663. expect(stat).to.be.instanceOf Dropbox.Stat
  664. expect(stat.path).to.equal @textFile
  665. expect(stat.isFile).to.equal true
  666. expect(stat.versionTag).to.equal @textFileTag
  667. expect(stat.size).to.equal @textFileData.length
  668. if clientKeys.sandbox
  669. expect(stat.inAppFolder).to.equal true
  670. else
  671. expect(stat.inAppFolder).to.equal false
  672. done()
  673. it 'retrieves a Stat for a folder', (done) ->
  674. @client.stat @testFolder, (error, stat, entries) =>
  675. expect(error).to.equal null
  676. expect(stat).to.be.instanceOf Dropbox.Stat
  677. expect(stat.path).to.equal @testFolder
  678. expect(stat.isFolder).to.equal true
  679. expect(stat.size).to.equal 0
  680. if clientKeys.sandbox
  681. expect(stat.inAppFolder).to.equal true
  682. else
  683. expect(stat.inAppFolder).to.equal false
  684. expect(entries).to.equal undefined
  685. done()
  686. it 'retrieves a Stat and entries for a folder', (done) ->
  687. @client.stat @testFolder, { readDir: true }, (error, stat, entries) =>
  688. expect(error).to.equal null
  689. expect(stat).to.be.instanceOf Dropbox.Stat
  690. expect(stat.path).to.equal @testFolder
  691. expect(stat.isFolder).to.equal true
  692. expect(entries).to.be.ok
  693. expect(entries).to.have.length 2
  694. expect(entries[0]).to.be.instanceOf Dropbox.Stat
  695. expect(entries[0].path).not.to.equal @testFolder
  696. expect(entries[0].path).to.have.string @testFolder
  697. done()
  698. it 'fails cleanly for a non-existing path', (done) ->
  699. listenerError = null
  700. @client.onError.addListener (error) -> listenerError = error
  701. @client.stat @testFolder + '/should_404.txt', (error, stat, entries) =>
  702. expect(stat).to.equal undefined
  703. expect(entries).to.equal.null
  704. expect(error).to.be.instanceOf Dropbox.ApiError
  705. expect(listenerError).to.equal error
  706. unless Dropbox.Xhr.ieXdr # IE's XDR doesn't do status codes.
  707. expect(error).to.have.property 'status'
  708. expect(error.status).to.equal 404
  709. done()
  710. describe 'with httpCache', ->
  711. beforeEach ->
  712. @xhr = null
  713. @client.onXhr.addListener (xhr) =>
  714. @xhr = xhr
  715. it 'retrieves a Stat for a file using Authorization headers', (done) ->
  716. @client.stat @textFile, httpCache: true, (error, stat) =>
  717. if Dropbox.Xhr.ieXdr # IE's XDR doesn't do headers
  718. expect(@xhr.url).to.contain 'oauth_nonce'
  719. else
  720. expect(@xhr.headers).to.have.key 'Authorization'
  721. expect(error).to.equal null
  722. expect(stat).to.be.instanceOf Dropbox.Stat
  723. expect(stat.path).to.equal @textFile
  724. expect(stat.isFile).to.equal true
  725. expect(stat.versionTag).to.equal @textFileTag
  726. expect(stat.size).to.equal @textFileData.length
  727. if clientKeys.sandbox
  728. expect(stat.inAppFolder).to.equal true
  729. else
  730. expect(stat.inAppFolder).to.equal false
  731. done()
  732. describe '#readdir', ->
  733. it 'retrieves a Stat and entries for a folder', (done) ->
  734. @client.readdir @testFolder, (error, entries, dir_stat, entry_stats) =>
  735. expect(error).to.equal null
  736. expect(entries).to.be.ok
  737. expect(entries).to.have.length 2
  738. expect(entries[0]).to.be.a 'string'
  739. expect(entries[0]).not.to.have.string '/'
  740. expect(entries[0]).to.match /^(test-binary-image.png)|(test-file.txt)$/
  741. expect(dir_stat).to.be.instanceOf Dropbox.Stat
  742. expect(dir_stat.path).to.equal @testFolder
  743. expect(dir_stat.isFolder).to.equal true
  744. expect(entry_stats).to.be.ok
  745. expect(entry_stats).to.have.length 2
  746. expect(entry_stats[0]).to.be.instanceOf Dropbox.Stat
  747. expect(entry_stats[0].path).not.to.equal @testFolder
  748. expect(entry_stats[0].path).to.have.string @testFolder
  749. done()
  750. describe 'with httpCache', ->
  751. beforeEach ->
  752. @xhr = null
  753. @client.onXhr.addListener (xhr) =>
  754. @xhr = xhr
  755. it 'retrieves a folder Stat and entries using Authorization', (done) ->
  756. @client.readdir @testFolder, httpCache: true,
  757. (error, entries, dir_stat, entry_stats) =>
  758. if Dropbox.Xhr.ieXdr # IE's XDR doesn't do headers
  759. expect(@xhr.url).to.contain 'oauth_nonce'
  760. else
  761. expect(@xhr.headers).to.have.key 'Authorization'
  762. expect(error).to.equal null
  763. expect(entries).to.be.ok
  764. expect(entries).to.have.length 2
  765. expect(entries[0]).to.be.a 'string'
  766. expect(entries[0]).not.to.have.string '/'
  767. expect(entries[0]).to.match(
  768. /^(test-binary-image.png)|(test-file.txt)$/)
  769. expect(dir_stat).to.be.instanceOf Dropbox.Stat
  770. expect(dir_stat.path).to.equal @testFolder
  771. expect(dir_stat.isFolder).to.equal true
  772. expect(entry_stats).to.be.ok
  773. expect(entry_stats).to.have.length 2
  774. expect(entry_stats[0]).to.be.instanceOf Dropbox.Stat
  775. expect(entry_stats[0].path).not.to.equal @testFolder
  776. expect(entry_stats[0].path).to.have.string @testFolder
  777. done()
  778. describe '#history', ->
  779. it 'gets a list of revisions', (done) ->
  780. @client.history @textFile, (error, versions) =>
  781. expect(error).to.equal null
  782. expect(versions).to.have.length 1
  783. expect(versions[0]).to.be.instanceOf Dropbox.Stat
  784. expect(versions[0].path).to.equal @textFile
  785. expect(versions[0].size).to.equal @textFileData.length
  786. expect(versions[0].versionTag).to.equal @textFileTag
  787. done()
  788. it 'returns 40x if the limit is set to 0', (done) ->
  789. listenerError = null
  790. @client.onError.addListener (error) -> listenerError = error
  791. @client.history @textFile, limit: 0, (error, versions) =>
  792. expect(error).to.be.instanceOf Dropbox.ApiError
  793. expect(listenerError).to.equal error
  794. unless Dropbox.Xhr.ieXdr # IE's XDR doesn't do status codes.
  795. expect(error.status).to.be.within 400, 499
  796. expect(versions).not.to.be.ok
  797. done()
  798. describe 'with httpCache', ->
  799. beforeEach ->
  800. @xhr = null
  801. @client.onXhr.addListener (xhr) =>
  802. @xhr = xhr
  803. it 'gets a list of revisions using Authorization headers', (done) ->
  804. @client.history @textFile, httpCache: true, (error, versions) =>
  805. if Dropbox.Xhr.ieXdr # IE's XDR doesn't do headers
  806. expect(@xhr.url).to.contain 'oauth_nonce'
  807. else
  808. expect(@xhr.headers).to.have.key 'Authorization'
  809. expect(error).to.equal null
  810. expect(versions).to.have.length 1
  811. expect(versions[0]).to.be.instanceOf Dropbox.Stat
  812. expect(versions[0].path).to.equal @textFile
  813. expect(versions[0].size).to.equal @textFileData.length
  814. expect(versions[0].versionTag).to.equal @textFileTag
  815. done()
  816. describe '#copy', ->
  817. afterEach (done) ->
  818. return done() unless @newFile
  819. @client.remove @newFile, (error, stat) -> done()
  820. it 'copies a file given by path', (done) ->
  821. @timeout 12 * 1000 # This sequence is slow on the current API server.
  822. @newFile = "#{@testFolder}/copy of test-file.txt"
  823. @client.copy @textFile, @newFile, (error, stat) =>
  824. expect(error).to.equal null
  825. expect(stat).to.be.instanceOf Dropbox.Stat
  826. expect(stat.path).to.equal @newFile
  827. @client.readFile @newFile, (error, data, stat) =>
  828. expect(error).to.equal null
  829. expect(data).to.equal @textFileData
  830. unless Dropbox.Xhr.ieXdr # IE's XDR doesn't do headers.
  831. expect(stat).to.be.instanceOf Dropbox.Stat
  832. expect(stat.path).to.equal @newFile
  833. @client.readFile @textFile, (error, data, stat) =>
  834. expect(error).to.equal null
  835. expect(data).to.equal @textFileData
  836. unless Dropbox.Xhr.ieXdr # IE's XDR doesn't do headers.
  837. expect(stat).to.be.instanceOf Dropbox.Stat
  838. expect(stat.path).to.equal @textFile
  839. expect(stat.versionTag).to.equal @textFileTag
  840. done()
  841. describe '#makeCopyReference', ->
  842. afterEach (done) ->
  843. return done() unless @newFile
  844. @client.remove @newFile, (error, stat) -> done()
  845. it 'creates a Dropbox.CopyReference that copies the file', (done) ->
  846. @timeout 12 * 1000 # This sequence is slow on the current API server.
  847. @newFile = "#{@testFolder}/ref copy of test-file.txt"
  848. @client.makeCopyReference @textFile, (error, copyRef) =>
  849. expect(error).to.equal null
  850. expect(copyRef).to.be.instanceOf Dropbox.CopyReference
  851. @client.copy copyRef, @newFile, (error, stat) =>
  852. expect(error).to.equal null
  853. expect(stat).to.be.instanceOf Dropbox.Stat
  854. expect(stat.path).to.equal @newFile
  855. expect(stat.isFile).to.equal true
  856. @client.readFile @newFile, (error, data, stat) =>
  857. expect(error).to.equal null
  858. expect(data).to.equal @textFileData
  859. unless Dropbox.Xhr.ieXdr # IE's XDR doesn't do headers.
  860. expect(stat).to.be.instanceOf Dropbox.Stat
  861. expect(stat.path).to.equal @newFile
  862. done()
  863. describe '#move', ->
  864. beforeEach (done) ->
  865. @timeout 10 * 1000 # This sequence is slow on the current API server.
  866. @moveFrom = "#{@testFolder}/move source of test-file.txt"
  867. @client.copy @textFile, @moveFrom, (error, stat) ->
  868. expect(error).to.equal null
  869. done()
  870. afterEach (done) ->
  871. @client.remove @moveFrom, (error, stat) =>
  872. return done() unless @moveTo
  873. @client.remove @moveTo, (error, stat) -> done()
  874. it 'moves a file', (done) ->
  875. @timeout 15 * 1000 # This sequence is slow on the current API server.
  876. @moveTo = "#{@testFolder}/moved test-file.txt"
  877. @client.move @moveFrom, @moveTo, (error, stat) =>
  878. expect(error).to.equal null
  879. expect(stat).to.be.instanceOf Dropbox.Stat
  880. expect(stat.path).to.equal @moveTo
  881. expect(stat.isFile).to.equal true
  882. @client.readFile @moveTo, (error, data, stat) =>
  883. expect(error).to.equal null
  884. expect(data).to.equal @textFileData
  885. unless Dropbox.Xhr.ieXdr # IE's XDR doesn't do headers.
  886. expect(stat).to.be.instanceOf Dropbox.Stat
  887. expect(stat.path).to.equal @moveTo
  888. @client.readFile @moveFrom, (error, data, stat) ->
  889. expect(error).to.be.ok
  890. unless Dropbox.Xhr.ieXdr # IE's XDR doesn't do status codes.
  891. expect(error).to.have.property 'status'
  892. expect(error.status).to.equal 404
  893. expect(data).to.equal undefined
  894. unless Dropbox.Xhr.ieXdr # IE's XDR doesn't do headers.
  895. expect(stat).to.equal undefined
  896. done()
  897. describe '#remove', ->
  898. beforeEach (done) ->
  899. @newFolder = "#{@testFolder}/folder delete test"
  900. @client.mkdir @newFolder, (error, stat) =>
  901. expect(error).to.equal null
  902. done()
  903. afterEach (done) ->
  904. return done() unless @newFolder
  905. @client.remove @newFolder, (error, stat) -> done()
  906. it 'deletes a folder', (done) ->
  907. @client.remove @newFolder, (error, stat) =>
  908. expect(error).to.equal null
  909. expect(stat).to.be.instanceOf Dropbox.Stat
  910. expect(stat.path).to.equal @newFolder
  911. @client.stat @newFolder, { removed: true }, (error, stat) =>
  912. expect(error).to.equal null
  913. expect(stat).to.be.instanceOf Dropbox.Stat
  914. expect(stat.isRemoved).to.equal true
  915. done()
  916. it 'deletes a folder when called as unlink', (done) ->
  917. @client.unlink @newFolder, (error, stat) =>
  918. expect(error).to.equal null
  919. expect(stat).to.be.instanceOf Dropbox.Stat
  920. expect(stat.path).to.equal @newFolder
  921. @client.stat @newFolder, { removed: true }, (error, stat) =>
  922. expect(error).to.equal null
  923. expect(stat).to.be.instanceOf Dropbox.Stat
  924. expect(stat.isRemoved).to.equal true
  925. done()
  926. describe '#revertFile', ->
  927. describe 'on a removed file', ->
  928. beforeEach (done) ->
  929. @timeout 12 * 1000 # This sequence seems to be quite slow.
  930. @newFile = "#{@testFolder}/file revert test.txt"
  931. @client.copy @textFile, @newFile, (error, stat) =>
  932. expect(error).to.equal null
  933. expect(stat).to.be.instanceOf Dropbox.Stat
  934. expect(stat.path).to.equal @newFile
  935. @versionTag = stat.versionTag
  936. @client.remove @newFile, (error, stat) =>
  937. expect(error).to.equal null
  938. expect(stat).to.be.instanceOf Dropbox.Stat
  939. expect(stat.path).to.equal @newFile
  940. done()
  941. afterEach (done) ->
  942. return done() unless @newFile
  943. @client.remove @newFile, (error, stat) -> done()
  944. it 'reverts the file to a previous version', (done) ->
  945. @timeout 12 * 1000 # This sequence seems to be quite slow.
  946. @client.revertFile @newFile, @versionTag, (error, stat) =>
  947. expect(error).to.equal null
  948. expect(stat).to.be.instanceOf Dropbox.Stat
  949. expect(stat.path).to.equal @newFile
  950. expect(stat.isRemoved).to.equal false
  951. @client.readFile @newFile, (error, data, stat) =>
  952. expect(error).to.equal null
  953. expect(data).to.equal @textFileData
  954. unless Dropbox.Xhr.ieXdr # IE's XDR doesn't do headers.
  955. expect(stat).to.be.instanceOf Dropbox.Stat
  956. expect(stat.path).to.equal @newFile
  957. expect(stat.isRemoved).to.equal false
  958. done()
  959. describe '#findByName', ->
  960. it 'locates the test folder given a partial name', (done) ->
  961. namePattern = @testFolder.substring 5
  962. @client.search '/', namePattern, (error, matches) =>
  963. expect(error).to.equal null
  964. expect(matches).to.have.length 1
  965. expect(matches[0]).to.be.instanceOf Dropbox.Stat
  966. expect(matches[0].path).to.equal @testFolder
  967. expect(matches[0].isFolder).to.equal true
  968. done()
  969. it 'lists the test folder files given the "test" pattern', (done) ->
  970. @client.search @testFolder, 'test', (error, matches) =>
  971. expect(error).to.equal null
  972. expect(matches).to.have.length 2
  973. done()
  974. it 'only lists one match when given limit 1', (done) ->
  975. @client.search @testFolder, 'test', limit: 1, (error, matches) =>
  976. expect(error).to.equal null
  977. expect(matches).to.have.length 1
  978. done()
  979. describe 'with httpCache', ->
  980. beforeEach ->
  981. @xhr = null
  982. @client.onXhr.addListener (xhr) =>
  983. @xhr = xhr
  984. it 'locates the test folder using Authorize headers', (done) ->
  985. namePattern = @testFolder.substring 5
  986. @client.search '/', namePattern, httpCache: true, (error, matches) =>
  987. if Dropbox.Xhr.ieXdr # IE's XDR doesn't do headers
  988. expect(@xhr.url).to.contain 'oauth_nonce'
  989. else
  990. expect(@xhr.headers).to.have.key 'Authorization'
  991. expect(error).to.equal null
  992. expect(matches).to.have.length 1
  993. expect(matches[0]).to.be.instanceOf Dropbox.Stat
  994. expect(matches[0].path).to.equal @testFolder
  995. expect(matches[0].isFolder).to.equal true
  996. done()
  997. describe '#makeUrl', ->
  998. describe 'for a short Web URL', ->
  999. it 'returns a shortened Dropbox URL', (done) ->
  1000. @client.makeUrl @textFile, (error, publicUrl) ->
  1001. expect(error).to.equal null
  1002. expect(publicUrl).to.be.instanceOf Dropbox.PublicUrl
  1003. expect(publicUrl.isDirect).to.equal false
  1004. expect(publicUrl.url).to.contain '//db.tt/'
  1005. done()
  1006. describe 'for a Web URL created with long: true', ->
  1007. it 'returns an URL to a preview page', (done) ->
  1008. @client.makeUrl @textFile, { long: true }, (error, publicUrl) =>
  1009. expect(error).to.equal null
  1010. expect(publicUrl).to.be.instanceOf Dropbox.PublicUrl
  1011. expect(publicUrl.isDirect).to.equal false
  1012. expect(publicUrl.url).not.to.contain '//db.tt/'
  1013. # The cont/ents server does not return CORS headers.
  1014. return done() unless @nodejs
  1015. Dropbox.Xhr.request 'GET', publicUrl.url, {}, null, (error, data) ->
  1016. expect(error).to.equal null
  1017. expect(data).to.contain '<!DOCTYPE html>'
  1018. done()
  1019. describe 'for a Web URL created with longUrl: true', ->
  1020. it 'returns an URL to a preview page', (done) ->
  1021. @client.makeUrl @textFile, { longUrl: true }, (error, publicUrl) =>
  1022. expect(error).to.equal null
  1023. expect(publicUrl).to.be.instanceOf Dropbox.PublicUrl
  1024. expect(publicUrl.isDirect).to.equal false
  1025. expect(publicUrl.url).not.to.contain '//db.tt/'
  1026. done()
  1027. describe 'for a direct download URL', ->
  1028. it 'gets a direct download URL', (done) ->
  1029. @client.makeUrl @textFile, { download: true }, (error, publicUrl) =>
  1030. expect(error).to.equal null
  1031. expect(publicUrl).to.be.instanceOf Dropbox.PublicUrl
  1032. expect(publicUrl.isDirect).to.equal true
  1033. expect(publicUrl.url).not.to.contain '//db.tt/'
  1034. # The contents server does not return CORS headers.
  1035. return done() unless @nodejs
  1036. Dropbox.Xhr.request 'GET', publicUrl.url, {}, null, (error, data) =>
  1037. expect(error).to.equal null
  1038. expect(data).to.equal @textFileData
  1039. done()
  1040. describe 'for a direct download URL created with downloadHack: true', ->
  1041. it 'gets a direct long-lived download URL', (done) ->
  1042. @client.makeUrl @textFile, { downloadHack: true }, (error, publicUrl) =>
  1043. expect(error).to.equal null
  1044. expect(publicUrl).to.be.instanceOf Dropbox.PublicUrl
  1045. expect(publicUrl.isDirect).to.equal true
  1046. expect(publicUrl.url).not.to.contain '//db.tt/'
  1047. expect(publicUrl.expiresAt - Date.now()).to.be.above 86400000
  1048. # The download server does not return CORS headers.
  1049. return done() unless @nodejs
  1050. Dropbox.Xhr.request 'GET', publicUrl.url, {}, null, (error, data) =>
  1051. expect(error).to.equal null
  1052. expect(data).to.equal @textFileData
  1053. done()
  1054. describe '#pullChanges', ->
  1055. beforeEach ->
  1056. # Pulling an entire Dropbox can take a lot of time, so we need fan

Large files files are truncated, but you can click here to view the full file