gristlabs_grist-core/test/server/lib/helpers/TestProxyServer.ts
Florent 5e3cd94177
Introduce APP_HOME_INTERNAL_URL and fix duplicate docs (#915)
Context:

On self-hosted instances, some places in the code rely on the fact that we resolves public domains while being behind reverse proxies. This leads to cases where features are not available, such as the "Duplicate document" one.

Bugs that are solved - n self-hosted instances:

Impossible to open templates and tutorials right after having converted them;
Impossible to submit forms since version 1.1.13;
Impossible to restore a previous version of a document (snapshot);
Impossible to copy a document;

Solution:

Introduce the APP_HOME_INTERNAL_URL env variable, which is quite the same as APP_DOC_INTERNAL_URL except that it may point to any home worker;
Make /api/worker/:assignmentId([^/]+)/?* return not only the doc worker public url but also the internal one, and adapt the call points like fetchDocs;
Ensure that the home and doc worker internal urls are trusted by trustOrigin;

---------

Co-authored-by: jordigh <jordigh@octave.org>
2024-05-14 12:58:41 -04:00

44 lines
1.2 KiB
TypeScript

import {serveSomething, Serving} from "test/server/customUtil";
import * as express from "express";
import axios from "axios";
export class TestProxyServer {
public static async Prepare(portNumber: number): Promise<TestProxyServer> {
const server = new TestProxyServer();
await server._prepare(portNumber);
return server;
}
private _proxyCallsCounter: number = 0;
private _proxyServing: Serving;
private constructor() {
}
public wasProxyCalled(): boolean {
return this._proxyCallsCounter > 0;
}
public async dispose() {
await this._proxyServing.shutdown();
}
private async _prepare(portNumber: number) {
this._proxyServing = await serveSomething(app => {
app.use(express.json());
app.all('*', async (req: express.Request, res: express.Response) => {
this._proxyCallsCounter += 1;
let responseCode;
try {
const axiosResponse = await axios.post(req.url, req.body);
responseCode = axiosResponse.status;
} catch (error: any) {
responseCode = error.response.status;
}
res.sendStatus(responseCode);
res.end();
});
}, portNumber);
}
}