Skip to content

Commit

Permalink
🐛 [RUM-2735] Track request with undefined/null method
Browse files Browse the repository at this point in the history
  • Loading branch information
amortemousque committed Jan 11, 2024
1 parent 4f28486 commit 43c4456
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 4 deletions.
5 changes: 5 additions & 0 deletions packages/core/src/browser/fetchObservable.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ describe('fetch proxy', () => {
fetchStub(FAKE_URL, { method: 'post' }).resolveWith({ status: 500 })
fetchStub(null as any).resolveWith({ status: 500 })
fetchStub({ method: 'POST' } as any).resolveWith({ status: 500 })
fetchStub(FAKE_URL, { method: null as any }).resolveWith({ status: 500 })
fetchStub(FAKE_URL, { method: undefined }).resolveWith({ status: 500 })

fetchStubManager.whenAllComplete(() => {
expect(requests[0].method).toEqual('GET')
Expand All @@ -127,6 +129,9 @@ describe('fetch proxy', () => {
expect(requests[6].method).toEqual('POST')
expect(requests[7].method).toEqual('GET')
expect(requests[8].method).toEqual('GET')
expect(requests[9].method).toEqual('NULL')
expect(requests[10].method).toEqual('GET')

done()
})
})
Expand Down
9 changes: 7 additions & 2 deletions packages/core/src/browser/fetchObservable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,13 @@ function beforeSend(
observable: Observable<FetchContext>
) {
const [input, init] = parameters
const methodFromParams = (init && init.method) || (input instanceof Request && input.method)
const method = methodFromParams ? methodFromParams.toUpperCase() : 'GET'
let methodFromParams = init && init.method

if (methodFromParams === undefined && input instanceof Request) {
methodFromParams = input.method
}

const method = methodFromParams !== undefined ? String(methodFromParams).toUpperCase() : 'GET'
const url = input instanceof Request ? input.url : normalizeUrl(String(input))
const startClocks = clocksNow()

Expand Down
20 changes: 20 additions & 0 deletions packages/core/src/browser/xhrObservable.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,4 +377,24 @@ describe('xhr observable', () => {
expect(XMLHttpRequest.prototype.send).toBe(originalXhrStubSend)
})
})

it('should track request with undefined or null methods', (done) => {
withXhr({
setup(xhr) {
xhr.open(null, '/ok')
xhr.send()
xhr.onreadystatechange = jasmine.createSpy()
xhr.complete(200, 'ok')
xhr.open(undefined, '/ok')
xhr.send()
xhr.onreadystatechange = jasmine.createSpy()
xhr.complete(200, 'ok')
},
onComplete() {
expect(requests[0].method).toBe('NULL')
expect(requests[1].method).toBe('UNDEFINED')
done()
},
})
})
})
2 changes: 1 addition & 1 deletion packages/core/src/browser/xhrObservable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ function createXhrObservable(configuration: Configuration) {
function openXhr({ target: xhr, parameters: [method, url] }: InstrumentedMethodCall<XMLHttpRequest, 'open'>) {
xhrContexts.set(xhr, {
state: 'open',
method: method.toUpperCase(),
method: String(method).toUpperCase(),
url: normalizeUrl(String(url)),
})
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/test/requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ class StubXhr extends StubEventEmitter {
private hasEnded = false

/* eslint-disable @typescript-eslint/no-unused-vars */
open(method: string, url: string | URL | undefined | null) {
open(method: string | undefined | null, url: string | URL | undefined | null) {
this.hasEnded = false
}

Expand Down

0 comments on commit 43c4456

Please sign in to comment.