Skip to content

Commit

Permalink
feat(scope): BasePath
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Schrimpf committed Apr 21, 2023
1 parent b94fff0 commit aaab6dd
Show file tree
Hide file tree
Showing 2 changed files with 167 additions and 1 deletion.
157 changes: 157 additions & 0 deletions packages/gitlab-backend/src/service/router.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,160 @@ describe('createRouter', () => {
});
});
});

describe('createRouter with baseUrl', () => {
let app: express.Application;
const server = setupServer(
rest.get(
'https://non-existing-example.com/api/v4/projects/434',
(req, res, ctx) => {
return res(
ctx.status(200),
ctx.json({
url: req.url.toString(),
headers: req.headers.all(),
})
);
}
),
rest.get(
'https://non-existing-example-2.com/api/v4/projects/434',
(req, res, ctx) => {
return res(
ctx.status(200),
ctx.json({
url: req.url.toString(),
headers: req.headers.all(),
})
);
}
)
);

const basePath = '/docs';

const config = new ConfigReader({
backend: {
baseUrl: `https://localhost:7007${basePath}`,
},
integrations: {
gitlab: [
{
host: 'non-existing-example.com',
apiBaseUrl: 'https://non-existing-example.com/api/v4',
},
{
host: 'non-existing-example-2.com',
apiBaseUrl: 'https://non-existing-example-2.com/api/v4',
},
],
},
});

beforeAll(async () => {
const router = await createRouter({
logger: getVoidLogger(),
config,
});
app = express().use(`${basePath}/api/gitlab`, router);
server.listen({
onUnhandledRequest: ({ headers }, print) => {
if (headers.get('User-Agent') === 'supertest') {
return;
}
print.error();
},
});
});

afterAll(() => server.close());

beforeEach(async () => {
jest.resetAllMocks();
server.resetHandlers();
});

describe('GET Request', () => {
it('First instance should work', async () => {
const agent = request.agent(app);
// this is set to let msw pass test requests through the mock server
agent.set('User-Agent', 'supertest');
const response = await agent.get(
`${basePath}/api/gitlab/non-existing-example.com/projects/434`
);
expect(response.status).toEqual(200);
expect(response.body).toEqual({
headers: {
'accept-encoding': 'gzip, deflate',
connection: 'close',
host: 'non-existing-example.com',
'user-agent': 'supertest',
},
url: 'https://non-existing-example.com/api/v4/projects/434',
});
});

it('Second instance should work', async () => {
const agent = request.agent(app);
// this is set to let msw pass test requests through the mock server
agent.set('User-Agent', 'supertest');
const response = await agent.get(
`${basePath}/api/gitlab/non-existing-example-2.com/projects/434`
);
expect(response.status).toEqual(200);
expect(response.body).toEqual({
headers: {
'accept-encoding': 'gzip, deflate',
connection: 'close',
host: 'non-existing-example-2.com',
'user-agent': 'supertest',
},
url: 'https://non-existing-example-2.com/api/v4/projects/434',
});
});

it('Third instance should fail', async () => {
const agent = request.agent(app);
// this is set to let msw pass test requests through the mock server
agent.set('User-Agent', 'supertest');
const response = await agent.get(
'/api/gitlab/non-existing-example.com/projects/434'
);
expect(response.status).toEqual(404);
});
});

describe('Error requests', () => {
it('Methods different from GET should reject', async () => {
const agent = request.agent(app);
// this is set to let msw pass test requests through the mock server
agent.set('User-Agent', 'supertest');
for (const method of [
'post',
'delete',
'put',
'options',
'trace',
'patch',
]) {
// @ts-ignore
const response = await agent?.[method](
`${basePath}/api/gitlab/non-existing-example-2.com/projects/434`
);
expect(response.status).toEqual(404);
expect(response.body).toEqual({});
}
});

it('Request out of instance should reject', async () => {
const agent = request.agent(app);
// this is set to let msw pass test requests through the mock server
agent.set('User-Agent', 'supertest');
const response = await agent.get(
`${basePath}/api/gitlab/does.not.exist/projects/434`
);
expect(response.status).toEqual(404);
expect(response.body).toEqual({});
});
});
});
11 changes: 10 additions & 1 deletion packages/gitlab-backend/src/service/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ import { Logger } from 'winston';
import { createProxyMiddleware } from 'http-proxy-middleware';
import { IncomingMessage } from 'http';

function getBasePath(config: Config) {
const baseUrl = config.getOptionalString('backend.baseUrl');
if (!baseUrl) {
return undefined;
}
return new URL(baseUrl).pathname.replace(/\/$/, '');
}

export interface RouterOptions {
logger: Logger;
config: Config;
Expand All @@ -19,6 +27,7 @@ export async function createRouter(
options: RouterOptions
): Promise<express.Router> {
const { logger, config } = options;
const basePath = getBasePath(config) || '';

const gitlabIntegrations: GitLabIntegrationConfig[] =
readGitLabIntegrationConfigs(
Expand Down Expand Up @@ -48,7 +57,7 @@ export async function createRouter(
},
logProvider: () => logger,
pathRewrite: {
[`^/api/gitlab/${host}`]: apiUrl.pathname,
[`^${basePath}/api/gitlab/${host}`]: apiUrl.pathname,
},
})
);
Expand Down

0 comments on commit aaab6dd

Please sign in to comment.