-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(request): Enable to create Request from light weight request. (#133
) * refactor(response): Object.defineProperty is done in response.ts, so there is no need to do it in globals.ts * feat(request): Enable to create Request from light weight request.
- Loading branch information
Showing
5 changed files
with
105 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,87 @@ | ||
import type { IncomingMessage } from 'node:http' | ||
import { newRequest } from '../src/request' | ||
import { newRequest, Request, GlobalRequest } from '../src/request' | ||
|
||
describe('Request', () => { | ||
it('Compatibility with standard Request object', async () => { | ||
const req = newRequest({ | ||
method: 'GET', | ||
url: '/', | ||
headers: { | ||
host: 'localhost', | ||
}, | ||
rawHeaders: ['host', 'localhost'], | ||
} as IncomingMessage) | ||
|
||
expect(req).toBeInstanceOf(global.Request) | ||
expect(req.method).toBe('GET') | ||
expect(req.url).toBe('http://localhost/') | ||
expect(req.headers.get('host')).toBe('localhost') | ||
}) | ||
describe('newRequest', () => { | ||
it('Compatibility with standard Request object', async () => { | ||
const req = newRequest({ | ||
method: 'GET', | ||
url: '/', | ||
headers: { | ||
host: 'localhost', | ||
}, | ||
rawHeaders: ['host', 'localhost'], | ||
} as IncomingMessage) | ||
|
||
expect(req).toBeInstanceOf(global.Request) | ||
expect(req.method).toBe('GET') | ||
expect(req.url).toBe('http://localhost/') | ||
expect(req.headers.get('host')).toBe('localhost') | ||
}) | ||
|
||
it('Should resolve double dots in URL', async () => { | ||
const req = newRequest({ | ||
headers: { | ||
host: 'localhost', | ||
}, | ||
url: '/static/../foo.txt', | ||
} as IncomingMessage) | ||
expect(req).toBeInstanceOf(global.Request) | ||
expect(req.url).toBe('http://localhost/foo.txt') | ||
}) | ||
|
||
it('Should resolve double dots in URL', async () => { | ||
const req = newRequest({ | ||
headers: { | ||
host: 'localhost', | ||
}, | ||
url: '/static/../foo.txt', | ||
} as IncomingMessage) | ||
expect(req).toBeInstanceOf(global.Request) | ||
expect(req.url).toBe('http://localhost/foo.txt') | ||
it('Should resolve double dots in host header', async () => { | ||
const req = newRequest({ | ||
headers: { | ||
host: 'localhost/..', | ||
}, | ||
url: '/foo.txt', | ||
} as IncomingMessage) | ||
expect(req).toBeInstanceOf(global.Request) | ||
expect(req.url).toBe('http://localhost/foo.txt') | ||
}) | ||
}) | ||
|
||
it('Should resolve double dots in host header', async () => { | ||
const req = newRequest({ | ||
headers: { | ||
host: 'localhost/..', | ||
}, | ||
url: '/foo.txt', | ||
} as IncomingMessage) | ||
expect(req).toBeInstanceOf(global.Request) | ||
expect(req.url).toBe('http://localhost/foo.txt') | ||
describe('GlobalRequest', () => { | ||
it('should be overrode by Request', () => { | ||
expect(Request).not.toBe(GlobalRequest) | ||
}) | ||
|
||
it('should be instance of GlobalRequest', () => { | ||
const req = new Request('http://localhost/') | ||
expect(req).toBeInstanceOf(GlobalRequest) | ||
}) | ||
|
||
it('should be success to create instance from old light weight instance', async () => { | ||
const req = newRequest({ | ||
method: 'GET', | ||
url: '/', | ||
headers: { | ||
host: 'localhost', | ||
}, | ||
rawHeaders: ['host', 'localhost'], | ||
} as IncomingMessage) | ||
const req2 = new Request(req, { | ||
method: 'POST', | ||
body: 'foo', | ||
}) | ||
expect(req2).toBeInstanceOf(GlobalRequest) | ||
expect(await req2.text()).toBe('foo') | ||
}) | ||
|
||
it('should set `duplex: "half"` automatically if body is a ReadableStream', async () => { | ||
const stream = new ReadableStream({ | ||
start(controller) { | ||
controller.enqueue(new TextEncoder().encode('bar')) | ||
controller.close() | ||
}, | ||
}) | ||
const req2 = new Request('http://localhost', { | ||
method: 'POST', | ||
body: stream, | ||
}) | ||
expect(req2).toBeInstanceOf(GlobalRequest) | ||
expect(req2.text()).resolves.toBe('bar') | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters