UNPKG

1.3 kBJavaScriptView Raw
1'use strict';
2
3const frisby = require('../src/frisby');
4const mocks = require('./fixtures/http_mocks');
5const fs = require('fs');
6
7const testHost = 'http://api.example.com';
8
9describe('File Uploads', function() {
10
11 it('should accept and process a FormData object as the "body" parameter', function(doneFn) {
12 mocks.use(['fileUploadPng']);
13
14 let logoImage = __dirname + '/fixtures/frisby-logo.png';
15 let form = frisby.formData();
16
17 form.append('file', fs.createReadStream(logoImage));
18
19 frisby.post(`${testHost}/upload`, {
20 body: form
21 })
22 .expect('status', 200)
23 .expect('header', 'Content-Type', 'image/png')
24 .done(doneFn);
25 });
26
27 it('should handle file contents as a response', function(doneFn) {
28 mocks.use(['fileContents']);
29
30 frisby.setup({ request: { rawBody: true } })
31 .get(`${testHost}/files/logo.png`)
32 .expect('status', 200)
33 .expect('header', 'Content-Type', 'image/png')
34 .then(res => {
35 let body = res.body;
36 expect(body).toBeInstanceOf(ArrayBuffer);
37 expect(body.byteLength).toBeGreaterThan(8);
38 const PNG_HEADER = [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a];
39 expect(new Uint8Array(body.slice(0, 8))).toEqual(new Uint8Array(PNG_HEADER));
40 })
41 .done(doneFn);
42 });
43
44});