Skip to content

Commit 7d79169

Browse files
committed
- add validation for custom domain
1 parent 274b245 commit 7d79169

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

src/index.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,17 @@ const parseInitConfig = (...args) => {
5656
}
5757
}
5858

59+
const validateConfig = config => {
60+
if (config.domain) {
61+
if (!config.domain.startsWith('https://') && !config.domain.startsWith('http://')) {
62+
throw new Error(
63+
'When initialize the SDK with a custom domain it should start with http:// or https://, ' +
64+
'for example: Backendless.initApp(\'https://foobar.com\')'
65+
)
66+
}
67+
}
68+
}
69+
5970
const SERVICES = {
6071
'Logging' : () => require('./logging').default,
6172
'Counters' : () => require('./counters').default,
@@ -113,6 +124,8 @@ class Backendless {
113124
initApp() {
114125
const config = parseInitConfig(...arguments)
115126

127+
validateConfig(config)
128+
116129
const app = config.standalone
117130
? new Backendless(this)
118131
: this

test/unit/specs/namespace.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,22 @@ describe('Namespace', function() {
102102
expect(Backendless.appPath).to.be.equal(`${CUSTOM_DOMAIN}/my-api-uri`)
103103
expect(Backendless.apiURI).to.be.equal('/my-api-uri')
104104
})
105+
106+
it('should fails with custom domain which does not start on https or http', () => {
107+
const errorMsg = (
108+
'When initialize the SDK with a custom domain it should start with http:// or https://, ' +
109+
'for example: Backendless.initApp(\'https://foobar.com\')'
110+
)
111+
112+
expect(() => Backendless.initApp('foobar.com')).to.throw(errorMsg)
113+
expect(() => Backendless.initApp('httpsfoobar.com')).to.throw(errorMsg)
114+
expect(() => Backendless.initApp('https:foobar.com')).to.throw(errorMsg)
115+
expect(() => Backendless.initApp('https:/foobar.com')).to.throw(errorMsg)
116+
expect(() => Backendless.initApp('httpfoobar.com')).to.throw(errorMsg)
117+
expect(() => Backendless.initApp('http:foobar.com')).to.throw(errorMsg)
118+
expect(() => Backendless.initApp('http:/foobar.com')).to.throw(errorMsg)
119+
expect(() => Backendless.initApp('file://foobar.com')).to.throw(errorMsg)
120+
})
105121
})
106122

107123
describe('Debug Mode', () => {

0 commit comments

Comments
 (0)