]> git.openstreetmap.org Git - nominatim-ui.git/blob - test/_bootstrap.js
Add tests for running API and UI on the same server port (#151)
[nominatim-ui.git] / test / _bootstrap.js
1 const static_server = require('static-server');
2 const http = require('http');
3 const httpProxy = require('http-proxy');
4 const puppeteer = require('puppeteer');
5 const fse = require('fs-extra');
6
7 const testing_port = 9999; // this is the port all tests expect nominatim-ui to listen to
8
9 // The installation on https://nominatim.openstreetmap.org/ui/ is a bit more complex as
10 // for backward compatiblity they run the API and the UI on the same port. Nominatim-UI
11 // is installed in the /ui subdirectory plus their webserver has custom redirect rules.
12 //
13 // We can simulate that with a proxy.
14 const use_proxy = !!process.env.API_ON_SAME_PORT;
15 const static_port = use_proxy ? 9998 : 9999;
16
17
18 // Methods to run at the start and end of the mocha testsuite run
19 // https://mochajs.org/#global-setup-fixtures
20 exports.mochaGlobalSetup = async function () {
21   const workdir = 'dist_for_testing';
22
23   // 1. Prepare build directory
24   fse.mkdirpSync(workdir);
25   fse.copySync('dist', workdir);
26
27   let api_endpoint = use_proxy ? '/' : 'https:/nominatim.openstreetmap.org/';
28
29   fse.outputFile(workdir + '/theme/config.theme.js', `
30 Nominatim_Config.Nominatim_API_Endpoint = '${api_endpoint}';
31   `);
32
33
34   // 2. Start webserver pointing to build directory
35   // https://github.com/nbluis/static-server#readme
36   this.static_http_server = new static_server({ port: static_port, rootPath: workdir });
37   await this.static_http_server.start();
38   console.log(`static server serving ${workdir} directory running on port ${static_port}`);
39
40   if (use_proxy) {
41     // https://github.com/http-party/node-http-proxy#readme
42     const proxy = await httpProxy.createProxy({ changeOrigin: true, followRedirects: true });
43     this.proxy = proxy;
44     console.log('proxy started');
45
46     this.proxy_server = await http.createServer((req, res) => {
47       // identify if the requests should be served by the (remote) API or static webserver
48       let api_url_match = req.url.match(/\/(\w+\.php)/);
49
50       let target = api_url_match
51         ? 'http://nominatim.openstreetmap.org/' + api_url_match[1]
52         : 'http://localhost:' + static_port;
53
54       // console.log(`http proxy ${req.url} => ${target + req.url}`)
55       return proxy.web(req, res, { target: target });
56     }).listen(testing_port);
57     console.log(`proxy server started on port ${testing_port}`);
58   }
59
60
61   // 3. Create browser instance
62   global.browser = await puppeteer.launch({
63     defaultViewport: { width: 1024, height: 768 },
64     timeout: 5000,
65     args: [
66       '--user-agent=Nominatim UI test suite Mozilla/5.0 Gecko/20100101 HeadlessChrome/90.0'
67     ]
68   });
69 };
70
71
72 exports.mochaGlobalTeardown = async function () {
73   global.browser.close();
74
75   await this.static_http_server.stop();
76   console.log('static server stopped');
77
78   if (use_proxy) {
79     await this.proxy.close();
80     console.log('proxy stopped');
81
82     this.proxy_server.close(() => console.log('proxy server stopped'));
83   }
84 };