|
| 1 | +import { Test, TestCallback, TestInfo } from '../Type' |
| 2 | +import { v2 } from '../../../../lib/index.js' |
| 3 | +import { starter } from './.createFileTxt' |
| 4 | + |
| 5 | +const content = 'Helio!'; |
| 6 | +function go(info : TestInfo, isValid : TestCallback, range : string, callback : (statusCode : number, headers : any, body : string) => void) |
| 7 | +{ |
| 8 | + starter(info.startServer(), info, isValid, content, (r, s) => { |
| 9 | + info.req({ |
| 10 | + url: 'http://localhost:' + s.options.port + '/file.txt', |
| 11 | + method: 'GET', |
| 12 | + headers: { |
| 13 | + 'Range': range |
| 14 | + } |
| 15 | + }, (res, body) => { |
| 16 | + callback(res.statusCode, res.headers, body); |
| 17 | + }) |
| 18 | + }) |
| 19 | +} |
| 20 | + |
| 21 | +export default ((info, isValid) => |
| 22 | +{ |
| 23 | + const server = info.init(7); |
| 24 | + |
| 25 | + go(info, isValid, 'bytes=0-100', (statusCode, headers, body) => { |
| 26 | + isValid(headers['content-length'] === content.length.toString(), 'The content length returned must be a maximum the range could retrieve, but instead of ' + content.length + ', got ' + headers['content-length'] + '.'); |
| 27 | + }) |
| 28 | + |
| 29 | + go(info, isValid, 'bytes=0-1', (statusCode, headers, body) => { |
| 30 | + isValid(headers['content-length'] === '2', 'The content length returned must be equals to 2 when 0-1 is asked, but instead of ' + 2 + ', got ' + headers['content-length'] + '.'); |
| 31 | + }) |
| 32 | + |
| 33 | + go(info, isValid, 'bytes=0-0', (statusCode, headers, body) => { |
| 34 | + isValid(headers['content-length'] === '1', 'The content length returned must be equals to 1 when 0-0 is asked, but instead of ' + 1 + ', got ' + headers['content-length'] + '.'); |
| 35 | + }) |
| 36 | + |
| 37 | + go(info, isValid, 'bytes=0-100', (statusCode, headers, body) => { |
| 38 | + isValid(body === content, 'Expected "' + content + '" but got "' + body + '".'); |
| 39 | + }) |
| 40 | + |
| 41 | + go(info, isValid, 'bytes=0-0', (statusCode, headers, body) => { |
| 42 | + isValid(body === 'H', 'Expected "H" but got "' + body + '".'); |
| 43 | + }) |
| 44 | + |
| 45 | + go(info, isValid, 'bytes=1-1', (statusCode, headers, body) => { |
| 46 | + isValid(body === 'e', 'Expected "e" but got "' + body + '".'); |
| 47 | + }) |
| 48 | + |
| 49 | + go(info, isValid, 'bytes=' + (content.length - 1) + '-' + (content.length - 1), (statusCode, headers, body) => { |
| 50 | + isValid(body === '!', 'Expected "!" but got "' + body + '".'); |
| 51 | + }) |
| 52 | + |
| 53 | +}) as Test; |
0 commit comments