-
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.
- Loading branch information
Showing
3 changed files
with
98 additions
and
11 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
71 changes: 71 additions & 0 deletions
71
apps/client/src/pages/view/components/custom/CustomViewUtils.test.ts
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,71 @@ | ||
import { | ||
buildUrl, | ||
addTimeStampForRefresh, | ||
urlToString, | ||
} from './CustomViewUtils'; | ||
|
||
describe('CustomViewUtils', () => { | ||
describe('buildUrl', () => { | ||
it('should return a URL object with the provided value', () => { | ||
const value = 'https://example.com'; | ||
const url = buildUrl(value); | ||
expect(url instanceof URL).toBe(true); | ||
expect(url.toString()).toBe('https://example.com/'); | ||
}); | ||
|
||
it('should return a URL object with the provided value', () => { | ||
const value = '/web-engines/pactl_trucking/admin/#/crossBorderRecycle'; | ||
const url = buildUrl(value); | ||
expect(url instanceof URL).toBe(true); | ||
expect(url.toString()).toBe( | ||
'http://localhost/web-engines/pactl_trucking/admin/#/crossBorderRecycle', | ||
); | ||
}); | ||
}); | ||
|
||
describe('addTimeStampForRefresh', () => { | ||
it('should return a new URL object with a timestamp query parameter', () => { | ||
const value = new URL('https://example.com'); | ||
const refreshedUrl = addTimeStampForRefresh(value); | ||
expect(refreshedUrl instanceof URL).toBe(true); | ||
expect(refreshedUrl.href).toMatch( | ||
/https:\/\/example.com\/\?iframe-refresh=\d+/, | ||
); | ||
}); | ||
|
||
it('should return a new URL object with a timestamp query parameter', () => { | ||
const value = new URL( | ||
'https://example.com/skp-simple-landing?d=%2Fprojects', | ||
); | ||
const refreshedUrl = addTimeStampForRefresh(value); | ||
expect(refreshedUrl instanceof URL).toBe(true); | ||
expect(refreshedUrl.href).toMatch( | ||
/https:\/\/example.com\/skp-simple-landing\?d=%2Fprojects&iframe-refresh=\d+/, | ||
); | ||
}); | ||
|
||
it('should return a new URL object with a timestamp query parameter', () => { | ||
const value = new URL( | ||
'https://example.com/skp-simple-landing?d=%2Fprojects#test', | ||
); | ||
const refreshedUrl = addTimeStampForRefresh(value); | ||
expect(refreshedUrl instanceof URL).toBe(true); | ||
expect(refreshedUrl.href).toMatch( | ||
/https:\/\/example.com\/skp-simple-landing\?d=%2Fprojects&iframe-refresh=\d+#test/, | ||
); | ||
}); | ||
}); | ||
|
||
describe('urlToString', () => { | ||
it('should return the string representation of the provided URL', () => { | ||
const value = new URL( | ||
'https://example.com/skp-simple-landing?d=%2Fprojects#test', | ||
); | ||
const urlStr = urlToString(value); | ||
expect(typeof urlStr).toBe('string'); | ||
expect(urlStr).toBe( | ||
'https://example.com/skp-simple-landing?d=%2Fprojects#test', | ||
); | ||
}); | ||
}); | ||
}); |
20 changes: 20 additions & 0 deletions
20
apps/client/src/pages/view/components/custom/CustomViewUtils.ts
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,20 @@ | ||
export const buildUrl = (value: string): URL => { | ||
// TODO only allow same origin, same org name and contents from web-engines. | ||
// TODO bypass the rule if enable develop mode | ||
try { | ||
return new URL(value); | ||
} catch (error) { | ||
const current = new URL(window.location.href); | ||
const next = new URL(`${current.protocol}//${current.host}${value}`); | ||
return next; | ||
} | ||
}; | ||
|
||
export const addTimeStampForRefresh = (value: URL): URL => { | ||
value.searchParams.append('iframe-refresh', new Date().getTime().toString()); | ||
return value; | ||
}; | ||
|
||
export const urlToString = (value: URL): string => { | ||
return value.toString(); | ||
}; |