Skip to content

Commit ec3e639

Browse files
committed
fix http errors testing
1 parent b141dad commit ec3e639

File tree

5 files changed

+38
-19
lines changed

5 files changed

+38
-19
lines changed

bin/local-test-util

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ async function buildNotifiers (notifier) {
121121
async function packNotifiers (notifier) {
122122
const notifiers = notifier
123123
? [ notifier ]
124-
: [ 'js', 'browser', 'node', 'web-worker', 'plugin-angular', 'plugin-react', 'plugin-vue' ]
124+
: [ 'js', 'browser', 'node', 'web-worker', 'plugin-angular', 'plugin-react', 'plugin-vue', 'plugin-http-errors' ]
125125
for (const n of notifiers) {
126126
let packageLocation = `packages/${n}/`
127127
if (n === 'plugin-angular') packageLocation += 'dist/'
@@ -131,7 +131,7 @@ async function packNotifiers (notifier) {
131131

132132
async function installNotifiers (notifier) {
133133
trace('install notifiers')
134-
if (notifier && ![ 'browser', 'plugin-vue', 'plugin-react', 'web-worker' ].includes(notifier)) return
134+
if (notifier && ![ 'browser', 'plugin-vue', 'plugin-react', 'web-worker', 'plugin-http-errors' ].includes(notifier)) return
135135
await ex(`npm`, [
136136
`install`,
137137
`--no-package-lock`,
@@ -144,7 +144,8 @@ async function installNotifiers (notifier) {
144144
`../../../../bugsnag-browser-${require('../packages/browser/package.json').version}.tgz`,
145145
`../../../../bugsnag-web-worker-${require('../packages/web-worker/package.json').version}.tgz`,
146146
`../../../../bugsnag-plugin-react-${require('../packages/plugin-react/package.json').version}.tgz`,
147-
`../../../../bugsnag-plugin-vue-${require('../packages/plugin-vue/package.json').version}.tgz`
147+
`../../../../bugsnag-plugin-vue-${require('../packages/plugin-vue/package.json').version}.tgz`,
148+
`../../../../bugsnag-plugin-http-errors-${require('../packages/plugin-http-errors/package.json').version}.tgz`
148149
]
149150
), {
150151
cwd: `${__dirname}/../test/browser/features/fixtures`

packages/plugin-http-errors/http-errors.js

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -178,28 +178,43 @@ module.exports = (config = {}, global = window) => {
178178
// Extract request body
179179
let requestBody = ''
180180
let requestBodyLength = 0
181-
if (startContext && startContext.body) {
182-
const bodyStr = typeof startContext.body === 'string' ? startContext.body : String(startContext.body)
183-
requestBodyLength = bodyStr.length
181+
const initialBody = startContext.init?.body || startContext.body
182+
if (initialBody) {
183+
const bodyStr = String(initialBody)
184184
requestBody = truncate(bodyStr, maxRequestSize)
185+
requestBodyLength = bodyStr.length
186+
}
187+
188+
// Extract response body
189+
let responseBody
190+
let responseBodyLength
191+
if (endContext.xhr && endContext.xhr.responseText) {
192+
responseBody = truncate(endContext.xhr.responseText, maxRequestSize)
193+
responseBodyLength = endContext.xhr.responseText.length
194+
}
195+
196+
// Extract response headers
197+
let responseHeaders = {}
198+
if (endContext.response && endContext.response.headers) {
199+
responseHeaders = headersToObject(endContext.response.headers)
200+
} else if (endContext.xhr && typeof endContext.xhr.getAllResponseHeaders === 'function') {
201+
responseHeaders = responseHeadersToObject(endContext.xhr.getAllResponseHeaders())
185202
}
186203

187204
// Create request and response objects for callback
188205
const requestObj = {
189206
url,
190207
httpMethod: method,
191-
headers: headersToObject(startContext && startContext.xhr && startContext.xhr._requestHeaders),
208+
headers: headersToObject(startContext.xhr?._requestHeaders || startContext.init?.headers),
192209
params: parseQueryParams(url),
193210
body: requestBody,
194211
bodyLength: requestBodyLength
195212
}
196-
197-
const responseHeaders = endContext.xhr.getAllResponseHeaders()
198213
const responseObj = {
199214
statusCode: endContext.status,
200-
headers: responseHeadersToObject(responseHeaders),
201-
body: endContext.xhr.responseText,
202-
bodyLength: endContext.xhr.responseText ? endContext.xhr.responseText.length : 0
215+
headers: responseHeaders,
216+
body: responseBody,
217+
bodyLength: responseBodyLength
203218
}
204219

205220
// Call onHttpError callback if provided

packages/plugin-http-errors/test/http-errors-xhr.test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,12 +165,12 @@ describe('plugin-http-errors', () => {
165165
expect(event.response.bodyLength).toBe(xhr.responseText.length)
166166
})
167167

168-
it('should truncate XHR response body when it exceeds maxResponseSize', async () => {
168+
it('should truncate XHR response body when it exceeds maxRequestSize', async () => {
169169
const notifyCallbacks: Event[] = []
170170

171171
plugin = createPlugin({
172172
httpErrorCodes: { min: 400, max: 499 },
173-
maxResponseSize: 20
173+
maxRequestSize: 20
174174
})
175175

176176
const client = new Client({ apiKey: 'api_key', plugins: [plugin] })
@@ -179,7 +179,7 @@ describe('plugin-http-errors', () => {
179179
// Create and configure XHR with large response body
180180
const xhr = new XMLHttpRequest() as any
181181
const largeResponseBody = 'A'.repeat(100)
182-
xhr.status = 500
182+
xhr.status = 404
183183
xhr.statusText = 'Internal Server Error'
184184
xhr.responseURL = 'https://api.example.com/error'
185185
xhr.response = largeResponseBody
@@ -188,6 +188,7 @@ describe('plugin-http-errors', () => {
188188
xhr.open('GET', 'https://api.example.com/error')
189189
xhr.send()
190190

191+
// Wait for async processing
191192
await new Promise(resolve => setTimeout(resolve, 20))
192193

193194
expect(notifyCallbacks.length).toBe(1)

test/browser/features/fixtures/http_errors/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<html>
33
<head>
44
<meta charset="utf-8" />
5-
<title>HTTP Errors - Fetch</title>
5+
<title>HTTP Errors</title>
66
</head>
77
<body>
88
<div id="root"></div>

test/browser/features/http_errors.feature

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@ Feature: HTTP Errors
2727

2828
And the event "response.statusCode" equals 404
2929
And the event "response.headers.content-length" equals "37"
30-
# response.body is not reported for fetch requests
31-
# response.bodyLength is not reported for fetch requests
30+
31+
# Response body is not captured for fetch requests
32+
And the event "response.body" is null
33+
And the event "response.bodyLength" is null
3234

3335
Scenario: XML HTTP Request
3436
When I navigate to the test URL "/http_errors?request=xhr"
@@ -54,6 +56,6 @@ Feature: HTTP Errors
5456

5557
And the event "response.statusCode" equals 404
5658
And the event "response.headers.content-length" equals "37"
57-
And the event "response.body" equals ""
59+
And the event "response.body" equals "Returned status 404 after waiting ms"
5860
And the event "response.bodyLength" equals 37
5961

0 commit comments

Comments
 (0)