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;
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}';
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}`);
41 // https://github.com/http-party/node-http-proxy#readme
42 const proxy = await httpProxy.createProxy({ changeOrigin: true, followRedirects: true });
44 console.log('proxy started');
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)/);
50 let target = api_url_match
51 ? 'http://nominatim.openstreetmap.org/' + api_url_match[1]
52 : 'http://localhost:' + static_port;
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}`);
61 // 3. Create browser instance
62 global.browser = await puppeteer.launch({
63 defaultViewport: { width: 1024, height: 768 },
66 '--user-agent=Nominatim UI test suite Mozilla/5.0 Gecko/20100101 HeadlessChrome/90.0'
72 exports.mochaGlobalTeardown = async function () {
73 global.browser.close();
75 await this.static_http_server.stop();
76 console.log('static server stopped');
79 await this.proxy.close();
80 console.log('proxy stopped');
82 this.proxy_server.close(() => console.log('proxy server stopped'));