Skip to content

Commit

Permalink
Added tests for the OPTIONS method (v2)
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrienCastex committed Jun 26, 2017
1 parent 9ce23d1 commit 6e5981a
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 0 deletions.
36 changes: 36 additions & 0 deletions test/v2/tests.ts/options/.createFiles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { TestCallback, TestInfo } from '../Type'
import { v2 } from '../../../../lib/index.js'

export function starter(server : v2.WebDAVServer, info : TestInfo, isValid : TestCallback, name : string, expect : string[], reject : string[], callback : (allowHeader ?: string[]) => void) : void
{
server.rootFileSystem.addSubTree(info.ctx, {
'folder': v2.ResourceType.Directory,
'file': v2.ResourceType.File,
'hybrid': v2.ResourceType.Hybrid,
'noResource': v2.ResourceType.NoResource,
}, (e) => {
if(e) return isValid(false, 'Cannot call "addSubTree(...)".', e);

info.req({
url: 'http://localhost:' + info.port + '/' + name,
method: 'OPTIONS'
}, v2.HTTPCodes.OK, (req) => {
if(!req.headers.allow)
return isValid(false, 'No "Allow" header returned in the response of the OPTIONS, but expected one.', JSON.stringify(req.headers, null, 4));

const allow = req.headers.allow.split(',').map((s) => s.trim().toLowerCase());
reject = reject.map((s) => s.trim().toLowerCase());
expect = expect.map((s) => s.trim().toLowerCase());

const rejected = reject.filter((r) => allow.some((a) => a === r));
if(rejected.length > 0)
return isValid(false, 'Forbidden methods for resource present in the "Allow" header in the response.', rejected.toString());

const expected = expect.filter((r) => !allow.some((a) => a === r));
if(expected.length > 0)
return isValid(false, 'All expected methods are not present in the "Allow" header in the response.', expected.toString());

callback(allow);
})
})
}
17 changes: 17 additions & 0 deletions test/v2/tests.ts/options/optionsOnDirectory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Test } from '../Type'
import { v2 } from '../../../../lib/index.js'
import { starter } from './.createFiles'

export default ((info, isValid) =>
{
const server = info.init(1);

starter(server, info, isValid, 'folder', [
'COPY', 'DELETE', 'LOCK', 'MOVE', 'PROPFIND', 'PROPPATCH', 'UNLOCK'
], [
'MKCOL', 'PUT', 'POST', 'GET', 'HEAD'
], () => {
isValid(true);
})

}) as Test;
17 changes: 17 additions & 0 deletions test/v2/tests.ts/options/optionsOnFile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Test } from '../Type'
import { v2 } from '../../../../lib/index.js'
import { starter } from './.createFiles'

export default ((info, isValid) =>
{
const server = info.init(1);

starter(server, info, isValid, 'file', [
'GET', 'PUT', 'POST', 'COPY', 'DELETE', 'HEAD', 'LOCK', 'MOVE', 'PROPFIND', 'PROPPATCH', 'UNLOCK'
], [
'MKCOL'
], () => {
isValid(true);
})

}) as Test;
17 changes: 17 additions & 0 deletions test/v2/tests.ts/options/optionsOnHybrid.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Test } from '../Type'
import { v2 } from '../../../../lib/index.js'
import { starter } from './.createFiles'

export default ((info, isValid) =>
{
const server = info.init(1);

starter(server, info, isValid, 'hybrid', [
'GET', 'PUT', 'POST', 'COPY', 'DELETE', 'HEAD', 'LOCK', 'MOVE', 'PROPFIND', 'PROPPATCH', 'UNLOCK'
], [
'MKCOL'
], () => {
isValid(true);
})

}) as Test;
17 changes: 17 additions & 0 deletions test/v2/tests.ts/options/optionsOnNoResourceType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Test } from '../Type'
import { v2 } from '../../../../lib/index.js'
import { starter } from './.createFiles'

export default ((info, isValid) =>
{
const server = info.init(1);

starter(server, info, isValid, 'noResource', [
'COPY', 'DELETE', 'LOCK', 'MOVE', 'PROPFIND', 'PROPPATCH', 'UNLOCK'
], [
'GET', 'PUT', 'POST', 'HEAD', 'MKCOL'
], () => {
isValid(true);
})

}) as Test;
17 changes: 17 additions & 0 deletions test/v2/tests.ts/options/optionsOnUndefined.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Test } from '../Type'
import { v2 } from '../../../../lib/index.js'
import { starter } from './.createFiles'

export default ((info, isValid) =>
{
const server = info.init(1);

starter(server, info, isValid, 'fileUndefined', [
'MKCOL', 'PUT', 'POST', 'LOCK'
], [
'GET', 'COPY', 'DELETE', 'HEAD', 'MOVE', 'PROPFIND', 'PROPPATCH', 'UNLOCK'
], () => {
isValid(true);
})

}) as Test;

0 comments on commit 6e5981a

Please sign in to comment.