write_cart.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. const { default: axios } = require("axios");
  2. const write = require("../coverage_models/write");
  3. const TestFactory = require("../lib/TestFactory");
  4. const chai = require('chai');
  5. chai.use(require('chai-as-promised'))
  6. const expect = chai.expect;
  7. module.exports = TestFactory.cartesian('Cartesian Test for /write', write, {
  8. each: async (t, state, i) => {
  9. if ( state['conditions.destinationIsFile'] ) {
  10. await t.write('write_cart_' + i, 'placeholder\n');
  11. } else {
  12. await t.mkdir('write_cart_' + i);
  13. }
  14. const dir = `/${t.cwd}/write_cart_` + i;
  15. const dirUID = (await t.stat('write_cart_' + i)).uid;
  16. const contents = new Blob(
  17. [`case ${i}\n`],
  18. { type: 'text/plain' },
  19. );
  20. console.log('DIR UID', dirUID)
  21. const fd = new FormData();
  22. if ( state.name === 'specified' ) {
  23. fd.append('name', 'specified_name.txt');
  24. }
  25. if ( state.overwrite ) {
  26. fd.append(state.overwrite, true);
  27. }
  28. fd.append('path', state.format === 'path' ? dir : dirUID);
  29. fd.append('size', contents.size),
  30. fd.append('file', contents, 'uploaded_name.txt');
  31. let e = null;
  32. let resp;
  33. try {
  34. resp = await axios.request({
  35. method: 'post',
  36. httpsAgent: t.httpsAgent,
  37. url: t.getURL('write'),
  38. data: fd,
  39. headers: {
  40. ...t.headers_,
  41. 'Content-Type': 'multipart/form-data'
  42. }
  43. })
  44. } catch (e_) {
  45. e = e_;
  46. }
  47. let error_expected = null;
  48. // Error conditions
  49. if (
  50. state['conditions.destinationIsFile'] &&
  51. state.name === 'specified'
  52. ) {
  53. error_expected = {
  54. code: 'dest_is_not_a_directory',
  55. message: `Destination must be a directory.`,
  56. };
  57. }
  58. if (
  59. state['conditions.destinationIsFile'] &&
  60. state.name === 'default' &&
  61. ! state.overwrite
  62. ) {
  63. error_expected = {
  64. code: 'item_with_same_name_exists',
  65. message: 'An item with name `write_cart_'+i+'` already exists.',
  66. entry_name: 'write_cart_' + i,
  67. };
  68. }
  69. if ( error_expected ) {
  70. expect(e).to.exist;
  71. const data = e.response.data;
  72. expect(data).deep.equal(error_expected);
  73. } else {
  74. if ( e ) throw e;
  75. }
  76. }
  77. })