-
Notifications
You must be signed in to change notification settings - Fork 1
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 #25 from onaio/override-default-csp
- Loading branch information
Showing
3 changed files
with
53 additions
and
1 deletion.
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
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,15 @@ | ||
/** | ||
* get the url origin from full url | ||
* e.g https://keycloak-example.domain.org from https://keycloak-example.domain.org/auth/realms/example-realm | ||
* @param url | ||
* @returns | ||
*/ | ||
export const getOriginFromUrl = (url?: string) => { | ||
if (!url) { | ||
return []; | ||
} | ||
const urlObject = new URL(url); | ||
return [urlObject.origin]; | ||
}; | ||
|
||
export default getOriginFromUrl; |
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,16 @@ | ||
import { getOriginFromUrl } from '../index'; | ||
|
||
describe('test util functions', () => { | ||
it('get origin from url', () => { | ||
const url = 'https://keycloak-example.domain.org/auth/realms/example-realm'; | ||
const result = getOriginFromUrl(url); | ||
expect(result).toEqual(['https://keycloak-example.domain.org']); | ||
}); | ||
it('returns empty array if url is empty or undefined', () => { | ||
const emptyUrl = ''; | ||
const emptyUrlResult = getOriginFromUrl(emptyUrl); | ||
const undefinedUrlResult = getOriginFromUrl(); | ||
expect(emptyUrlResult).toEqual([]); | ||
expect(undefinedUrlResult).toEqual([]); | ||
}); | ||
}); |