improve the config test too
Signed-off-by: Fredrik Adelöw <freben@gmail.com>
This commit is contained in:
@@ -71,13 +71,11 @@
|
||||
"@backstage/config-loader": "workspace:^",
|
||||
"@backstage/errors": "workspace:^",
|
||||
"@types/http-proxy-middleware": "^1.0.0",
|
||||
"@types/supertest": "^2.0.8",
|
||||
"@types/uuid": "^9.0.0",
|
||||
"@types/yup": "^0.32.0",
|
||||
"msw": "^2.0.0",
|
||||
"node-fetch": "^2.6.7",
|
||||
"portfinder": "^1.0.32",
|
||||
"supertest": "^6.1.3"
|
||||
"portfinder": "^1.0.32"
|
||||
},
|
||||
"configSchema": "config.d.ts"
|
||||
}
|
||||
|
||||
@@ -14,51 +14,46 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { createBackend } from '@backstage/backend-defaults';
|
||||
import {
|
||||
HostDiscovery,
|
||||
loggerToWinstonLogger,
|
||||
} from '@backstage/backend-common';
|
||||
coreServices,
|
||||
createServiceFactory,
|
||||
} from '@backstage/backend-plugin-api';
|
||||
import {
|
||||
mockServices,
|
||||
setupRequestMockHandlers,
|
||||
} from '@backstage/backend-test-utils';
|
||||
import {
|
||||
ConfigSources,
|
||||
MutableConfigSource,
|
||||
StaticConfigSource,
|
||||
} from '@backstage/config-loader';
|
||||
import express from 'express';
|
||||
import { http, HttpResponse } from 'msw';
|
||||
import { HttpResponse, http, passthrough } from 'msw';
|
||||
import { setupServer } from 'msw/node';
|
||||
import request from 'supertest';
|
||||
import { createRouter } from './router';
|
||||
import { mockServices } from '@backstage/backend-test-utils';
|
||||
import fetch from 'node-fetch';
|
||||
import portFinder from 'portfinder';
|
||||
|
||||
// this test is stored in its own file to work around the mocked
|
||||
// http-proxy-middleware module used in the main test file
|
||||
|
||||
describe('createRouter reloadable configuration', () => {
|
||||
const server = setupServer(
|
||||
http.get('https://non-existing-example.com/', req =>
|
||||
HttpResponse.json({
|
||||
url: req.request.url.toString(),
|
||||
headers: req.request.headers,
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
beforeAll(() =>
|
||||
server.listen({
|
||||
onUnhandledRequest: ({ headers }, print) => {
|
||||
if (headers.get('User-Agent') === 'supertest') {
|
||||
return;
|
||||
}
|
||||
print.error();
|
||||
},
|
||||
}),
|
||||
);
|
||||
|
||||
afterAll(() => server.close());
|
||||
afterEach(() => server.resetHandlers());
|
||||
const server = setupServer();
|
||||
setupRequestMockHandlers(server);
|
||||
|
||||
it('should be able to observe the config', async () => {
|
||||
const logger = loggerToWinstonLogger(mockServices.logger.mock());
|
||||
const host = 'localhost';
|
||||
const port = await portFinder.getPortPromise({ host });
|
||||
const baseUrl = `http://${host}:${port}`;
|
||||
|
||||
server.use(
|
||||
http.all(`${baseUrl}/*`, passthrough),
|
||||
http.get('https://non-existing-example.com/*', req =>
|
||||
HttpResponse.json({
|
||||
url: req.request.url.toString(),
|
||||
headers: req.request.headers,
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
// Grab the subscriber function and use mutable config data to mock a config file change
|
||||
const mutableConfigSource = MutableConfigSource.create({ data: {} });
|
||||
@@ -67,18 +62,14 @@ describe('createRouter reloadable configuration', () => {
|
||||
StaticConfigSource.create({
|
||||
data: {
|
||||
backend: {
|
||||
baseUrl: 'http://localhost:7007',
|
||||
listen: {
|
||||
port: 7007,
|
||||
},
|
||||
baseUrl,
|
||||
listen: { host, port },
|
||||
},
|
||||
proxy: {
|
||||
endpoints: {
|
||||
'/test': {
|
||||
target: 'https://non-existing-example.com',
|
||||
pathRewrite: {
|
||||
'.*': '/',
|
||||
},
|
||||
credentials: 'dangerously-allow-unauthenticated',
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -88,40 +79,38 @@ describe('createRouter reloadable configuration', () => {
|
||||
]),
|
||||
);
|
||||
|
||||
const discovery = HostDiscovery.fromConfig(config);
|
||||
const router = await createRouter({
|
||||
config,
|
||||
logger,
|
||||
discovery,
|
||||
const backend = createBackend();
|
||||
backend.add(import('../alpha'));
|
||||
backend.add(
|
||||
createServiceFactory({
|
||||
service: coreServices.rootConfig,
|
||||
deps: {},
|
||||
factory: () => config,
|
||||
}),
|
||||
);
|
||||
backend.add(mockServices.rootLogger.factory());
|
||||
await backend.start();
|
||||
|
||||
await expect(fetch(`${baseUrl}/api/proxy/test`)).resolves.toMatchObject({
|
||||
status: 200,
|
||||
});
|
||||
expect(router).toBeDefined();
|
||||
|
||||
const app = express();
|
||||
app.use(router);
|
||||
|
||||
const agent = request.agent(app);
|
||||
// this is set to let msw pass test requests through the mock server
|
||||
agent.set('User-Agent', 'supertest');
|
||||
|
||||
const response1 = await agent.get('/test');
|
||||
|
||||
expect(response1.status).toEqual(200);
|
||||
await expect(
|
||||
fetch(`${baseUrl}/api/proxy/test2`),
|
||||
).resolves.not.toMatchObject({ status: 200 });
|
||||
|
||||
mutableConfigSource.setData({
|
||||
proxy: {
|
||||
endpoints: {
|
||||
'/test2': {
|
||||
target: 'https://non-existing-example.com',
|
||||
pathRewrite: {
|
||||
'.*': '/',
|
||||
},
|
||||
credentials: 'dangerously-allow-unauthenticated',
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const response2 = await agent.get('/test2');
|
||||
|
||||
expect(response2.status).toEqual(200);
|
||||
await expect(fetch(`${baseUrl}/api/proxy/test2`)).resolves.toMatchObject({
|
||||
status: 200,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -35,7 +35,7 @@ describe('credentials', () => {
|
||||
|
||||
it('handles all valid credentials settings', async () => {
|
||||
const host = 'localhost';
|
||||
const port = await portFinder.getPortPromise();
|
||||
const port = await portFinder.getPortPromise({ host });
|
||||
const baseUrl = `http://${host}:${port}`;
|
||||
|
||||
const config = {
|
||||
@@ -82,7 +82,7 @@ describe('credentials', () => {
|
||||
};
|
||||
|
||||
worker.use(
|
||||
http.all(`${baseUrl}/*`, () => passthrough()),
|
||||
http.all(`${baseUrl}/*`, passthrough),
|
||||
http.get('http://target.com/*', req => {
|
||||
const auth = req.request.headers.get('authorization');
|
||||
return HttpResponse.json({
|
||||
|
||||
Reference in New Issue
Block a user