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');
7 const testing_port = 9999; // this is the port all tests expect nominatim-ui to listen to
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.
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 const reverse_only = !!process.env.REVERSE_ONLY;
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';
23 // 1. Prepare build directory
24 fse.mkdirpSync(workdir);
25 fse.copySync('dist', workdir);
27 let api_endpoint = use_proxy ? '/' : 'https:/nominatim.openstreetmap.org/';
29 fse.outputFile(workdir + '/theme/config.theme.js', `
30 Nominatim_Config.Nominatim_API_Endpoint = '${api_endpoint}';
31 Nominatim_Config.Reverse_Only = ${reverse_only};
35 // 2. Start webserver pointing to build directory
36 // https://github.com/nbluis/static-server#readme
37 this.static_http_server = new static_server({ port: static_port, rootPath: workdir });
38 await this.static_http_server.start();
39 console.log(`static server serving ${workdir} directory running on port ${static_port}`);
42 // https://github.com/http-party/node-http-proxy#readme
43 const proxy = await httpProxy.createProxy({ changeOrigin: true, followRedirects: true });
45 console.log('proxy started');
47 this.proxy_server = await http.createServer((req, res) => {
48 // identify if the requests should be served by the (remote) API or static webserver
49 let api_url_match = req.url.match(/\/(\w+\.php)/);
51 let target = api_url_match
52 ? 'http://nominatim.openstreetmap.org/' + api_url_match[1]
53 : 'http://localhost:' + static_port;
55 // console.log(`http proxy ${req.url} => ${target + req.url}`)
56 return proxy.web(req, res, { target: target });
57 }).listen(testing_port);
58 console.log(`proxy server started on port ${testing_port}`);
62 // 3. Create browser instance
63 global.browser = await puppeteer.launch({
64 defaultViewport: { width: 1024, height: 768 },
68 '--user-agent=Nominatim UI test suite Mozilla/5.0 Gecko/20100101 HeadlessChrome/90.0'
74 exports.mochaGlobalTeardown = async function () {
75 global.browser.close();
77 await this.static_http_server.stop();
78 console.log('static server stopped');
81 await this.proxy.close();
82 console.log('proxy stopped');
84 this.proxy_server.close(() => console.log('proxy server stopped'));