forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request elastic#8097 from ycombinator/fwd-port-8043
Adding system API module
- Loading branch information
Showing
6 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
src/core_plugins/kibana/server/lib/__tests__/system_api.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import expect from 'expect.js'; | ||
import { isSystemApiRequest } from '../system_api'; | ||
|
||
describe('system_api', () => { | ||
describe('#isSystemApiRequest', () => { | ||
it ('returns true for a system API HTTP request', () => { | ||
const mockHapiRequest = { | ||
headers: { | ||
'kbn-system-api': true | ||
} | ||
}; | ||
expect(isSystemApiRequest(mockHapiRequest)).to.be(true); | ||
}); | ||
|
||
it ('returns false for a non-system API HTTP request', () => { | ||
const mockHapiRequest = { | ||
headers: {} | ||
}; | ||
expect(isSystemApiRequest(mockHapiRequest)).to.be(false); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
const SYSTEM_API_HEADER_NAME = 'kbn-system-api'; | ||
|
||
/** | ||
* Checks on the *server-side*, if an HTTP request is a system API request | ||
* | ||
* @param request HAPI request object | ||
* @return true if request is a system API request; false, otherwise | ||
*/ | ||
export function isSystemApiRequest(request) { | ||
return !!request.headers[SYSTEM_API_HEADER_NAME]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import expect from 'expect.js'; | ||
import ngMock from 'ng_mock'; | ||
import { addSystemApiHeader, isSystemApiRequest } from '../system_api'; | ||
|
||
describe('system_api', () => { | ||
describe('#addSystemApiHeader', () => { | ||
it ('adds the correct system API header', () => { | ||
const headers = { | ||
'kbn-version': '4.6.0' | ||
}; | ||
const newHeaders = addSystemApiHeader(headers); | ||
|
||
expect(newHeaders).to.have.property('kbn-system-api'); | ||
expect(newHeaders['kbn-system-api']).to.be(true); | ||
|
||
expect(newHeaders).to.have.property('kbn-version'); | ||
expect(newHeaders['kbn-version']).to.be('4.6.0'); | ||
}); | ||
}); | ||
|
||
describe('#isSystemApiRequest', () => { | ||
it ('returns true for a system API HTTP request', () => { | ||
const mockRequest = { | ||
headers: { | ||
'kbn-system-api': true | ||
} | ||
}; | ||
expect(isSystemApiRequest(mockRequest)).to.be(true); | ||
}); | ||
|
||
it ('returns false for a non-system API HTTP request', () => { | ||
const mockRequest = { | ||
headers: {} | ||
}; | ||
expect(isSystemApiRequest(mockRequest)).to.be(false); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
const SYSTEM_API_HEADER_NAME = 'kbn-system-api'; | ||
|
||
/** | ||
* Adds a custom header designating request as system API | ||
* @param originalHeaders Object representing set of headers | ||
* @return Object representing set of headers, with system API header added in | ||
*/ | ||
export function addSystemApiHeader(originalHeaders) { | ||
const systemApiHeaders = { | ||
[SYSTEM_API_HEADER_NAME]: true | ||
}; | ||
return { | ||
...originalHeaders, | ||
...systemApiHeaders | ||
}; | ||
} | ||
|
||
/** | ||
* Returns true if request is a system API request; false otherwise | ||
* | ||
* @param request Object Request object created by $http service | ||
* @return true if request is a system API request; false otherwise | ||
*/ | ||
export function isSystemApiRequest(request) { | ||
return !!request.headers[SYSTEM_API_HEADER_NAME]; | ||
} |