Skip to content

Commit

Permalink
Add basic unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
miltonhultgren committed May 3, 2024
1 parent d68ed31 commit d1a8d60
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 1 deletion.
85 changes: 85 additions & 0 deletions packages/core/http/core-http-server/src/router/response.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { isKibanaResponse } from './response';

describe('isKibanaResponse', () => {
it('expects the status to be a number', () => {
expect(
isKibanaResponse({
status: 200,
options: {},
})
).toEqual(true);

expect(
isKibanaResponse({
status: '200',
options: {},
})
).toEqual(false);
});

it('expects the options to be an object', () => {
expect(
isKibanaResponse({
status: 200,
options: {},
})
).toEqual(true);

expect(
isKibanaResponse({
status: 200,
options: [],
})
).toEqual(false);
expect(
isKibanaResponse({
status: 200,
options: null,
})
).toEqual(false);
expect(
isKibanaResponse({
status: 200,
options: 'a string',
})
).toEqual(false);
expect(
isKibanaResponse({
status: 200,
options: new Set(),
})
).toEqual(false);
expect(
isKibanaResponse({
status: 200,
options: () => {},
})
).toEqual(false);
});

it('allows a payload but no other properties', () => {
expect(
isKibanaResponse({
status: 200,
options: {},
payload: 'My stuff',
})
).toEqual(true);

expect(
isKibanaResponse({
status: 200,
options: {},
data: 'Not allowed',
})
).toEqual(false);
});
});
14 changes: 13 additions & 1 deletion packages/core/http/core-http-server/src/router/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,19 @@ export interface IKibanaResponse<T extends HttpResponsePayload | ResponseError =
}

export function isKibanaResponse(response: Record<string, any>): response is IKibanaResponse {
return typeof response.status === 'number' && typeof response.options === 'object';
const { status, options, payload, ...rest } = response;

if (Object.keys(rest).length !== 0) {
return false;
}

return (
typeof status === 'number' &&
typeof options === 'object' &&
!Array.isArray(options) &&
options !== null &&
!(options instanceof Set)
);
}

/**
Expand Down

0 comments on commit d1a8d60

Please sign in to comment.