|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +const makeServer = require('../lib/makeserver'); |
| 4 | +const request = require('supertest'); |
| 5 | +const nock = require('nock'); |
| 6 | +const path = require('path'); |
| 7 | + |
| 8 | +jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000; |
| 9 | + |
| 10 | +describe('share endpoint (integration, real controller)', function() { |
| 11 | + const appOptions = { |
| 12 | + wwwroot: "./spec/mockwwwroot", |
| 13 | + hostName: "localhost", |
| 14 | + port: "3001", |
| 15 | + settings: { |
| 16 | + shareUrlPrefixes: { |
| 17 | + "g": { |
| 18 | + "service": "gist", |
| 19 | + "userAgent": "TerriaJS-Server", |
| 20 | + "gistFilename": "terriajs-server-catalog.json", |
| 21 | + "gistDescription": "TerriaJS Shared catalog" |
| 22 | + } |
| 23 | + }, |
| 24 | + newShareUrlPrefix: "g", |
| 25 | + shareMaxRequestSize: "200kb" |
| 26 | + } |
| 27 | + }; |
| 28 | + |
| 29 | + function buildApp() { |
| 30 | + const options = require('../lib/options').init(true); |
| 31 | + const mergedOptions = Object.assign(options, appOptions); |
| 32 | + return makeServer(mergedOptions); |
| 33 | + } |
| 34 | + |
| 35 | + beforeEach(() => { |
| 36 | + nock.cleanAll(); |
| 37 | + }); |
| 38 | + |
| 39 | + afterAll(() => { |
| 40 | + nock.restore(); |
| 41 | + }); |
| 42 | + |
| 43 | + describe('POST /share', function() { |
| 44 | + it('should return 201 and correct response for valid payload', function(done) { |
| 45 | + // Mock GitHub Gist API |
| 46 | + const fakeGistId = "123456"; |
| 47 | + nock('https://api.github.com') |
| 48 | + .post('/gists') |
| 49 | + .reply(201, { |
| 50 | + id: fakeGistId |
| 51 | + }); |
| 52 | + |
| 53 | + const payload = { test: "me" }; |
| 54 | + |
| 55 | + request(buildApp()) |
| 56 | + .post('/share') |
| 57 | + .send(payload) |
| 58 | + .expect(201) |
| 59 | + .expect('Content-Type', /json/) |
| 60 | + .expect(res => { |
| 61 | + const actualUrl = res.body.url; |
| 62 | + const expectedPath = `/share/g-${fakeGistId}`; |
| 63 | + expect(res.body.id).toBe(`g-${fakeGistId}`); |
| 64 | + expect(res.body.path).toBe(expectedPath); |
| 65 | + expect(actualUrl.endsWith(expectedPath)).toBeTrue(); |
| 66 | + }) |
| 67 | + .end(function(err) { |
| 68 | + if (err) return done.fail(err); |
| 69 | + done(); |
| 70 | + }); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should return 413 when payload exceeds shareMaxRequestSize', function(done) { |
| 74 | + const largePayload = 'a'.repeat(250000); // 250KB |
| 75 | + request(buildApp()) |
| 76 | + .post('/share') |
| 77 | + .set('Content-Type', 'application/json') |
| 78 | + .send(`{"data":"${largePayload}"}`) |
| 79 | + .expect(413) |
| 80 | + .expect('Content-Type', /text|plain/) |
| 81 | + .expect(res => { |
| 82 | + expect( |
| 83 | + res.text.includes('Payload Too Large') |
| 84 | + ).toBeTrue(); |
| 85 | + }) |
| 86 | + .end(function(err) { |
| 87 | + if (err) return done.fail(err); |
| 88 | + done(); |
| 89 | + }); |
| 90 | + }); |
| 91 | + }); |
| 92 | +}); |
0 commit comments