diff --git a/test/v2/tests.ts/readWrite/.createFileTxt.ts b/test/v2/tests.ts/readWrite/.createFileTxt.ts index 819e7846..07f4d9c1 100644 --- a/test/v2/tests.ts/readWrite/.createFileTxt.ts +++ b/test/v2/tests.ts/readWrite/.createFileTxt.ts @@ -1,9 +1,9 @@ import { TestCallback, TestInfo } from '../Type' import { v2 } from '../../../../lib/index.js' -export function starter(server : v2.WebDAVServer, info : TestInfo, isValid : TestCallback, content : string | Buffer, _type ?: v2.ResourceType | ((r ?: v2.Resource) => void), _callback ?: (r ?: v2.Resource) => void) : void +export function starter(server : v2.WebDAVServer, info : TestInfo, isValid : TestCallback, content : string | Buffer, _type ?: v2.ResourceType | ((r ?: v2.Resource, server ?: v2.WebDAVServer) => void), _callback ?: (r ?: v2.Resource, server ?: v2.WebDAVServer) => void) : void { - const callback = _callback ? _callback : _type as (r ?: v2.Resource) => void; + const callback = _callback ? _callback : _type as (r ?: v2.Resource, server ?: v2.WebDAVServer) => void; const type = _callback ? _type as v2.ResourceType : v2.ResourceType.File; const name = 'file.txt'; @@ -17,14 +17,14 @@ export function starter(server : v2.WebDAVServer, info : TestInfo, isValid : Tes if(e) return isValid(false, 'Could not find //' + name, e); if(!type.isFile) - return callback(r); + return callback(r, server); r.openWriteStream((e, wStream) => { if(e) return isValid(false, 'Could not open the resource for writing.', e); wStream.end(content, (e) => { if(e) return isValid(false, 'Could not write content to the resource.', e); - callback(r); + callback(r, server); }); }) }) diff --git a/test/v2/tests.ts/readWrite/getRanged.ts b/test/v2/tests.ts/readWrite/getRanged.ts new file mode 100644 index 00000000..891538c0 --- /dev/null +++ b/test/v2/tests.ts/readWrite/getRanged.ts @@ -0,0 +1,53 @@ +import { Test, TestCallback, TestInfo } from '../Type' +import { v2 } from '../../../../lib/index.js' +import { starter } from './.createFileTxt' + +const content = 'Helio!'; +function go(info : TestInfo, isValid : TestCallback, range : string, callback : (statusCode : number, headers : any, body : string) => void) +{ + starter(info.startServer(), info, isValid, content, (r, s) => { + info.req({ + url: 'http://localhost:' + s.options.port + '/file.txt', + method: 'GET', + headers: { + 'Range': range + } + }, (res, body) => { + callback(res.statusCode, res.headers, body); + }) + }) +} + +export default ((info, isValid) => +{ + const server = info.init(7); + + go(info, isValid, 'bytes=0-100', (statusCode, headers, body) => { + 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'] + '.'); + }) + + go(info, isValid, 'bytes=0-1', (statusCode, headers, body) => { + 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'] + '.'); + }) + + go(info, isValid, 'bytes=0-0', (statusCode, headers, body) => { + 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'] + '.'); + }) + + go(info, isValid, 'bytes=0-100', (statusCode, headers, body) => { + isValid(body === content, 'Expected "' + content + '" but got "' + body + '".'); + }) + + go(info, isValid, 'bytes=0-0', (statusCode, headers, body) => { + isValid(body === 'H', 'Expected "H" but got "' + body + '".'); + }) + + go(info, isValid, 'bytes=1-1', (statusCode, headers, body) => { + isValid(body === 'e', 'Expected "e" but got "' + body + '".'); + }) + + go(info, isValid, 'bytes=' + (content.length - 1) + '-' + (content.length - 1), (statusCode, headers, body) => { + isValid(body === '!', 'Expected "!" but got "' + body + '".'); + }) + +}) as Test;