-
Notifications
You must be signed in to change notification settings - Fork 83
[FSSDK-10643] make event processor injectable #948
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
Merged
Merged
Changes from 4 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { vi, expect, it, describe, afterAll } from 'vitest'; | ||
raju-opti marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
vi.mock('./default_dispatcher', () => { | ||
const DefaultEventDispatcher = vi.fn(); | ||
return { DefaultEventDispatcher }; | ||
}); | ||
|
||
vi.mock('../utils/http_request_handler/browser_request_handler', () => { | ||
const BrowserRequestHandler = vi.fn(); | ||
return { BrowserRequestHandler }; | ||
}); | ||
|
||
import { DefaultEventDispatcher } from './default_dispatcher'; | ||
import { BrowserRequestHandler } from '../utils/http_request_handler/browser_request_handler'; | ||
import eventDispatcher from './default_dispatcher.browser'; | ||
|
||
describe('eventDispatcher', () => { | ||
afterAll(() => { | ||
MockDefaultEventDispatcher.mockReset(); | ||
MockBrowserRequestHandler.mockReset(); | ||
}); | ||
const MockBrowserRequestHandler = vi.mocked(BrowserRequestHandler); | ||
const MockDefaultEventDispatcher = vi.mocked(DefaultEventDispatcher); | ||
|
||
it('creates and returns the instance by calling DefaultEventDispatcher', () => { | ||
expect(Object.is(eventDispatcher, MockDefaultEventDispatcher.mock.instances[0])).toBe(true); | ||
}); | ||
|
||
it('uses a BrowserRequestHandler', () => { | ||
expect(Object.is(eventDispatcher, MockDefaultEventDispatcher.mock.instances[0])).toBe(true); | ||
expect(Object.is(MockDefaultEventDispatcher.mock.calls[0][0], MockBrowserRequestHandler.mock.instances[0])).toBe(true); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/** | ||
* Copyright 2016-2017, 2020-2022, Optimizely | ||
raju-opti marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { BrowserRequestHandler } from "../utils/http_request_handler/browser_request_handler"; | ||
import { EventDispatcher } from '../event_processor'; | ||
import { DefaultEventDispatcher } from './default_dispatcher'; | ||
|
||
const eventDispatcher: EventDispatcher = new DefaultEventDispatcher(new BrowserRequestHandler()); | ||
|
||
export default eventDispatcher; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { vi, expect, it, describe, afterAll } from 'vitest'; | ||
|
||
vi.mock('./default_dispatcher', () => { | ||
const DefaultEventDispatcher = vi.fn(); | ||
return { DefaultEventDispatcher }; | ||
}); | ||
|
||
vi.mock('../utils/http_request_handler/node_request_handler', () => { | ||
const NodeRequestHandler = vi.fn(); | ||
return { NodeRequestHandler }; | ||
}); | ||
|
||
import { DefaultEventDispatcher } from './default_dispatcher'; | ||
import { NodeRequestHandler } from '../utils/http_request_handler/node_request_handler'; | ||
import eventDispatcher from './default_dispatcher.node'; | ||
|
||
describe('eventDispatcher', () => { | ||
const MockNodeRequestHandler = vi.mocked(NodeRequestHandler); | ||
const MockDefaultEventDispatcher = vi.mocked(DefaultEventDispatcher); | ||
|
||
afterAll(() => { | ||
MockDefaultEventDispatcher.mockReset(); | ||
MockNodeRequestHandler.mockReset(); | ||
}) | ||
|
||
it('creates and returns the instance by calling DefaultEventDispatcher', () => { | ||
expect(Object.is(eventDispatcher, MockDefaultEventDispatcher.mock.instances[0])).toBe(true); | ||
}); | ||
|
||
it('uses a NodeRequestHandler', () => { | ||
expect(Object.is(eventDispatcher, MockDefaultEventDispatcher.mock.instances[0])).toBe(true); | ||
expect(Object.is(MockDefaultEventDispatcher.mock.calls[0][0], MockNodeRequestHandler.mock.instances[0])).toBe(true); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* Copyright 2016-2018, 2020-2021, 2024 Optimizely | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
import { EventDispatcher } from '../event_processor'; | ||
import { NodeRequestHandler } from '../utils/http_request_handler/node_request_handler'; | ||
import { DefaultEventDispatcher } from './default_dispatcher'; | ||
|
||
const eventDispatcher: EventDispatcher = new DefaultEventDispatcher(new NodeRequestHandler()); | ||
|
||
export default eventDispatcher; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import { expect, vi, describe, it } from 'vitest'; | ||
import { DefaultEventDispatcher } from './default_dispatcher'; | ||
import { EventV1 } from '../event_processor'; | ||
|
||
const getEvent = (): EventV1 => { | ||
return { | ||
account_id: 'string', | ||
project_id: 'string', | ||
revision: 'string', | ||
client_name: 'string', | ||
client_version: 'string', | ||
anonymize_ip: true, | ||
enrich_decisions: false, | ||
visitors: [], | ||
}; | ||
}; | ||
|
||
describe('DefaultEventDispatcher', () => { | ||
it('reject the response promise if the eventObj.httpVerb is not POST', async () => { | ||
const eventObj = { | ||
url: 'https://cdn.com/event', | ||
params: getEvent(), | ||
httpVerb: 'GET' as const, | ||
}; | ||
|
||
const requestHnadler = { | ||
makeRequest: vi.fn().mockReturnValue({ | ||
abort: vi.fn(), | ||
responsePromise: Promise.resolve({ statusCode: 203 }), | ||
}), | ||
}; | ||
|
||
const dispatcher = new DefaultEventDispatcher(requestHnadler); | ||
await expect(dispatcher.dispatchEvent(eventObj)).rejects.toThrow(); | ||
}); | ||
|
||
it('sends correct headers and data to the requestHandler', async () => { | ||
const eventObj = { | ||
url: 'https://cdn.com/event', | ||
params: getEvent(), | ||
httpVerb: 'POST' as const, | ||
}; | ||
|
||
const requestHnadler = { | ||
makeRequest: vi.fn().mockReturnValue({ | ||
abort: vi.fn(), | ||
responsePromise: Promise.resolve({ statusCode: 203 }), | ||
}), | ||
}; | ||
|
||
const dispatcher = new DefaultEventDispatcher(requestHnadler); | ||
await dispatcher.dispatchEvent(eventObj); | ||
|
||
expect(requestHnadler.makeRequest).toHaveBeenCalledWith( | ||
eventObj.url, | ||
{ | ||
'content-type': 'application/json', | ||
'content-length': JSON.stringify(eventObj.params).length.toString(), | ||
}, | ||
'POST', | ||
JSON.stringify(eventObj.params) | ||
); | ||
}); | ||
|
||
it('returns a promise that resolves with correct value if the response of the requestHandler resolves', async () => { | ||
const eventObj = { | ||
url: 'https://cdn.com/event', | ||
params: getEvent(), | ||
httpVerb: 'POST' as const, | ||
}; | ||
|
||
const requestHnadler = { | ||
makeRequest: vi.fn().mockReturnValue({ | ||
abort: vi.fn(), | ||
responsePromise: Promise.resolve({ statusCode: 203 }), | ||
}), | ||
}; | ||
|
||
const dispatcher = new DefaultEventDispatcher(requestHnadler); | ||
const response = await dispatcher.dispatchEvent(eventObj); | ||
|
||
expect(response.statusCode).toEqual(203); | ||
}); | ||
|
||
it('returns a promise that rejects if the response of the requestHandler rejects', async () => { | ||
const eventObj = { | ||
url: 'https://cdn.com/event', | ||
params: getEvent(), | ||
httpVerb: 'POST' as const, | ||
}; | ||
|
||
const requestHnadler = { | ||
makeRequest: vi.fn().mockReturnValue({ | ||
abort: vi.fn(), | ||
responsePromise: Promise.reject(new Error('error')), | ||
}), | ||
}; | ||
|
||
const dispatcher = new DefaultEventDispatcher(requestHnadler); | ||
await expect(dispatcher.dispatchEvent(eventObj)).rejects.toThrow(); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
|
||
raju-opti marked this conversation as resolved.
Show resolved
Hide resolved
|
||
import { RequestHandler } from '../utils/http_request_handler/http'; | ||
import { EventDispatcher, EventDispatcherResponse, EventV1Request } from '../event_processor'; | ||
|
||
export class DefaultEventDispatcher implements EventDispatcher { | ||
private requestHandler: RequestHandler; | ||
|
||
constructor(requestHandler: RequestHandler) { | ||
this.requestHandler = requestHandler; | ||
} | ||
|
||
async dispatchEvent( | ||
eventObj: EventV1Request | ||
): Promise<EventDispatcherResponse> { | ||
// Non-POST requests not supported | ||
if (eventObj.httpVerb !== 'POST') { | ||
return Promise.reject(new Error('Only POST requests are supported')); | ||
} | ||
|
||
const dataString = JSON.stringify(eventObj.params); | ||
|
||
const headers = { | ||
'content-type': 'application/json', | ||
'content-length': dataString.length.toString(), | ||
}; | ||
|
||
const abortableRequest = this.requestHandler.makeRequest(eventObj.url, headers, 'POST', dataString); | ||
return abortableRequest.responsePromise; | ||
mikechu-optimizely marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
lib/event_processor/event_processor_factory.browser.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { vi, describe, it, expect, beforeEach } from 'vitest'; | ||
|
||
vi.mock('./default_dispatcher.browser', () => { | ||
return { default: {} }; | ||
}); | ||
|
||
vi.mock('./forwarding_event_processor', () => { | ||
const getForwardingEventProcessor = vi.fn().mockReturnValue({}); | ||
return { getForwardingEventProcessor }; | ||
}); | ||
|
||
import { createForwardingEventProcessor } from './event_processor_factory.browser'; | ||
import { getForwardingEventProcessor } from './forwarding_event_processor'; | ||
import browserDefaultEventDispatcher from './default_dispatcher.browser'; | ||
|
||
describe('createForwardingEventProcessor', () => { | ||
const mockGetForwardingEventProcessor = vi.mocked(getForwardingEventProcessor); | ||
|
||
beforeEach(() => { | ||
mockGetForwardingEventProcessor.mockClear(); | ||
}); | ||
|
||
it('returns forwarding event processor by calling getForwardingEventProcessor with the provided dispatcher', () => { | ||
const eventDispatcher = { | ||
dispatchEvent: vi.fn(), | ||
}; | ||
const processor = createForwardingEventProcessor(eventDispatcher); | ||
expect(Object.is(processor, mockGetForwardingEventProcessor.mock.results[0].value)).toBe(true); | ||
raju-opti marked this conversation as resolved.
Show resolved
Hide resolved
|
||
expect(mockGetForwardingEventProcessor).toHaveBeenNthCalledWith(1, eventDispatcher); | ||
}); | ||
|
||
it('uses the browser default event dispatcher if none is provided', () => { | ||
const processor = createForwardingEventProcessor(); | ||
expect(Object.is(processor, mockGetForwardingEventProcessor.mock.results[0].value)).toBe(true); | ||
expect(mockGetForwardingEventProcessor).toHaveBeenNthCalledWith(1, browserDefaultEventDispatcher); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { getForwardingEventProcessor } from './forwarding_event_processor'; | ||
import { EventDispatcher } from './eventDispatcher'; | ||
import { EventProcessor } from './eventProcessor'; | ||
import defaultEventDispatcher from './default_dispatcher.browser'; | ||
|
||
export const createForwardingEventProcessor = ( | ||
eventDispatcher?: EventDispatcher, | ||
): EventProcessor => { | ||
if (!eventDispatcher) { | ||
eventDispatcher = defaultEventDispatcher; | ||
} | ||
return getForwardingEventProcessor(eventDispatcher); | ||
}; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.