Open History to see the last commit
1import { describe, expect, it } from 'vitest'
2import { parseRangeHeader, resolveBlobRange } from '../src/blob-range'
3
4describe('resolveBlobRange', () => {
5 it('resolves a slice and defaults length to the end of the object', () => {
6 expect(resolveBlobRange(1000, { offset: 100, length: 50 })).toEqual({
7 offset: 100,
8 length: 50,
9 })
10 expect(resolveBlobRange(1000, { offset: 900 })).toEqual({
11 offset: 900,
12 length: 100,
13 })
14 })
15
16 it('clamps a length that runs past the end', () => {
17 // `bytes=900-5000` against a 1000-byte object is a legal request that
18 // serves 100 bytes — not an error, and not 4101 bytes of promise.
19 expect(resolveBlobRange(1000, { offset: 900, length: 5000 })).toEqual({
20 offset: 900,
21 length: 100,
22 })
23 })
24
25 it('throws on an offset outside the object', () => {
26 // A store that returned an empty body here would serve a 206 claiming
27 // bytes it does not carry. The route is supposed to have answered 416.
28 expect(() => resolveBlobRange(1000, { offset: 1000 })).toThrow(RangeError)
29 expect(() => resolveBlobRange(1000, { offset: -1 })).toThrow(RangeError)
30 expect(() => resolveBlobRange(0, { offset: 0 })).toThrow(RangeError)
31 expect(() => resolveBlobRange(1000, { offset: 1.5 })).toThrow(RangeError)
32 expect(() => resolveBlobRange(1000, { offset: 0, length: -1 })).toThrow(
33 RangeError,
34 )
35 })
36})
37
38describe('parseRangeHeader', () => {
39 it('parses the three satisfiable forms', () => {
40 expect(parseRangeHeader('bytes=0-499', 1000)).toEqual({
41 offset: 0,
42 length: 500,
43 })
44 expect(parseRangeHeader('bytes=500-', 1000)).toEqual({ offset: 500 })
45 // A suffix range is the LAST n bytes — the form that is easy to read
46 // backwards, and reading it backwards serves the wrong bytes as a 206.
47 expect(parseRangeHeader('bytes=-500', 1000)).toEqual({ offset: 500 })
48 expect(parseRangeHeader('bytes=-5000', 1000)).toEqual({ offset: 0 })
49 expect(parseRangeHeader(' bytes=0-0 ', 1000)).toEqual({
50 offset: 0,
51 length: 1,
52 })
53 })
54
55 it('clamps an end past the last byte', () => {
56 expect(parseRangeHeader('bytes=900-5000', 1000)).toEqual({
57 offset: 900,
58 length: 100,
59 })
60 })
61
62 it('reports unsatisfiable ranges so the route can answer 416', () => {
63 expect(parseRangeHeader('bytes=1000-', 1000)).toBe('unsatisfiable')
64 expect(parseRangeHeader('bytes=1500-1600', 1000)).toBe('unsatisfiable')
65 expect(parseRangeHeader('bytes=-0', 1000)).toBe('unsatisfiable')
66 // No range is satisfiable against a zero-byte object — including the
67 // suffix form, which would otherwise resolve to `{ offset: 0 }` and throw
68 // out of the store instead of answering 416.
69 expect(parseRangeHeader('bytes=-1', 0)).toBe('unsatisfiable')
70 expect(parseRangeHeader('bytes=0-', 0)).toBe('unsatisfiable')
71 })
72
73 it('ignores an invalid byte-range-spec rather than rejecting it', () => {
74 // RFC 9110 §14.1.1: `last-byte-pos < first-byte-pos` is INVALID, not
75 // unsatisfiable, and an invalid spec is ignored — the request still
76 // succeeds with the whole representation. A 416 here would fail a request
77 // that is supposed to work.
78 expect(parseRangeHeader('bytes=500-499', 1000)).toBeUndefined()
79 // Invalid wins over unsatisfiable: the size must not turn an ignorable
80 // spec into a 416.
81 expect(parseRangeHeader('bytes=5000-499', 1000)).toBeUndefined()
82 })
83
84 it('serves the whole object for an absent or unimplemented header', () => {
85 expect(parseRangeHeader(null, 1000)).toBeUndefined()
86 expect(parseRangeHeader(undefined, 1000)).toBeUndefined()
87 expect(parseRangeHeader('', 1000)).toBeUndefined()
88 expect(parseRangeHeader('bytes=-', 1000)).toBeUndefined()
89 // Multiple ranges and non-`bytes` units are legal to answer in full.
90 expect(parseRangeHeader('bytes=0-99,200-299', 1000)).toBeUndefined()
91 expect(parseRangeHeader('items=0-99', 1000)).toBeUndefined()
92 expect(parseRangeHeader('bytes 0-99', 1000)).toBeUndefined()
93 })
94})