|
| 1 | +import fs from 'fs' |
1 | 2 | import path from 'path' |
2 | 3 | import Axios from 'axios' |
3 | 4 | import { expect } from 'chai' |
@@ -171,6 +172,49 @@ describe('Contentstack Asset test', () => { |
171 | 172 | .catch(done) |
172 | 173 | }) |
173 | 174 |
|
| 175 | + it('should upload asset from buffer', (done) => { |
| 176 | + const mock = new MockAdapter(Axios) |
| 177 | + mock.onPost('/assets').reply(200, { |
| 178 | + asset: { |
| 179 | + uid: 'mock-uid', |
| 180 | + url: '/assets', |
| 181 | + filename: 'customUpload.html', |
| 182 | + title: 'buffer-asset', |
| 183 | + description: 'Buffer Asset Desc', |
| 184 | + content_type: 'text/html', |
| 185 | + tags: ['Buffer'], |
| 186 | + parent_uid: 'UID' |
| 187 | + } |
| 188 | + }) |
| 189 | + const filePath = path.join(__dirname, '../api/mock/customUpload.html') |
| 190 | + const fileBuffer = fs.readFileSync(filePath) |
| 191 | + const assetUpload = { |
| 192 | + upload: fileBuffer, // Buffer upload |
| 193 | + filename: 'customUpload.html', // Filename to identify the file |
| 194 | + content_type: 'text/html', // MIME type |
| 195 | + title: 'buffer-asset', |
| 196 | + description: 'Buffer Asset Desc', |
| 197 | + tags: ['Buffer'], |
| 198 | + parent_uid: 'UID' |
| 199 | + } |
| 200 | + const form = createFormData(assetUpload)() // Create FormData for Buffer upload |
| 201 | + const boundary = form.getBoundary() |
| 202 | + expect(boundary).to.be.equal(form.getBoundary()) |
| 203 | + expect(boundary.length).to.be.greaterThan(30) |
| 204 | + makeAsset() |
| 205 | + .create(assetUpload) |
| 206 | + .then((asset) => { |
| 207 | + expect(asset.uid).to.be.equal('mock-uid') |
| 208 | + expect(asset.filename).to.be.equal('customUpload.html') |
| 209 | + expect(asset.title).to.be.equal('buffer-asset') |
| 210 | + expect(asset.description).to.be.equal('Buffer Asset Desc') |
| 211 | + expect(asset.content_type).to.be.equal('text/html') |
| 212 | + expect(asset.tags).to.include('Buffer') |
| 213 | + done() |
| 214 | + }) |
| 215 | + .catch(done) |
| 216 | + }) |
| 217 | + |
174 | 218 | it('Asset replace test', done => { |
175 | 219 | var mock = new MockAdapter(Axios) |
176 | 220 | mock.onPut('/assets/UID').reply(200, { |
|
0 commit comments