Spaces:
Sleeping
Sleeping
File size: 4,290 Bytes
cd6720a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | import { describe, it, expect } from 'vitest'
import { FetchRequest, FetchResponse } from './fetchUtils'
describe('FetchRequest', () => {
const URL = 'https://example.com/'
it('passes instanceof check with the regular Request', () => {
expect(new FetchRequest(URL)).toBeInstanceOf(Request)
})
it('creates a request with a non-configurable method', () => {
expect
.soft(new FetchRequest(URL, { method: 'CONNECT' }))
.toHaveProperty('method', 'CONNECT')
expect
.soft(new FetchRequest(URL, { method: 'TRACE' }))
.toHaveProperty('method', 'TRACE')
expect
.soft(new FetchRequest(new FetchRequest(URL, { method: 'TRACE' })))
.toHaveProperty('method', 'TRACE')
expect
.soft(new FetchRequest(URL, { method: 'TRACK' }))
.toHaveProperty('method', 'TRACK')
expect
.soft(new FetchRequest(new FetchRequest(URL, { method: 'TRACK' })))
.toHaveProperty('method', 'TRACK')
})
it('creates a request with a non-configurable mode', () => {
expect
.soft(new FetchRequest(URL, { mode: 'navigate' }))
.toHaveProperty('mode', 'navigate')
expect
.soft(new FetchRequest(new FetchRequest(URL, { mode: 'navigate' })))
.toHaveProperty('mode', 'navigate')
expect
.soft(new FetchRequest(URL, { mode: 'websocket' }))
.toHaveProperty('mode', 'websocket')
expect
.soft(new FetchRequest(new FetchRequest(URL, { mode: 'websocket' })))
.toHaveProperty('mode', 'websocket')
expect
.soft(new FetchRequest(URL, { mode: 'webtransport' }))
.toHaveProperty('mode', 'webtransport')
expect
.soft(new FetchRequest(new FetchRequest(URL, { mode: 'webtransport' })))
.toHaveProperty('mode', 'webtransport')
})
it('ignores body for the requests without a body', () => {
expect
.soft(new FetchRequest(URL, { body: null }))
.toHaveProperty('body', null)
expect
.soft(new FetchRequest(URL, { body: undefined }))
.toHaveProperty('body', null)
expect
.soft(new FetchRequest(URL, { body: 'hello' }))
.toHaveProperty('body', null)
expect
.soft(new FetchRequest(URL, { method: 'GET', body: 'hello' }))
.toHaveProperty('body', null)
expect
.soft(new FetchRequest(URL, { method: 'HEAD', body: 'hello' }))
.toHaveProperty('body', null)
expect
.soft(new FetchRequest(URL, { method: 'CONNECT', body: 'hello' }))
.toHaveProperty('body', null)
expect
.soft(new FetchRequest(URL, { method: 'TRACE', body: 'hello' }))
.toHaveProperty('body', null)
expect
.soft(new FetchRequest(URL, { method: 'TRACK', body: 'hello' }))
.toHaveProperty('body', null)
expect
.soft(new FetchRequest(URL, { method: 'POST', body: 'hello' }))
.toHaveProperty('body', expect.any(ReadableStream))
})
})
describe('FetchResponse', () => {
it('clones a regular response', () => {
const response = new Response('hello world')
expect(FetchResponse.clone(response)).toMatchObject(response)
})
it('returns a mocked 500 response if cloning throws', async () => {
const response = new Response('hello world')
const error = new Error('Cannot clone!')
response.clone = function () {
throw error
}
const clone = FetchResponse.clone(response)
expect.soft(clone.status).toBe(500)
expect.soft(clone.statusText).toBe('Unclonable Response')
await expect.soft(clone.json()).resolves.toEqual({
name: error.name,
message: error.message,
stack: error.stack,
})
})
it('sets the response URL', () => {
const response = new Response('hello world')
FetchResponse.setUrl('https://example.com/', response)
expect(response.url).toBe('https://example.com/')
})
it('preserves a custom response URL after cloning Response', () => {
const response = new FetchResponse('hello world')
FetchResponse.setUrl('https://example.com/', response)
expect(response.clone().url).toBe('https://example.com/')
})
it('preserves a custom response URL after cloning FetchResponse', () => {
const response = new FetchResponse('hello world', {
url: 'https://example.com/',
})
expect(response.clone().url).toBe('https://example.com/')
})
})
|