Skip to content

Commit

Permalink
Added tests for the ranged GET
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrienCastex committed Aug 1, 2017
1 parent 10f0e51 commit 647a8c3
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 4 deletions.
8 changes: 4 additions & 4 deletions test/v2/tests.ts/readWrite/.createFileTxt.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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);
});
})
})
Expand Down
53 changes: 53 additions & 0 deletions test/v2/tests.ts/readWrite/getRanged.ts
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 647a8c3

Please sign in to comment.