|
| 1 | +import { createStealthXhr } from '../../src/js/utils/xhr'; |
| 2 | + |
| 3 | +describe('xhr', () => { |
| 4 | + it('creates xhr and calls monkey patched methods if original was not preserved', () => { |
| 5 | + const XMLHttpRequestMock = getXhrMock(); |
| 6 | + const globalMock = createGlobalMock(XMLHttpRequestMock); |
| 7 | + |
| 8 | + const xhr = createStealthXhr(globalMock); |
| 9 | + |
| 10 | + xhr!.open('GET', 'https://example.com'); |
| 11 | + xhr!.send(); |
| 12 | + |
| 13 | + expect(xhr!.open).toHaveBeenCalledWith('GET', 'https://example.com'); |
| 14 | + expect(xhr!.send).toHaveBeenCalled(); |
| 15 | + }); |
| 16 | + |
| 17 | + it('monkey patched xhr is not called when original is preserved', () => { |
| 18 | + const XMLHttpRequestMock = getXhrMock(); |
| 19 | + const globalMock = createGlobalMock(XMLHttpRequestMock); |
| 20 | + |
| 21 | + const { xhrOpenMonkeyPatch, xhrSendMonkeyPatch } = mockSentryPatchWithOriginal(globalMock); |
| 22 | + |
| 23 | + const xhr = createStealthXhr(globalMock); |
| 24 | + |
| 25 | + xhr!.open('GET', 'https://example.com'); |
| 26 | + xhr!.send(); |
| 27 | + |
| 28 | + expect(xhrOpenMonkeyPatch).not.toHaveBeenCalled(); |
| 29 | + expect(xhrSendMonkeyPatch).not.toHaveBeenCalled(); |
| 30 | + expect(xhr!.open).toHaveBeenCalledWith('GET', 'https://example.com'); |
| 31 | + expect(xhr!.send).toHaveBeenCalled(); |
| 32 | + }); |
| 33 | +}); |
| 34 | + |
| 35 | +function createGlobalMock(xhr: unknown) { |
| 36 | + return { |
| 37 | + XMLHttpRequest: xhr as typeof XMLHttpRequest, |
| 38 | + }; |
| 39 | +} |
| 40 | + |
| 41 | +function getXhrMock() { |
| 42 | + function XhrMock() {} |
| 43 | + |
| 44 | + XhrMock.prototype.open = jest.fn(); |
| 45 | + XhrMock.prototype.send = jest.fn(); |
| 46 | + |
| 47 | + return XhrMock; |
| 48 | +} |
| 49 | + |
| 50 | +type WithSentryOriginal<T> = T & { __sentry_original__?: T }; |
| 51 | + |
| 52 | +function mockSentryPatchWithOriginal(globalMock: { XMLHttpRequest: typeof XMLHttpRequest }): { |
| 53 | + xhrOpenMonkeyPatch: jest.Mock; |
| 54 | + xhrSendMonkeyPatch: jest.Mock; |
| 55 | +} { |
| 56 | + const originalOpen = globalMock.XMLHttpRequest.prototype.open; |
| 57 | + const originalSend = globalMock.XMLHttpRequest.prototype.send; |
| 58 | + |
| 59 | + const xhrOpenMonkeyPatch = jest.fn(); |
| 60 | + const xhrSendMonkeyPatch = jest.fn(); |
| 61 | + |
| 62 | + globalMock.XMLHttpRequest.prototype.open = xhrOpenMonkeyPatch; |
| 63 | + globalMock.XMLHttpRequest.prototype.send = xhrSendMonkeyPatch; |
| 64 | + |
| 65 | + ( |
| 66 | + globalMock.XMLHttpRequest.prototype.open as WithSentryOriginal<typeof XMLHttpRequest.prototype.open> |
| 67 | + ).__sentry_original__ = originalOpen; |
| 68 | + ( |
| 69 | + globalMock.XMLHttpRequest.prototype.send as WithSentryOriginal<typeof XMLHttpRequest.prototype.send> |
| 70 | + ).__sentry_original__ = originalSend; |
| 71 | + |
| 72 | + return { |
| 73 | + xhrOpenMonkeyPatch, |
| 74 | + xhrSendMonkeyPatch, |
| 75 | + }; |
| 76 | +} |
0 commit comments