Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions spec/src/constructorio.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,22 @@ describe(`ConstructorIO${bundledDescriptionSuffix}`, () => {
expect(instance.options).to.have.property('serviceUrl').to.equal('https://constructor.io');
});

it('Should return an instance with correct serviceUrl when a http serviceUrl is passed and allowHttpServiceUrl is true', () => {
const clientId = 'client-id';
const sessionId = 'session-id';
const serviceUrl = 'http://constructor.io';
const instance = new ConstructorIO({
apiKey: validApiKey,
clientId,
sessionId,
serviceUrl,
allowHttpServiceUrl: true,
});

expect(instance).to.be.an('object');
expect(instance.options).to.have.property('serviceUrl').to.equal('http://constructor.io');
});

it('Should remove any trailing slashes from the serviceUrl', () => {
const serviceUrl = 'https://constructor.io/';
const instance = new ConstructorIO({
Expand Down
73 changes: 57 additions & 16 deletions spec/src/utils/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -584,30 +584,71 @@ describe('ConstructorIO - Utils - Helpers', () => {
});

describe('addHTTPSToString', () => {
it('Should return the url without any modification', () => {
const testUrl = 'https://www.constructor.io';
describe('when allowHttp is not given', () => {
it('Should return the url without any modification', () => {
const testUrl = 'https://www.constructor.io';

expect(addHTTPSToString(testUrl)).to.equal(testUrl);
});
expect(addHTTPSToString(testUrl)).to.equal(testUrl);
});

it('Should return url with no protocol with https at the beginning', () => {
const testUrl = 'www.constructor.io';
const expectedUrl = 'https://www.constructor.io';
it('Should return url with no protocol with https at the beginning', () => {
const testUrl = 'www.constructor.io';
const expectedUrl = 'https://www.constructor.io';

expect(addHTTPSToString(testUrl)).to.equal(expectedUrl);
});
expect(addHTTPSToString(testUrl)).to.equal(expectedUrl);
});

it('Should return url with an http protocol with https at the beginning', () => {
const testUrl = 'http://www.constructor.io';
const expectedUrl = 'https://www.constructor.io';

expect(addHTTPSToString(testUrl)).to.equal(expectedUrl);
});

it('Should not replace "http" in the middle of the url', () => {
const testUrl = 'http://www.constructor.io/http/';
const expectedUrl = 'https://www.constructor.io/http/';

expect(addHTTPSToString(testUrl)).to.equal(expectedUrl);
});

it('Should return url with an http protocol with https at the beginning', () => {
const testUrl = 'http://www.constructor.io';
const expectedUrl = 'https://www.constructor.io';
it('Should return null if param is not a string', () => {
const testUrl = {};

expect(addHTTPSToString(testUrl)).to.equal(expectedUrl);
expect(addHTTPSToString(testUrl)).to.equal(null);
});
});
describe('when allowHttp is true', () => {
it('Should return the url without any modification', () => {
const testUrl = 'https://www.constructor.io';

it('Should return null if param is not a string', () => {
const testUrl = {};
expect(addHTTPSToString(testUrl, true)).to.equal(testUrl);
});

expect(addHTTPSToString(testUrl)).to.equal(null);
it('Should return url with no protocol with https at the beginning', () => {
const testUrl = 'www.constructor.io';
const expectedUrl = 'https://www.constructor.io';

expect(addHTTPSToString(testUrl, true)).to.equal(expectedUrl);
});

it('Should return url with an http protocol without any modification', () => {
const testUrl = 'http://localhost:8080';

expect(addHTTPSToString(testUrl, true)).to.equal(testUrl);
});

it('Should not replace "http" in the middle of the url', () => {
const testUrl = 'http://www.constructor.io/http/';

expect(addHTTPSToString(testUrl, true)).to.equal(testUrl);
});

it('Should return null if param is not a string', () => {
const testUrl = {};

expect(addHTTPSToString(testUrl, true)).to.equal(null);
});
});
});

Expand Down
4 changes: 3 additions & 1 deletion src/constructorio.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class ConstructorIO {
* @param {object} parameters - Parameters for client instantiation
* @param {string} parameters.apiKey - Constructor.io API key
* @param {string} [parameters.serviceUrl='https://ac.cnstrc.com'] - API URL endpoint
* @param {boolean} [parameters.allowHttpServiceUrl=false] - Allow HTTP protocol in API URL endpoint
* @param {string} [parameters.quizzesServiceUrl='https://quizzes.cnstrc.com'] - Quizzes API URL endpoint
* @param {string} [parameters.agentServiceUrl='https://agent.cnstrc.com'] - AI Shopping Agent API URL endpoint
* @param {string} [parameters.mediaServiceUrl='https://media-cnstrc.com'] - Media API URL endpoint
Expand Down Expand Up @@ -73,6 +74,7 @@ class ConstructorIO {
apiKey,
version: versionFromOptions,
serviceUrl,
allowHttpServiceUrl = false,
quizzesServiceUrl,
agentServiceUrl,
assistantServiceUrl,
Expand Down Expand Up @@ -121,7 +123,7 @@ class ConstructorIO {
this.options = {
apiKey,
version: versionFromOptions || versionFromGlobal || computePackageVersion(),
serviceUrl: helpers.addHTTPSToString(normalizedServiceUrl) || 'https://ac.cnstrc.com',
serviceUrl: helpers.addHTTPSToString(normalizedServiceUrl, allowHttpServiceUrl) || 'https://ac.cnstrc.com',
quizzesServiceUrl: (quizzesServiceUrl && quizzesServiceUrl.replace(/\/$/, '')) || 'https://quizzes.cnstrc.com',
agentServiceUrl: (agentServiceUrl && agentServiceUrl.replace(/\/$/, '')) || 'https://agent.cnstrc.com',
assistantServiceUrl: (assistantServiceUrl && assistantServiceUrl.replace(/\/$/, '')) || 'https://assistant.cnstrc.com',
Expand Down
1 change: 1 addition & 0 deletions src/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export interface ConstructorClientOptions {
apiKey: string;
version?: string;
serviceUrl?: string;
allowHttpServiceUrl?: boolean;
quizzesServiceUrl?: string;
agentServiceUrl?: string;
mediaServiceUrl?: string;
Expand Down
5 changes: 4 additions & 1 deletion src/utils/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ const utils = {

return utils.throwHttpErrorFromResponse(new Error(), response);
},
addHTTPSToString(url) {
addHTTPSToString(url, allowHttp = false) {
if (typeof url !== 'string') {
return null;
}
Expand All @@ -359,6 +359,9 @@ const utils = {
const doesUrlStartWithHTTP = url.startsWith('http://');

if (!doesUrlIncludeHTTPS && doesUrlStartWithHTTP) {
if (allowHttp) {
return url;
}
return url.replace('http', 'https');

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not possible, since in this branch the http:// prefix is confirmed, so didn't change the code unnecessarily. I added tests though to verify this remains the case.

}

Expand Down
Loading