Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CSL-3108] Trim space adjustments #183

Merged
merged 7 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
22 changes: 22 additions & 0 deletions spec/src/modules/autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,22 @@ describe('ConstructorIO - Autocomplete', () => {
});
});

it('Should not trim non-breaking spaces from query', (done) => {
const queryWithSpaces = ` ${query} `;
const { autocomplete } = new ConstructorIO({
apiKey: testApiKey,
fetch: fetchSpy,
});

autocomplete.getAutocompleteResults(queryWithSpaces).then((res) => {
expect(res.request.term).to.equal(queryWithSpaces);
expect(res).to.have.property('request').to.be.an('object');
expect(res).to.have.property('sections').to.be.an('object');
expect(res).to.have.property('result_id').to.be.an('string');
done();
});
});

it('Should return a response with a / query', (done) => {
const { autocomplete } = new ConstructorIO({
apiKey: testApiKey,
Expand Down Expand Up @@ -593,6 +609,12 @@ describe('ConstructorIO - Autocomplete', () => {
return expect(autocomplete.getAutocompleteResults(null)).to.eventually.be.rejected;
});

it('Should be rejected when query consisting of only non-breaking spaces is provided', () => {
const { autocomplete } = new ConstructorIO(validOptions);

return expect(autocomplete.getAutocompleteResults(' ')).to.eventually.be.rejected;
});

it('Should be rejected when invalid numResults parameter is provided', () => {
const { autocomplete } = new ConstructorIO(validOptions);

Expand Down
23 changes: 23 additions & 0 deletions spec/src/modules/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const sinon = require('sinon');
const sinonChai = require('sinon-chai');
const ConstructorIO = require('../../../test/constructorio'); // eslint-disable-line import/extensions
const helpers = require('../../mocha.helpers');
const utilsHelpers = require('../../../src/utils/helpers');
Copy link
Contributor

Choose a reason for hiding this comment

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

Feel free to ignore this, but should we just import the specific function here so we don't have helpers and utilsHelpers? Haha

i.e. const { trimNonBreakingSpaces } = require('../../../src/utils/helpers');

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Great callout - this is going to go away anyway. We should not be trimming on search either


const nodeFetch = (...args) => import('node-fetch').then(({ default: fetch }) => fetch(...args));

Expand Down Expand Up @@ -563,6 +564,22 @@ describe('ConstructorIO - Search', () => {
});
});

it('Should trim non-breaking spaces from query', (done) => {
const queryWithSpaces = ` ${query} `;
const { search } = new ConstructorIO({
apiKey: testApiKey,
fetch: fetchSpy,
});

search.getSearchResults(queryWithSpaces).then((res) => {
expect(res.request.term).to.equal(utilsHelpers.trimNonBreakingSpaces(queryWithSpaces));
expect(res).to.have.property('request').to.be.an('object');
expect(res).to.have.property('response').to.be.an('object');
expect(res).to.have.property('result_id').to.be.an('string');
done();
});
});

it('Should pass the correct custom headers passed in function networkParameters', (done) => {
const { search } = new ConstructorIO({
...validOptions,
Expand Down Expand Up @@ -664,6 +681,12 @@ describe('ConstructorIO - Search', () => {
return expect(search.getSearchResults(null, { section })).to.eventually.be.rejected;
});

it('Should be rejected when query consisting of only non-breaking spaces is provided', () => {
const { search } = new ConstructorIO(validOptions);

return expect(search.getSearchResults(' ', { section })).to.eventually.be.rejected;
});

it('Should be rejected when invalid page parameter is provided', () => {
const { search } = new ConstructorIO(validOptions);
const searchParams = {
Expand Down
10 changes: 7 additions & 3 deletions src/modules/autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@ function createAutocompleteUrl(query, parameters, userParameters, options) {
queryParams.i = clientId;
queryParams.s = sessionId;

// Validate query (term) is provided
if (!query || typeof query !== 'string') {
// Trim non breaking spaces from query
const queryTrimmed = helpers.trimNonBreakingSpaces(query);

// Validate query (term) is provided and consists of more than non-breaking spaces
if (!queryTrimmed || typeof queryTrimmed !== 'string') {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was added to ensure we don't dispatch requests consisting of solely non-breaking spaces (ex: )

throw new Error('query is a required parameter of type string');
}

Expand Down Expand Up @@ -112,7 +115,8 @@ function createAutocompleteUrl(query, parameters, userParameters, options) {
const queryString = qs.stringify(queryParams, { indices: false });
const cleanedQuery = query.replace(/^\//, '|'); // For compatibility with backend API

return `${serviceUrl}/autocomplete/${helpers.encodeURIComponentRFC3986(helpers.trimNonBreakingSpaces(cleanedQuery))}?${queryString}`;
// Note: it is intentional that query is dispatched without being trimmed (`queryTrimmed`)
return `${serviceUrl}/autocomplete/${helpers.encodeURIComponentRFC3986(cleanedQuery)}?${queryString}`;
}

/**
Expand Down
9 changes: 6 additions & 3 deletions src/modules/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ function createSearchUrl(query, parameters, userParameters, options, isVoiceSear
queryParams.i = clientId;
queryParams.s = sessionId;

// Validate query (term) is provided
if (!query || typeof query !== 'string') {
// Trim non breaking spaces from query
const queryTrimmed = helpers.trimNonBreakingSpaces(query);

// Validate query (term) is provided and consists of more than non-breaking spaces
if (!queryTrimmed || typeof queryTrimmed !== 'string') {
Copy link
Contributor Author

@sblaurock sblaurock Dec 19, 2023

Choose a reason for hiding this comment

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

This was added to ensure we don't dispatch requests consisting of solely non-breaking spaces (ex: )

throw new Error('query is a required parameter of type string');
}

Expand Down Expand Up @@ -145,7 +148,7 @@ function createSearchUrl(query, parameters, userParameters, options, isVoiceSear

const searchUrl = isVoiceSearch ? 'search/natural_language' : 'search';

return `${serviceUrl}/${searchUrl}/${helpers.encodeURIComponentRFC3986(helpers.trimNonBreakingSpaces(query))}?${queryString}`;
return `${serviceUrl}/${searchUrl}/${helpers.encodeURIComponentRFC3986(queryTrimmed)}?${queryString}`;
}

/**
Expand Down
Loading