Skip to content

Commit

Permalink
fix: handle invalid url (#1000)
Browse files Browse the repository at this point in the history
  • Loading branch information
rgwozdz authored May 1, 2024
1 parent bc7cc8b commit 593910d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 47 deletions.
5 changes: 5 additions & 0 deletions .changeset/modern-dryers-hang.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@koopjs/output-geoservices": patch
---

- handle MapServer requests with invalid url error
14 changes: 6 additions & 8 deletions packages/output-geoservices/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class GeoServices {
{
path: '$namespace/rest/services/$providerParams/MapServer*',
methods: ['get', 'post'],
handler: 'generalHandler',
handler: 'invalidUrlHandler',
},
];

Expand Down Expand Up @@ -144,13 +144,11 @@ class GeoServices {
return false;
}

async generalHandler(req, res) {
try {
const data = await this.model.pull(req);
return FeatureServer.route(req, res, data);
} catch (error) {
this.#errorHandler(error, req, res);
}
async invalidUrlHandler(req, res) {
const error = new Error('Invalid URL');
error.code = 400;
error.details = ['Invalid URL'];
this.#errorHandler(error, req, res);
}

#errorHandler(error, req, res) {
Expand Down
51 changes: 12 additions & 39 deletions packages/output-geoservices/src/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ const FeatureServer = require('@koopjs/featureserver');

jest.mock('@koopjs/featureserver', () => ({
setLogger: jest.fn(),
route: jest.fn(),
setDefaults: jest.fn(),
restInfo: jest.fn(),
serverInfo: jest.fn(),
Expand Down Expand Up @@ -98,51 +97,25 @@ describe('Output Geoservices', () => {
{
path: '$namespace/rest/services/$providerParams/MapServer*',
methods: ['get', 'post'],
handler: 'generalHandler',
handler: 'invalidUrlHandler',
},
]);
});
});

describe('generalHandler', () => {
test('should pull data and route', async () => {
describe('error handling', () => {
test('should handle Invalid URL', async () => {
const output = new OutputGeoServices(modelMock, { logger: loggerMock });
await output.generalHandler({ foo: 'bar' }, resMock);
expect(FeatureServer.route.mock.calls.length).toBe(1);
expect(FeatureServer.route.mock.calls[0]).toEqual([{ foo: 'bar' }, resMock, 'someData']);
expect(modelMock.pull.mock.calls.length).toBe(1);
expect(modelMock.pull.mock.calls[0][0]).toEqual({ foo: 'bar' });
});

test('should authorize, then pull data and route', async () => {
const modelMock = {
pull: jest.fn(async () => 'someData'),
};
const output = new OutputGeoServices(modelMock, { logger: loggerMock });
await output.generalHandler({ foo: 'bar' }, resMock);
expect(FeatureServer.route.mock.calls.length).toBe(1);
expect(FeatureServer.route.mock.calls[0]).toEqual([{ foo: 'bar' }, resMock, 'someData']);
});

test('should handle 5xx error', async () => {
const modelMock = {
pull: jest.fn(async () => {
const error = new Error('Upstream error');
error.code = 503;
throw error;
}),
};
const output = new OutputGeoServices(modelMock, { logger: loggerMock });
await output.generalHandler(reqMock, resMock);
await output.invalidUrlHandler(reqMock, resMock);
expect(resMock.status.mock.calls[0].length).toBe(1);
expect(resMock.status.mock.calls[0]).toEqual([200]);
expect(resMock.json.mock.calls[0].length).toBe(1);
expect(resMock.json.mock.calls[0]).toEqual([
{
error: {
code: 503,
details: [],
message: 'Upstream error',
code: 400,
details: ['Invalid URL'],
message: 'Invalid URL',
},
},
]);
Expand All @@ -155,7 +128,7 @@ describe('Output Geoservices', () => {
}),
};
const output = new OutputGeoServices(modelMock, { logger: loggerMock });
await output.generalHandler(reqMock, resMock);
await output.queryHandler(reqMock, resMock);
expect(resMock.status.mock.calls[0].length).toBe(1);
expect(resMock.status.mock.calls[0]).toEqual([200]);
expect(resMock.json.mock.calls[0].length).toBe(1);
Expand All @@ -179,7 +152,7 @@ describe('Output Geoservices', () => {
}),
};
const output = new OutputGeoServices(modelMock, { logger: loggerMock });
await output.generalHandler(reqMock, resMock);
await output.queryHandler(reqMock, resMock);
expect(resMock.status.mock.calls[0].length).toBe(1);
expect(resMock.status.mock.calls[0]).toEqual([200]);
expect(resMock.json.mock.calls[0].length).toBe(1);
Expand All @@ -204,7 +177,7 @@ describe('Output Geoservices', () => {
}),
};
const output = new OutputGeoServices(modelMock, { logger: loggerMock });
await output.generalHandler({ headers: { authorization: '123' } }, resMock);
await output.queryHandler({ headers: { authorization: '123' } }, resMock);
expect(resMock.status.mock.calls[0].length).toBe(1);
expect(resMock.status.mock.calls[0]).toEqual([200]);
expect(resMock.json.mock.calls[0].length).toBe(1);
Expand All @@ -228,7 +201,7 @@ describe('Output Geoservices', () => {
}),
};
const output = new OutputGeoServices(modelMock, { logger: loggerMock });
await output.generalHandler({ headers: { authorization: '123' } }, resMock);
await output.queryHandler({ headers: { authorization: '123' } }, resMock);
expect(resMock.status.mock.calls[0].length).toBe(1);
expect(resMock.status.mock.calls[0]).toEqual([200]);
expect(resMock.json.mock.calls[0].length).toBe(1);
Expand All @@ -251,7 +224,7 @@ describe('Output Geoservices', () => {
}),
};
const output = new OutputGeoServices(modelMock, { logger: loggerMock });
await output.generalHandler(reqMock, resMock);
await output.queryHandler(reqMock, resMock);
expect(resMock.status.mock.calls[0].length).toBe(1);
expect(resMock.status.mock.calls[0]).toEqual([200]);
expect(resMock.json.mock.calls[0].length).toBe(1);
Expand Down

0 comments on commit 593910d

Please sign in to comment.