Skip to content

Commit 4dfcaa8

Browse files
committed
#RI-5448 - fix resource url for docker
1 parent 3fdc300 commit 4dfcaa8

File tree

2 files changed

+69
-3
lines changed

2 files changed

+69
-3
lines changed

redisinsight/ui/src/services/resourcesService.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,13 @@ export const getOriginUrl = () => (IS_ABSOLUTE_PATH.test(RESOURCES_BASE_URL)
1616
? RESOURCES_BASE_URL
1717
: (window?.location?.origin || RESOURCES_BASE_URL))
1818

19-
export const getPathToResource = (url: string = ''): string => (IS_ABSOLUTE_PATH.test(url)
20-
? url
21-
: new URL(url, resourcesService.defaults.baseURL).toString())
19+
export const getPathToResource = (url: string = ''): string => {
20+
try {
21+
return IS_ABSOLUTE_PATH.test(url) ? url : new URL(url, getOriginUrl()).toString()
22+
} catch {
23+
return ''
24+
}
25+
}
2226

2327
export const checkResourse = async (url: string = '') => resourcesService.head(url)
2428

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
eslint-disable global-require
3+
*/
4+
5+
Object.defineProperty(window, 'location', {
6+
value: {
7+
origin: 'http://localhost'
8+
},
9+
writable: true
10+
})
11+
12+
describe('getOriginUrl', () => {
13+
const OLD_ENV = process.env
14+
15+
beforeEach(() => {
16+
jest.resetModules()
17+
process.env = { ...OLD_ENV }
18+
})
19+
afterAll(() => {
20+
process.env = OLD_ENV
21+
})
22+
23+
test('shoud return url with absolute path', () => {
24+
const { getOriginUrl } = require('../resourcesService')
25+
26+
expect(getOriginUrl()).toEqual('http://localhost:5001/')
27+
})
28+
29+
test('shoud return origin with not absolute path', () => {
30+
process.env.RI_APP_TYPE = 'web'
31+
process.env.NODE_ENV = 'production'
32+
33+
const { getOriginUrl } = require('../resourcesService')
34+
expect(getOriginUrl()).toEqual('http://localhost')
35+
})
36+
})
37+
38+
describe('getPathToResource', () => {
39+
const OLD_ENV = process.env
40+
41+
beforeEach(() => {
42+
jest.resetModules()
43+
process.env = { ...OLD_ENV }
44+
})
45+
afterAll(() => {
46+
process.env = OLD_ENV
47+
})
48+
49+
test('shoud return url with absolute path', () => {
50+
const { getPathToResource } = require('../resourcesService')
51+
52+
expect(getPathToResource('data/file.txt')).toEqual('http://localhost:5001/data/file.txt')
53+
})
54+
55+
test('shoud return origin with not absolute path', () => {
56+
process.env.RI_APP_TYPE = 'web'
57+
process.env.NODE_ENV = 'production'
58+
59+
const { getPathToResource } = require('../resourcesService')
60+
expect(getPathToResource('data/file.txt')).toEqual('http://localhost/data/file.txt')
61+
})
62+
})

0 commit comments

Comments
 (0)