Skip to content
This repository was archived by the owner on Jul 31, 2025. It is now read-only.

Commit 977bb2d

Browse files
author
v1rtl
committed
add a few tests for req.range
1 parent af7adb0 commit 977bb2d

File tree

1 file changed

+38
-5
lines changed

1 file changed

+38
-5
lines changed

tests/modules/req.test.ts

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
// import { expect } from 'https://deno.land/x/expect@v0.2.6/mod.ts'
1+
import { expect } from 'https://deno.land/x/expect@v0.2.6/mod.ts'
22
import { describe, it, InitAppAndTest } from '../util.ts'
33

44
import {
55
checkIfXMLHttpRequest,
66
getAccepts,
77
getAcceptsEncodings,
88
getFreshOrStale,
9-
getRequestHeader
10-
/* getRangeFromHeader,
11-
reqIs */
9+
getRequestHeader,
10+
getRangeFromHeader,
11+
reqIs
1212
} from '../../extensions/req/mod.ts'
13-
// import { Ranges } from '../../types.ts'
13+
import { Ranges } from '../../types.ts'
1414

1515
describe('Request extensions', () => {
1616
describe('req.get(header)', () => {
@@ -104,4 +104,37 @@ describe('Request extensions', () => {
104104
await fetch.get('/').expect('stale')
105105
})
106106
})
107+
describe('req.range', () => {
108+
it('should return parsed ranges', async () => {
109+
const { fetch } = InitAppAndTest((req, res) => {
110+
const range = getRangeFromHeader(req)
111+
const array = range(300)
112+
expect(array).toContain({ end: 299, start: 0 })
113+
expect(array).toHaveLength(1)
114+
res.end()
115+
})
116+
117+
await fetch.get('/').set('Range', 'bytes=0-1000')
118+
})
119+
it('should cap to the given size', async () => {
120+
const { fetch } = InitAppAndTest((req, res) => {
121+
const range = getRangeFromHeader(req)
122+
const size = 300
123+
expect((range(size) as Ranges)?.[0].end).toBe(size - 1)
124+
res.end()
125+
})
126+
127+
await fetch.get('/').set('Range', 'bytes=0-1000')
128+
})
129+
it('should cap to the given size when open-ended', async () => {
130+
const { fetch } = InitAppAndTest((req, res) => {
131+
const range = getRangeFromHeader(req)
132+
const size = 300
133+
expect((range(size) as Ranges)?.[0].end).toBe(size - 1)
134+
res.end()
135+
})
136+
137+
await fetch.get('/').set('Range', 'bytes=0-')
138+
})
139+
})
107140
})

0 commit comments

Comments
 (0)