]> git.openstreetmap.org Git - nominatim-ui.git/blob - test/_bootstrap.js
update a test after openstreetmap data changed
[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 const reverse_only = !!process.env.REVERSE_ONLY;
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 Nominatim_Config.Reverse_Only = ${reverse_only};
32   `);
33
34
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}`);
40
41   if (use_proxy) {
42     // https://github.com/http-party/node-http-proxy#readme
43     const proxy = await httpProxy.createProxy({ changeOrigin: true, followRedirects: true });
44     this.proxy = proxy;
45     console.log('proxy started');
46
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)/);
50
51       let target = api_url_match
52         ? 'http://nominatim.openstreetmap.org/' + api_url_match[1]
53         : 'http://localhost:' + static_port;
54
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}`);
59   }
60
61
62   // 3. Create browser instance
63   global.browser = await puppeteer.launch({
64     defaultViewport: { width: 1024, height: 768 },
65     timeout: 10000,
66     // latency: 1000,
67     args: [
68       '--user-agent=Nominatim UI test suite Mozilla/5.0 Gecko/20100101 HeadlessChrome/90.0'
69     ]
70   });
71 };
72
73
74 exports.mochaGlobalTeardown = async function () {
75   global.browser.close();
76
77   await this.static_http_server.stop();
78   console.log('static server stopped');
79
80   if (use_proxy) {
81     await this.proxy.close();
82     console.log('proxy stopped');
83
84     this.proxy_server.close(() => console.log('proxy server stopped'));
85   }
86 };