-
Notifications
You must be signed in to change notification settings - Fork 13
Noci support backend headers #436
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -49,6 +49,7 @@ | |
| "testdata", | ||
| "Bytespider", | ||
| "Timespans", | ||
| "googlequicksearchbox" | ||
| "googlequicksearchbox", | ||
| "cnstrc" | ||
| ] | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -849,6 +849,131 @@ describe('ConstructorIO - Utils - Request Queue', function utilsRequestQueue() { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, waitInterval); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| describe('headers', () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| describe('securityToken', () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| it('Should send x-cnstrc-token header on POST requests when securityToken is provided', (done) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const requests = new RequestQueue({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ...requestQueueOptions, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| securityToken: 'test-security-token', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| store.session.set(humanityStorageKey, true); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| requests.queue('https://ac.cnstrc.com/behavior?action=session_start', 'POST', { action: 'session_start' }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| requests.send(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| setTimeout(() => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const requestHeaders = helpers.extractHeadersFromFetch(fetchSpy); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(requestHeaders).to.have.property('x-cnstrc-token').to.equal('test-security-token'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| done(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, waitInterval); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| it('Should send x-cnstrc-token header on GET requests when securityToken is provided', (done) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const requests = new RequestQueue({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ...requestQueueOptions, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| securityToken: 'test-security-token', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| store.session.set(humanityStorageKey, true); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| requests.queue('https://ac.cnstrc.com/behavior?action=session_start'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| requests.send(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| setTimeout(() => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const requestHeaders = helpers.extractHeadersFromFetch(fetchSpy); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(requestHeaders).to.have.property('x-cnstrc-token').to.equal('test-security-token'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| done(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, waitInterval); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| it('Should not send x-cnstrc-token header when securityToken is not provided', (done) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const requests = new RequestQueue(requestQueueOptions); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| store.session.set(humanityStorageKey, true); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| requests.queue('https://ac.cnstrc.com/behavior?action=session_start', 'POST', { action: 'session_start' }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| requests.send(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| setTimeout(() => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const requestHeaders = helpers.extractHeadersFromFetch(fetchSpy); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(requestHeaders).to.not.have.property('x-cnstrc-token'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| done(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, waitInterval); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| describe('userIp', () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| it('Should send X-Forwarded-For header when userIp is provided', (done) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const requests = new RequestQueue({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ...requestQueueOptions, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| userIp: '127.0.0.1', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| store.session.set(humanityStorageKey, true); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| requests.queue('https://ac.cnstrc.com/behavior?action=session_start', 'POST', { action: 'session_start' }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| requests.send(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| setTimeout(() => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const requestHeaders = helpers.extractHeadersFromFetch(fetchSpy); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(requestHeaders).to.have.property('X-Forwarded-For').to.equal('127.0.0.1'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| done(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, waitInterval); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| it('Should not send X-Forwarded-For header when userIp is not provided', (done) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const requests = new RequestQueue(requestQueueOptions); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| store.session.set(humanityStorageKey, true); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| requests.queue('https://ac.cnstrc.com/behavior?action=session_start', 'POST', { action: 'session_start' }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| requests.send(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| setTimeout(() => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const requestHeaders = helpers.extractHeadersFromFetch(fetchSpy); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(requestHeaders).to.not.have.property('X-Forwarded-For'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| done(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, waitInterval); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| describe('userAgent', () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| it('Should send User-Agent header when userAgent is provided', (done) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const requests = new RequestQueue({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ...requestQueueOptions, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| userAgent: 'custom-agent/1.0', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| store.session.set(humanityStorageKey, true); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| requests.queue('https://ac.cnstrc.com/behavior?action=session_start', 'POST', { action: 'session_start' }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| requests.send(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| setTimeout(() => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const requestHeaders = helpers.extractHeadersFromFetch(fetchSpy); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(requestHeaders).to.have.property('User-Agent').to.equal('custom-agent/1.0'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| done(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, waitInterval); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| it('Should not send User-Agent header when userAgent is not provided', (done) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const requests = new RequestQueue(requestQueueOptions); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| store.session.set(humanityStorageKey, true); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| requests.queue('https://ac.cnstrc.com/behavior?action=session_start', 'POST', { action: 'session_start' }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| requests.send(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| setTimeout(() => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const requestHeaders = helpers.extractHeadersFromFetch(fetchSpy); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(requestHeaders).to.not.have.property('User-Agent'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| done(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, waitInterval); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | |
| }); | |
| it('Should send User-Agent header for GET when userAgent is provided', (done) => { | |
| const requests = new RequestQueue({ | |
| ...requestQueueOptions, | |
| userAgent: 'custom-agent/1.0', | |
| }); | |
| store.session.set(humanityStorageKey, true); | |
| requests.queue('https://ac.cnstrc.com/behavior?action=session_start', 'GET'); | |
| requests.send(); | |
| setTimeout(() => { | |
| const requestHeaders = helpers.extractHeadersFromFetch(fetchSpy); | |
| expect(requestHeaders).to.have.property('User-Agent').to.equal('custom-agent/1.0'); | |
| done(); | |
| }, waitInterval); | |
| }); | |
| it('Should not send User-Agent header for GET when userAgent is not provided', (done) => { | |
| const requests = new RequestQueue(requestQueueOptions); | |
| store.session.set(humanityStorageKey, true); | |
| requests.queue('https://ac.cnstrc.com/behavior?action=session_start', 'GET'); | |
| requests.send(); | |
| setTimeout(() => { | |
| const requestHeaders = helpers.extractHeadersFromFetch(fetchSpy); | |
| expect(requestHeaders).to.not.have.property('User-Agent'); | |
| done(); | |
| }, waitInterval); | |
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -91,6 +91,9 @@ class ConstructorIO { | |
| beaconMode, | ||
| networkParameters, | ||
| humanityCheckLocation, | ||
| securityToken, | ||
| userIp, | ||
| userAgent, | ||
| } = options; | ||
|
Comment on lines
91
to
97
|
||
|
|
||
| if (!apiKey || typeof apiKey !== 'string') { | ||
|
|
@@ -139,6 +142,9 @@ class ConstructorIO { | |
| beaconMode: (beaconMode === false) ? false : true, // Defaults to 'true', | ||
| networkParameters: networkParameters || {}, | ||
| humanityCheckLocation: humanityCheckLocation || 'session', | ||
| securityToken: securityToken || '', | ||
| userIp: userIp || '', | ||
| userAgent: userAgent || '', | ||
|
Comment on lines
144
to
+147
|
||
| }; | ||
|
|
||
| // Expose global modules | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -112,16 +112,19 @@ class RequestQueue { | |||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| const headers = this.buildHeaders(); | ||||||||
|
|
||||||||
| if (nextInQueue.method === 'GET') { | ||||||||
| request = fetch(nextInQueue.url, { signal }); | ||||||||
| request = fetch(nextInQueue.url, { headers, signal }); | ||||||||
| } | ||||||||
|
|
||||||||
| if (nextInQueue.method === 'POST') { | ||||||||
| headers['Content-Type'] = 'text/plain'; | ||||||||
| request = fetch(nextInQueue.url, { | ||||||||
| method: nextInQueue.method, | ||||||||
| body: JSON.stringify(nextInQueue.body), | ||||||||
| mode: 'cors', | ||||||||
| headers: { 'Content-Type': 'text/plain' }, | ||||||||
| headers, | ||||||||
| signal, | ||||||||
| }); | ||||||||
| } | ||||||||
|
|
@@ -186,6 +189,21 @@ class RequestQueue { | |||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| // Build request headers from options | ||||||||
| buildHeaders() { | ||||||||
| const headers = {}; | ||||||||
| if (this.options.securityToken && typeof this.options.securityToken === 'string') { | ||||||||
| headers['x-cnstrc-token'] = this.options.securityToken; | ||||||||
| } | ||||||||
| if (this.options.userIp && typeof this.options.userIp === 'string') { | ||||||||
| headers['X-Forwarded-For'] = this.options.userIp; | ||||||||
| } | ||||||||
| if (this.options.userAgent && typeof this.options.userAgent === 'string') { | ||||||||
|
||||||||
| if (this.options.userAgent && typeof this.options.userAgent === 'string') { | |
| // Only set User-Agent in DOM-less environments (e.g., server-side/custom fetch) | |
| if (!helpers.canUseDOM() && this.options.userAgent && typeof this.options.userAgent === 'string') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Header support is implemented for both GET and POST requests, but the tests for
userIpcurrently only cover POST. Add a GET-coverage test to ensureX-Forwarded-Foris included whenuserIpis provided on GET requests as well (and absent when not provided).