From 47c8804a01d69867bae8cffad1605a53d577512d Mon Sep 17 00:00:00 2001 From: fflorent Date: Thu, 2 May 2024 17:20:19 +0200 Subject: [PATCH] Use nip.io to have a specific hostname for proxy Also use GRIST_ORG_IN_PATH to make it work as expected, and GRIST_SINGLE_PORT=0 to ensure to query the pool of workers. --- test/server/lib/DocApi.ts | 9 ++++++--- test/server/lib/helpers/TestServer.ts | 16 +++++++++++----- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/test/server/lib/DocApi.ts b/test/server/lib/DocApi.ts index 60ef23f1..0770283e 100644 --- a/test/server/lib/DocApi.ts +++ b/test/server/lib/DocApi.ts @@ -173,12 +173,14 @@ describe('DocApi', function () { const additionalEnvConfiguration = { ALLOWED_WEBHOOK_DOMAINS: `example.com,localhost:${webhooksTestPort}`, GRIST_DATA_DIR: dataDir, - APP_HOME_URL: await proxy.getServerUrl() + APP_HOME_URL: await proxy.getServerUrl(), + GRIST_ORG_IN_PATH: 'true', + GRIST_SINGLE_PORT: '0', }; home = await TestServer.startServer('home', tmpDir, suitename, additionalEnvConfiguration); docs = await TestServer.startServer('docs', tmpDir, suitename, additionalEnvConfiguration, home.serverUrl); - proxy.start(home, docs); + await proxy.start(home, docs); homeUrl = serverUrl = await proxy.getServerUrl(); iterateOverAccountHeaders(account => { @@ -3182,8 +3184,9 @@ function testDocApi() { it("GET /docs/{did1}/compare/{did2} tracks changes between docs", async function () { // Pass kiwi's headers as it contains both Authorization and Origin headers // if run behind a proxy, so we can ensure that the Origin header check is not made. + const userApiServerUrl = docs.proxiedServer ? serverUrl : undefined; const chimpyApi = home.makeUserApi( - ORG_NAME, 'chimpy', { serverUrl, headers: chimpy.headers as Record } + ORG_NAME, 'chimpy', { serverUrl: userApiServerUrl, headers: chimpy.headers as Record } ); const ws1 = (await chimpyApi.getOrgWorkspaces('current'))[0].id; const docId1 = await chimpyApi.newDoc({name: 'testdoc1'}, ws1); diff --git a/test/server/lib/helpers/TestServer.ts b/test/server/lib/helpers/TestServer.ts index e0a65d86..3af99b2b 100644 --- a/test/server/lib/helpers/TestServer.ts +++ b/test/server/lib/helpers/TestServer.ts @@ -199,7 +199,12 @@ export class TestServer { // FIXME: found that TestProxyServer exist, what should I do? :'( export class TestServerProxy { - public static readonly HOSTNAME: string = 'grist-test-proxy.localhost'; + + // Use a different hostname for the proxy than the doc and home workers' + // so we can ensure that either we omit the Origin header (so the internal calls to home and doc workers + // are not considered as CORS requests), or otherwise we fail because the hostnames are different + // https://github.com/gristlabs/grist-core/blob/24b39c651b9590cc360cc91b587d3e1b301a9c63/app/server/lib/requestUtils.ts#L85-L98 + public static readonly HOSTNAME: string = 'grist-test-proxy.127.0.0.1.nip.io'; private _stopped: boolean = false; private _app = express(); @@ -210,18 +215,19 @@ export class TestServerProxy { public constructor() { this._address = new Promise(resolve => { - this._server = this._app.listen(0, TestServerProxy.HOSTNAME, () => { + this._server = this._app.listen(0, () => { resolve(this._server.address() as AddressInfo); }); }); } - public start(homeServer: TestServer, docServer: TestServer) { + public async start(homeServer: TestServer, docServer: TestServer) { this._app.all(['/dw/dw1', '/dw/dw1/*'], (oreq, ores) => this._getRequestHandlerFor(docServer)); this._app.all('/*', this._getRequestHandlerFor(homeServer)); // Forbid now the use of serverUrl property homeServer.disallowDirectAccess(); docServer.disallowDirectAccess(); + log.info('proxy server running on ', await this.getServerUrl()); } public async getAddress() { @@ -254,11 +260,11 @@ export class TestServerProxy { headers: oreq.headers, }; - log.debug('[proxy] Requesting: ' + oreq.url); + log.debug(`[proxy] Requesting (method=${oreq.method}): ${new URL(oreq.url, serverUrl).href}`); const creq = http .request(options, pres => { - log.debug('[proxy] Received response for ' + pres.url); + log.debug('[proxy] Received response for ' + oreq.url); // set encoding, required? pres.setEncoding('utf8');