blob: 29a89bf7382754513506611bf2023ac9dc73f281 [file] [log] [blame] [raw]
Matt Godboltf68198a2020-09-26 17:50:40 -05001// Copyright (c) 2017, Compiler Explorer Authors
Matt Godbolt22c87052017-12-21 10:58:24 -06002// 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 Morton044dcfb2020-09-26 16:59:26 -040025import express from 'express';
Matt Godbolt22c87052017-12-21 10:58:24 -060026
Matt Godboltf2c1e0b2022-05-09 23:13:50 -050027import {SourceHandler} from '../../lib/handlers/source';
28import {chai} from '../utils';
Matt Godbolt22c87052017-12-21 10:58:24 -060029
30describe('Sources', () => {
31 const app = express();
Austin Morton044dcfb2020-09-26 16:59:26 -040032 const handler = new SourceHandler(
Matt Godboltf2c1e0b2022-05-09 23:13:50 -050033 [
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 Godbolt22c87052017-12-21 10:58:24 -060043 app.use('/source', handler.handle.bind(handler));
44
45 it('should list', () => {
Matt Godboltf2c1e0b2022-05-09 23:13:50 -050046 return chai
47 .request(app)
Matt Godbolt22c87052017-12-21 10:58:24 -060048 .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 Blancoccff4b92020-08-04 22:39:02 +020053 res.should.have.header('Yibble', 'boing');
Matt Godbolt22c87052017-12-21 10:58:24 -060054 })
55 .catch(function (err) {
56 throw err;
57 });
58 });
59 it('should fetch files', () => {
Matt Godboltf2c1e0b2022-05-09 23:13:50 -050060 return chai
61 .request(app)
Matt Godbolt22c87052017-12-21 10:58:24 -060062 .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 Blancoccff4b92020-08-04 22:39:02 +020067 res.should.have.header('Yibble', 'boing');
Matt Godbolt22c87052017-12-21 10:58:24 -060068 })
69 .catch(function (err) {
70 throw err;
71 });
72 });
73});