|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const http = require('http'); |
| 4 | +const { createGraphQLUploadMiddleware } = require('../lib/GraphQL/helpers/graphqlUpload'); |
| 5 | +const { handleUpload } = require('../lib/GraphQL/loaders/filesMutations'); |
| 6 | + |
| 7 | +const createMockReadStream = () => { |
| 8 | + const stream = { |
| 9 | + pipe(destination) { |
| 10 | + setImmediate(() => { |
| 11 | + if (stream.endHandler) { |
| 12 | + stream.endHandler(); |
| 13 | + } |
| 14 | + }); |
| 15 | + return destination; |
| 16 | + }, |
| 17 | + on(event, handler) { |
| 18 | + if (event === 'end') { |
| 19 | + stream.endHandler = handler; |
| 20 | + } |
| 21 | + }, |
| 22 | + destroy() {}, |
| 23 | + }; |
| 24 | + return stream; |
| 25 | +}; |
| 26 | + |
| 27 | +const createMockHttpResponse = (statusCode, body) => ({ |
| 28 | + statusCode, |
| 29 | + on(event, handler) { |
| 30 | + if (event === 'data') { |
| 31 | + handler(body); |
| 32 | + } |
| 33 | + if (event === 'end') { |
| 34 | + handler(); |
| 35 | + } |
| 36 | + }, |
| 37 | +}); |
| 38 | + |
| 39 | +describe('graphqlUpload helper', () => { |
| 40 | + it('skips middleware for non-multipart requests', async () => { |
| 41 | + const middleware = createGraphQLUploadMiddleware({ maxFileSize: 1000 }); |
| 42 | + const req = { is: type => type !== 'multipart/form-data' }; |
| 43 | + const next = jasmine.createSpy('next'); |
| 44 | + |
| 45 | + await middleware(req, {}, next); |
| 46 | + |
| 47 | + expect(next).toHaveBeenCalledWith(); |
| 48 | + expect(req.body).toBeUndefined(); |
| 49 | + }); |
| 50 | + |
| 51 | + it('forwards multipart parsing errors to Express', async () => { |
| 52 | + const middleware = createGraphQLUploadMiddleware({ maxFileSize: 1000 }); |
| 53 | + const req = { |
| 54 | + is: () => true, |
| 55 | + headers: { 'content-type': 'multipart/form-data; boundary=----parse' }, |
| 56 | + }; |
| 57 | + const next = jasmine.createSpy('next'); |
| 58 | + |
| 59 | + await middleware(req, {}, next); |
| 60 | + |
| 61 | + expect(next).toHaveBeenCalledWith(jasmine.any(Error)); |
| 62 | + }); |
| 63 | +}); |
| 64 | + |
| 65 | +describe('filesMutations handleUpload', () => { |
| 66 | + const upload = { |
| 67 | + createReadStream: createMockReadStream, |
| 68 | + filename: 'café.txt', |
| 69 | + mimetype: 'text/plain', |
| 70 | + }; |
| 71 | + const config = { |
| 72 | + serverURL: 'http://localhost:8378/1', |
| 73 | + headers: {}, |
| 74 | + }; |
| 75 | + |
| 76 | + afterEach(() => { |
| 77 | + if (jasmine.isSpy(http.request)) { |
| 78 | + http.request.and.callThrough(); |
| 79 | + } |
| 80 | + }); |
| 81 | + |
| 82 | + it('rejects non-2xx responses from the internal /files handoff', async () => { |
| 83 | + spyOn(http, 'request').and.callFake((_options, callback) => { |
| 84 | + const req = { on() {}, end() {} }; |
| 85 | + setImmediate(() => { |
| 86 | + callback(createMockHttpResponse(400, JSON.stringify({ code: 130, error: 'bad upload' }))); |
| 87 | + }); |
| 88 | + return req; |
| 89 | + }); |
| 90 | + |
| 91 | + await expectAsync( |
| 92 | + handleUpload(Promise.resolve(upload), config) |
| 93 | + ).toBeRejectedWith(jasmine.objectContaining({ code: 130 })); |
| 94 | + }); |
| 95 | + |
| 96 | + it('rejects invalid JSON responses from the internal /files handoff', async () => { |
| 97 | + spyOn(http, 'request').and.callFake((_options, callback) => { |
| 98 | + const req = { on() {}, end() {} }; |
| 99 | + setImmediate(() => { |
| 100 | + callback(createMockHttpResponse(200, 'not-json')); |
| 101 | + }); |
| 102 | + return req; |
| 103 | + }); |
| 104 | + |
| 105 | + await expectAsync( |
| 106 | + handleUpload(Promise.resolve(upload), config) |
| 107 | + ).toBeRejectedWith(jasmine.objectContaining({ code: Parse.Error.FILE_SAVE_ERROR })); |
| 108 | + }); |
| 109 | + |
| 110 | + it('wraps request failures in FILE_SAVE_ERROR', async () => { |
| 111 | + spyOn(http, 'request').and.throwError('connection failed'); |
| 112 | + |
| 113 | + await expectAsync( |
| 114 | + handleUpload(Promise.resolve(upload), config) |
| 115 | + ).toBeRejectedWith(jasmine.objectContaining({ code: Parse.Error.FILE_SAVE_ERROR })); |
| 116 | + }); |
| 117 | +}); |
0 commit comments