|
| 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 | + |
| 36 | + .then(() => ngServe('--proxy-config', proxyConfigFile)) |
| 37 | + .then(() => request('http://localhost:4200/api/test')) |
| 38 | + .then(body => { |
| 39 | + if (!body.match(/TEST_API_RETURN/)) { |
| 40 | + throw new Error('Response does not match expected value.'); |
| 41 | + } |
| 42 | + }) |
| 43 | + .then(() => server.close(), (err) => { server.close(); throw err; }) |
| 44 | + .then(() => killAllProcesses(), (err) => { killAllProcesses(); throw err; }) |
| 45 | + |
| 46 | + .then(() => updateJsonFile('.angular-cli.json', configJson => { |
| 47 | + const app = configJson.defaults; |
| 48 | + app.serve = { |
| 49 | + proxyConfig: proxyConfigFile |
| 50 | + }; |
| 51 | + })) |
| 52 | + .then(() => ngServe()) |
| 53 | + .then(() => request('http://localhost:4200/api/test')) |
| 54 | + .then(body => { |
| 55 | + if (!body.match(/TEST_API_RETURN/)) { |
| 56 | + throw new Error('Response does not match expected value.'); |
| 57 | + } |
| 58 | + }) |
| 59 | + .then(() => server.close(), (err) => { server.close(); throw err; }) |
| 60 | + .then(() => killAllProcesses(), (err) => { killAllProcesses(); throw err; }) |
| 61 | + |
| 62 | + // A non-existing proxy file should error. |
| 63 | + |
| 64 | + .then(() => expectToFail(() => ng('serve', '--proxy-config', 'proxy.non-existent.json'))) |
| 65 | + .then(() => updateJsonFile('.angular-cli.json', configJson => { |
| 66 | + const app = configJson.defaults; |
| 67 | + app.serve = { |
| 68 | + proxyConfig: 'proxy.non-existent.json' |
| 69 | + }; |
| 70 | + })) |
| 71 | + .then(() => expectToFail(() => ngServe())); |
| 72 | +} |
0 commit comments