Matt Godbolt | f68198a | 2020-09-26 17:50:40 -0500 | [diff] [blame] | 1 | // Copyright (c) 2017, Compiler Explorer Authors |
Matt Godbolt | 22c8705 | 2017-12-21 10:58:24 -0600 | [diff] [blame] | 2 | // All rights reserved. |
| 3 | // |
| 4 | // Redistribution and use in source and binary forms, with or without |
| 5 | // modification, are permitted provided that the following conditions are met: |
| 6 | // |
| 7 | // * Redistributions of source code must retain the above copyright notice, |
| 8 | // this list of conditions and the following disclaimer. |
| 9 | // * Redistributions in binary form must reproduce the above copyright |
| 10 | // notice, this list of conditions and the following disclaimer in the |
| 11 | // documentation and/or other materials provided with the distribution. |
| 12 | // |
| 13 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 14 | // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 16 | // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 17 | // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 18 | // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 19 | // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 20 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 21 | // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 22 | // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 23 | // POSSIBILITY OF SUCH DAMAGE. |
| 24 | |
Austin Morton | 044dcfb | 2020-09-26 16:59:26 -0400 | [diff] [blame] | 25 | import express from 'express'; |
Matt Godbolt | 22c8705 | 2017-12-21 10:58:24 -0600 | [diff] [blame] | 26 | |
Matt Godbolt | f2c1e0b | 2022-05-09 23:13:50 -0500 | [diff] [blame] | 27 | import {SourceHandler} from '../../lib/handlers/source'; |
| 28 | import {chai} from '../utils'; |
Matt Godbolt | 22c8705 | 2017-12-21 10:58:24 -0600 | [diff] [blame] | 29 | |
| 30 | describe('Sources', () => { |
| 31 | const app = express(); |
Austin Morton | 044dcfb | 2020-09-26 16:59:26 -0400 | [diff] [blame] | 32 | const handler = new SourceHandler( |
Matt Godbolt | f2c1e0b | 2022-05-09 23:13:50 -0500 | [diff] [blame] | 33 | [ |
| 34 | { |
| 35 | urlpart: 'moose', |
| 36 | list: () => Promise.resolve({moose: 'pig'}), |
| 37 | load: name => Promise.resolve({file: `File called ${name}`}), |
| 38 | save: null, |
| 39 | }, |
| 40 | ], |
| 41 | res => res.setHeader('Yibble', 'boing'), |
| 42 | ); |
Matt Godbolt | 22c8705 | 2017-12-21 10:58:24 -0600 | [diff] [blame] | 43 | app.use('/source', handler.handle.bind(handler)); |
| 44 | |
| 45 | it('should list', () => { |
Matt Godbolt | f2c1e0b | 2022-05-09 23:13:50 -0500 | [diff] [blame] | 46 | return chai |
| 47 | .request(app) |
Matt Godbolt | 22c8705 | 2017-12-21 10:58:24 -0600 | [diff] [blame] | 48 | .get('/source/moose/list') |
| 49 | .then(res => { |
| 50 | res.should.have.status(200); |
| 51 | res.should.be.json; |
| 52 | res.body.should.deep.equals({moose: 'pig'}); |
Rubén Rincón Blanco | ccff4b9 | 2020-08-04 22:39:02 +0200 | [diff] [blame] | 53 | res.should.have.header('Yibble', 'boing'); |
Matt Godbolt | 22c8705 | 2017-12-21 10:58:24 -0600 | [diff] [blame] | 54 | }) |
| 55 | .catch(function (err) { |
| 56 | throw err; |
| 57 | }); |
| 58 | }); |
| 59 | it('should fetch files', () => { |
Matt Godbolt | f2c1e0b | 2022-05-09 23:13:50 -0500 | [diff] [blame] | 60 | return chai |
| 61 | .request(app) |
Matt Godbolt | 22c8705 | 2017-12-21 10:58:24 -0600 | [diff] [blame] | 62 | .get('/source/moose/load/Grunkle') |
| 63 | .then(res => { |
| 64 | res.should.have.status(200); |
| 65 | res.should.be.json; |
| 66 | res.body.should.deep.equals({file: 'File called Grunkle'}); |
Rubén Rincón Blanco | ccff4b9 | 2020-08-04 22:39:02 +0200 | [diff] [blame] | 67 | res.should.have.header('Yibble', 'boing'); |
Matt Godbolt | 22c8705 | 2017-12-21 10:58:24 -0600 | [diff] [blame] | 68 | }) |
| 69 | .catch(function (err) { |
| 70 | throw err; |
| 71 | }); |
| 72 | }); |
| 73 | }); |