Skip to content

Commit

Permalink
fix(xhr): support original responses without any headers (#216)
Browse files Browse the repository at this point in the history
  • Loading branch information
kettanaito authored Feb 27, 2022
1 parent 4563ce7 commit ef653c6
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
"@open-draft/until": "^1.0.3",
"@xmldom/xmldom": "^0.7.5",
"debug": "^4.3.3",
"headers-polyfill": "^3.0.3",
"headers-polyfill": "^3.0.4",
"outvariant": "^1.2.1",
"strict-event-emitter": "^0.2.0"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* @jest-environment jsdom
*/
import { ServerApi, createServer } from '@open-draft/test-server'
import { createInterceptor } from '../../../../src'
import { interceptXMLHttpRequest } from '../../../../src/interceptors/XMLHttpRequest'
import { createXMLHttpRequest } from '../../../helpers'

let httpServer: ServerApi

const interceptor = createInterceptor({
modules: [interceptXMLHttpRequest],
resolver() {},
})

beforeAll(async () => {
httpServer = await createServer((app) => {
app.get('/user', (_req, res) => {
// Respond with a message that has no headers.
res.socket?.end(`\
HTTP/1.1 200 OK
hello world`)
})
})

interceptor.apply()

/**
* @note Stub the internal JSDOM property to prevent the following error:
* Error: Cross origin http://127.0.0.1:XXXXX/ forbidden
*/
const { protocol, host, port } = httpServer.http.getAddress()
// @ts-expect-error
window._origin = `${protocol}//${host}:${port}`
})

afterAll(async () => {
interceptor.restore()
await httpServer.close()
})

test('handles an original response without any headers', async () => {
const req = await createXMLHttpRequest((req) => {
req.open('GET', httpServer.http.makeUrl('/user'))
req.send()
})

expect(req.status).toEqual(200)
expect(req.statusText).toEqual('OK')
expect(req.responseText).toEqual('hello world')
expect(req.getAllResponseHeaders()).toEqual('')
})
11 changes: 8 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2544,9 +2544,9 @@ findup-sync@^4.0.0:
resolve-dir "^1.0.1"

follow-redirects@^1.14.4:
version "1.14.8"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.14.8.tgz#016996fb9a11a100566398b1c6839337d7bfa8fc"
integrity sha512-1x0S9UVJHsQprFcEC/qnNzBLcIxsjAV905f/UkQxbclCsoTWlacCNOpQa/anodLl2uaEKFhfWOvM2Qg77+15zA==
version "1.14.9"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.14.9.tgz#dd4ea157de7bfaf9ea9b3fbd85aa16951f78d8d7"
integrity sha512-MQDfihBQYMcyy5dhRDJUHcw7lb2Pv/TuE6xP1vyraLukNDHKbDxDNaOE3NbCAdKQApno+GPRyo1YAp89yCjK4w==

form-data@^3.0.0:
version "3.0.1"
Expand Down Expand Up @@ -2777,6 +2777,11 @@ headers-polyfill@^3.0.3:
resolved "https://registry.yarnpkg.com/headers-polyfill/-/headers-polyfill-3.0.3.tgz#222bd9a6b5a1f610ad563473efb0d34a13eb7ebc"
integrity sha512-Ak5m549Y+5vBtWNnIUcyARJjF0tQpINuy3+vOqG8tK/qjF0itYhAPgrzJc0zsZJh7AX8PalzICMPuWIqHpZlEA==

headers-polyfill@^3.0.4:
version "3.0.4"
resolved "https://registry.yarnpkg.com/headers-polyfill/-/headers-polyfill-3.0.4.tgz#cd70c815a441dd882372fcd6eda212ce997c9b18"
integrity sha512-I1DOM1EdWYntdrnCvqQtcKwSSuiTzoqOExy4v1mdcFixFZABlWP4IPHdmoLtPda0abMHqDOY4H9svhQ10DFR4w==

homedir-polyfill@^1.0.1:
version "1.0.3"
resolved "https://registry.yarnpkg.com/homedir-polyfill/-/homedir-polyfill-1.0.3.tgz#743298cef4e5af3e194161fbadcc2151d3a058e8"
Expand Down

0 comments on commit ef653c6

Please sign in to comment.