|
1 | 1 | const {assert} = require('./utils'); |
| 2 | +const path = require('path'); |
| 3 | +const FormData = require('form-data'); |
2 | 4 |
|
3 | | -const {isJSON, normalizeURL} = require('../lib/utils'); |
| 5 | +const {isJSON, normalizeURL, attachFile} = require('../lib/utils'); |
4 | 6 |
|
5 | 7 | describe('Utils', function () { |
6 | 8 | it('isJSON', function () { |
@@ -30,4 +32,90 @@ describe('Utils', function () { |
30 | 32 | assert.equal(url, 'http://example.com/?yolo=9000'); |
31 | 33 | }); |
32 | 34 | }); |
| 35 | + |
| 36 | + describe('attachFile', function () { |
| 37 | + it('creates FormData with file content', function () { |
| 38 | + const filePath = path.join(__dirname, 'fixtures/test-file.txt'); |
| 39 | + const formData = attachFile('testfile', filePath); |
| 40 | + |
| 41 | + assert.equal(formData instanceof FormData, true); |
| 42 | + assert.equal(typeof formData.getHeaders, 'function'); |
| 43 | + assert.equal(typeof formData.getBuffer, 'function'); |
| 44 | + }); |
| 45 | + |
| 46 | + it('sets correct filename from path', function () { |
| 47 | + const filePath = path.join(__dirname, 'fixtures/test-file.txt'); |
| 48 | + const formData = attachFile('testfile', filePath); |
| 49 | + |
| 50 | + // FormData doesn't expose a direct way to check the filename, |
| 51 | + // but we can verify it was created without errors |
| 52 | + const headers = formData.getHeaders(); |
| 53 | + assert.match(headers['content-type'], /^multipart\/form-data; boundary=/); |
| 54 | + }); |
| 55 | + |
| 56 | + it('sets correct content type for text file', function () { |
| 57 | + const filePath = path.join(__dirname, 'fixtures/test-file.txt'); |
| 58 | + const formData = attachFile('testfile', filePath); |
| 59 | + |
| 60 | + // Get the form data buffer to check it contains the right content type |
| 61 | + const buffer = formData.getBuffer(); |
| 62 | + const content = buffer.toString(); |
| 63 | + |
| 64 | + // Check that the form data contains the expected content type |
| 65 | + assert.match(content, /Content-Type: text\/plain/); |
| 66 | + }); |
| 67 | + |
| 68 | + it('sets correct content type for PNG image', function () { |
| 69 | + const filePath = path.join(__dirname, 'fixtures/ghost-favicon.png'); |
| 70 | + const formData = attachFile('image', filePath); |
| 71 | + |
| 72 | + // Get the form data buffer to check it contains the right content type |
| 73 | + const buffer = formData.getBuffer(); |
| 74 | + const content = buffer.toString(); |
| 75 | + |
| 76 | + // Check that the form data contains the expected content type |
| 77 | + assert.match(content, /Content-Type: image\/png/); |
| 78 | + }); |
| 79 | + |
| 80 | + it('uses default content type for unknown file extension', function () { |
| 81 | + // Create a file with unknown extension |
| 82 | + const fs = require('fs'); |
| 83 | + const unknownFile = path.join(__dirname, 'fixtures/test.unknown'); |
| 84 | + fs.writeFileSync(unknownFile, 'test content'); |
| 85 | + |
| 86 | + try { |
| 87 | + const formData = attachFile('file', unknownFile); |
| 88 | + const buffer = formData.getBuffer(); |
| 89 | + const content = buffer.toString(); |
| 90 | + |
| 91 | + // Check that the form data contains the default content type |
| 92 | + assert.match(content, /Content-Type: application\/octet-stream/); |
| 93 | + } finally { |
| 94 | + // Clean up |
| 95 | + fs.unlinkSync(unknownFile); |
| 96 | + } |
| 97 | + }); |
| 98 | + |
| 99 | + it('includes the correct field name', function () { |
| 100 | + const filePath = path.join(__dirname, 'fixtures/test-file.txt'); |
| 101 | + const formData = attachFile('myfield', filePath); |
| 102 | + |
| 103 | + const buffer = formData.getBuffer(); |
| 104 | + const content = buffer.toString(); |
| 105 | + |
| 106 | + // Check that the form data contains the field name |
| 107 | + assert.match(content, /Content-Disposition: form-data; name="myfield"/); |
| 108 | + }); |
| 109 | + |
| 110 | + it('includes the file content', function () { |
| 111 | + const filePath = path.join(__dirname, 'fixtures/test-file.txt'); |
| 112 | + const formData = attachFile('testfile', filePath); |
| 113 | + |
| 114 | + const buffer = formData.getBuffer(); |
| 115 | + const content = buffer.toString(); |
| 116 | + |
| 117 | + // Check that the form data contains the file content |
| 118 | + assert.match(content, /test content for file upload/); |
| 119 | + }); |
| 120 | + }); |
33 | 121 | }); |
0 commit comments