|
4 | 4 | 'use strict';
|
5 | 5 |
|
6 | 6 | const path = require('path');
|
| 7 | +const Stream = require('stream'); |
7 | 8 | // const assert = require('assert');
|
8 | 9 | const Request = require('http').ClientRequest;
|
9 | 10 |
|
@@ -159,6 +160,90 @@ function makeHeader(filename) {
|
159 | 160 | form.emit('end');
|
160 | 161 | });
|
161 | 162 |
|
| 163 | + describe(`${name}#_onPart`, () => { |
| 164 | + describe('when not allow empty files', () => { |
| 165 | + describe('when file is empty', () => { |
| 166 | + test('emits error when part is received', (done) => { |
| 167 | + const form = getForm(name, { |
| 168 | + multiples: true, |
| 169 | + allowEmptyFiles: false, |
| 170 | + }); |
| 171 | + |
| 172 | + const part = new Stream(); |
| 173 | + part.mime = 'text/plain'; |
| 174 | + // eslint-disable-next-line max-nested-callbacks |
| 175 | + form.on('error', (error) => { |
| 176 | + expect(error.message).toBe( |
| 177 | + 'options.allowEmptyFiles is false, file size should be greather than 0', |
| 178 | + ); |
| 179 | + done(); |
| 180 | + }); |
| 181 | + form.onPart(part); |
| 182 | + part.emit('end'); |
| 183 | + }); |
| 184 | + }); |
| 185 | + |
| 186 | + describe('when file is not empty', () => { |
| 187 | + test('not emits error when part is received', () => { |
| 188 | + const form = getForm(name, { |
| 189 | + multiples: true, |
| 190 | + allowEmptyFiles: false, |
| 191 | + }); |
| 192 | + const formEmitSpy = jest.spyOn(form, 'emit'); |
| 193 | + |
| 194 | + const part = new Stream(); |
| 195 | + part.mime = 'text/plain'; |
| 196 | + form.onPart(part); |
| 197 | + part.emit('data', Buffer.alloc(1)); |
| 198 | + expect(formEmitSpy).not.toBeCalledWith('error'); |
| 199 | + }); |
| 200 | + }); |
| 201 | + }); |
| 202 | + |
| 203 | + describe('when allow empty files', () => { |
| 204 | + test('not emits error when part is received', () => { |
| 205 | + const form = getForm(name, { multiples: true }); |
| 206 | + const formEmitSpy = jest.spyOn(form, 'emit'); |
| 207 | + |
| 208 | + const part = new Stream(); |
| 209 | + part.mime = 'text/plain'; |
| 210 | + form.onPart(part); |
| 211 | + part.emit('end'); |
| 212 | + expect(formEmitSpy).not.toBeCalledWith('error'); |
| 213 | + }); |
| 214 | + }); |
| 215 | + |
| 216 | + describe('when file uploaded size is inferior than minFileSize option', () => { |
| 217 | + test('emits error when part is received', (done) => { |
| 218 | + const form = getForm(name, { multiples: true, minFileSize: 5 }); |
| 219 | + |
| 220 | + const part = new Stream(); |
| 221 | + part.mime = 'text/plain'; |
| 222 | + form.on('error', (error) => { |
| 223 | + expect(error.message).toBe( |
| 224 | + 'options.minFileSize (5 bytes) inferior, received 4 bytes of file data', |
| 225 | + ); |
| 226 | + done(); |
| 227 | + }); |
| 228 | + form.onPart(part); |
| 229 | + part.emit('data', Buffer.alloc(4)); |
| 230 | + }); |
| 231 | + }); |
| 232 | + |
| 233 | + describe('when file uploaded size is superior than minFileSize option', () => { |
| 234 | + test('not emits error when part is received', () => { |
| 235 | + const form = getForm(name, { multiples: true, minFileSize: 10 }); |
| 236 | + const formEmitSpy = jest.spyOn(form, 'emit'); |
| 237 | + |
| 238 | + const part = new Stream(); |
| 239 | + part.mime = 'text/plain'; |
| 240 | + form.onPart(part); |
| 241 | + part.emit('data', Buffer.alloc(11)); |
| 242 | + expect(formEmitSpy).not.toBeCalledWith('error'); |
| 243 | + }); |
| 244 | + }); |
| 245 | + }); |
| 246 | + |
162 | 247 | // test(`${name}: use custom options.filename instead of form._uploadPath`, () => {
|
163 | 248 | // const form = getForm(name, {
|
164 | 249 | // filename: (_) => path.join(__dirname, 'sasa'),
|
|
0 commit comments