|
| 1 | +import * as express from 'express'; |
| 2 | +import * as http from 'http'; |
| 3 | + |
| 4 | +import {writeFile} from '../../utils/fs'; |
| 5 | +import { request } from '../../utils/http'; |
| 6 | +import { assetDir } from '../../utils/assets'; |
| 7 | +import { killAllProcesses } from '../../utils/process'; |
| 8 | +import { ngServe } from '../../utils/project'; |
| 9 | +import { updateJsonFile } from '../../utils/project'; |
| 10 | +import {expectToFail} from "../../utils/utils"; |
| 11 | + |
| 12 | +export default function() { |
| 13 | + // Create an express app that serves as a proxy. |
| 14 | + const app = express(); |
| 15 | + const server = http.createServer(app); |
| 16 | + server.listen(0); |
| 17 | + |
| 18 | + app.set('port', server.address().port); |
| 19 | + app.get('/api/test', function (req, res) { |
| 20 | + res.send('TEST_API_RETURN'); |
| 21 | + }); |
| 22 | + |
| 23 | + const backendHost = 'localhost'; |
| 24 | + const backendPort = server.address().port; |
| 25 | + const proxyServerUrl = `http://${backendHost}:${backendPort}`; |
| 26 | + const proxyConfigFile = 'proxy.config.json'; |
| 27 | + const proxyConfig = { |
| 28 | + '/api/*': { |
| 29 | + target: proxyServerUrl |
| 30 | + } |
| 31 | + }; |
| 32 | + |
| 33 | + return Promise.resolve() |
| 34 | + .then(() => writeFile(proxyConfigFile, JSON.stringify(proxyConfig, null, 2))) |
| 35 | + .then(() => updateJsonFile('.angular-cli.json', configJson => { |
| 36 | + const app = configJson.defaults; |
| 37 | + app.serve = { |
| 38 | + proxyConfig: proxyConfigFile |
| 39 | + }; |
| 40 | + })) |
| 41 | + .then(() => ngServe()) |
| 42 | + .then(() => request('http://localhost:4200/api/test')) |
| 43 | + .then(body => { |
| 44 | + if (!body.match(/TEST_API_RETURN/)) { |
| 45 | + throw new Error('Response does not match expected value.'); |
| 46 | + } |
| 47 | + }) |
| 48 | + .then(() => server.close(), (err) => { server.close(); throw err; }) |
| 49 | + .then(() => killAllProcesses(), (err) => { killAllProcesses(); throw err; }) |
| 50 | + |
| 51 | + // A non-existing proxy file should error. |
| 52 | + .then(() => updateJsonFile('.angular-cli.json', configJson => { |
| 53 | + const app = configJson.defaults; |
| 54 | + app.serve = { |
| 55 | + proxyConfig: 'proxy.non-existent.json' |
| 56 | + }; |
| 57 | + })) |
| 58 | + .then(() => expectToFail(() => ngServe())); |
| 59 | +} |
0 commit comments