Skip to content

Commit

Permalink
Merge pull request #27 from PVince81/test-additions
Browse files Browse the repository at this point in the history
Test additions
  • Loading branch information
evert authored Mar 9, 2017
2 parents f1fd805 + e9afcfd commit 2fd4f11
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 87 deletions.
13 changes: 13 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,16 @@ language: node_js
node_js:
"6"

cache:
directories:
- $HOME/.npm
- /tmp/phantomjs

branches:
only:
- master

before_script:
- export DISPLAY=:99.0
- sh -e /etc/init.d/xvfb start

4 changes: 4 additions & 0 deletions lib/client.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/*
* vim: expandtab shiftwidth=4 softtabstop=4
*/

/* global dav */
if (typeof dav == 'undefined') { dav = {}; };

Expand Down
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
"karma-jasmine": "^1.1.0",
"karma-jasmine-sinon": "^1.0.4",
"karma-phantomjs-launcher": "^0.2.1",
"phantomjs-prebuilt": "2.1.7"
"phantomjs": "2.1.7"
},
"scripts": {
"test": "./node_modules/.bin/karma start --single-run --browsers PhantomJS,Firefox"
}
}

186 changes: 100 additions & 86 deletions tests/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,95 @@ describe("client", function() {
resolver = null;
});

function getRequestedProps(xmlBody, path) {
var doc = parser.parseFromString(xmlBody, "application/xml");

var requestedProps = [];
var propsIterator = doc.evaluate(path, doc, resolver, XPathResult.ANY_TYPE, null);
var propNode;
while (propNode = propsIterator.iterateNext()) {
requestedProps.push(propNode);
}

return requestedProps;
}

function testPropSet(clientMethod, httpMethod, xmlPath) {
it('sends given properties as XML body', function() {
var requestStub = sinon.stub(dav.Client.prototype, 'request');
var thenStub = sinon.stub();
requestStub.returns({
then: thenStub
});
client[clientMethod](
'/rel/path',
{
'{DAV:}getetag': '"abc"',
'{http://example.org/ns}someprop': 'newvalue'
},
{
'CustomHeader': 'Value',
'CustomHeader2': 'Value2'
}
);

expect(requestStub.calledOnce).toEqual(true);
expect(requestStub.getCall(0).args[0]).toEqual(httpMethod);
expect(requestStub.getCall(0).args[1]).toEqual('/rel/path');
expect(requestStub.getCall(0).args[2]).toEqual({
'Content-Type': 'application/xml; charset=utf-8',
'CustomHeader': 'Value',
'CustomHeader2': 'Value2'
});

var xmlBody = requestStub.getCall(0).args[3];
expect(xmlBody).toBeDefined();
var requestedProps = getRequestedProps(xmlBody, xmlPath);

expect(requestedProps.length).toEqual(2);
expect(requestedProps[0].nodeName).toEqual('d:getetag');
expect(requestedProps[0].textContent).toEqual('"abc"');
expect(requestedProps[1].nodeName).toEqual('e:someprop');
expect(requestedProps[1].textContent).toEqual('newvalue');

requestStub.restore();
});

it('returns body', function() {
var requestStub = sinon.stub(dav.Client.prototype, 'request');
var thenStub = sinon.stub();
requestStub.returns({
then: thenStub
});

client[clientMethod](
'/rel/path',
[],
{},
{
'{DAV:}getetag': '"abc"',
'{http://example.org/ns}someprop': 'newvalue'
}
);

expect(requestStub.calledOnce).toEqual(true);
expect(thenStub.calledOnce).toEqual(true);

var result = thenStub.getCall(0).args[0].call(null, {
status: 207,
body: 'response',
xhr: 'dummyxhr'
});

expect(result).toEqual({
status: 207,
body: 'response',
xhr: 'dummyxhr'
});
requestStub.restore();
});
};

describe('request', function() {
it('sends the given request', function() {

Expand Down Expand Up @@ -270,22 +359,6 @@ describe("client", function() {
});

describe('PROPFIND', function() {
/**
* Returns requested properties from a propfind body
*/
function getRequestedProps(xmlBody) {
var doc = parser.parseFromString(xmlBody, "application/xml");

var requestedProps = [];
var propsIterator = doc.evaluate('/d:propfind/d:prop/*', doc, resolver, XPathResult.ANY_TYPE, null);
var propNode;
while (propNode = propsIterator.iterateNext()) {
requestedProps.push(propNode);
}

return requestedProps;
}

it('submits the given properties as XML', function() {
var requestStub = sinon.stub(dav.Client.prototype, 'request');
var thenStub = sinon.stub();
Expand Down Expand Up @@ -318,7 +391,7 @@ describe("client", function() {

var xmlBody = requestStub.getCall(0).args[3];
expect(xmlBody).toBeDefined();
var requestedProps = getRequestedProps(xmlBody);
var requestedProps = getRequestedProps(xmlBody, '/d:propfind/d:prop/*');

expect(requestedProps.length).toEqual(3);
expect(requestedProps[0].nodeName).toEqual('d:getetag');
Expand Down Expand Up @@ -368,90 +441,31 @@ describe("client", function() {
});

describe('PROPPATCH', function() {
function getRequestedProps(xmlBody) {
var doc = parser.parseFromString(xmlBody, "application/xml");

var requestedProps = [];
var propsIterator = doc.evaluate('/d:propertyupdate/d:set/d:prop/*', doc, resolver, XPathResult.ANY_TYPE, null);
var propNode;
while (propNode = propsIterator.iterateNext()) {
requestedProps.push(propNode);
}
testPropSet('propPatch', 'PROPPATCH', '/d:propertyupdate/d:set/d:prop/*');
});

return requestedProps;
}
describe('MKCOL', function() {
testPropSet('mkcol', 'MKCOL', '/d:mkcol/d:set/d:prop/*');

it('sends given properties as XML body', function() {
it('does not send any body when no properties given', function() {
var requestStub = sinon.stub(dav.Client.prototype, 'request');
var thenStub = sinon.stub();
requestStub.returns({
then: thenStub
});
client.propPatch(
client.mkcol(
'/rel/path',
{
'{DAV:}getetag': '"abc"',
'{http://example.org/ns}someprop': 'newvalue'
},
{
'CustomHeader': 'Value',
'CustomHeader2': 'Value2'
}
null
);

expect(requestStub.calledOnce).toEqual(true);
expect(requestStub.getCall(0).args[0]).toEqual('PROPPATCH');
expect(requestStub.getCall(0).args[0]).toEqual('MKCOL');
expect(requestStub.getCall(0).args[1]).toEqual('/rel/path');
expect(requestStub.getCall(0).args[2]).toEqual({
'Content-Type': 'application/xml; charset=utf-8',
'CustomHeader': 'Value',
'CustomHeader2': 'Value2'
'Content-Type': 'application/xml; charset=utf-8'
});
expect(requestStub.getCall(0).args[3]).toEqual('');

var xmlBody = requestStub.getCall(0).args[3];
expect(xmlBody).toBeDefined();
var requestedProps = getRequestedProps(xmlBody);

expect(requestedProps.length).toEqual(2);
expect(requestedProps[0].nodeName).toEqual('d:getetag');
expect(requestedProps[0].textContent).toEqual('"abc"');
expect(requestedProps[1].nodeName).toEqual('e:someprop');
expect(requestedProps[1].textContent).toEqual('newvalue');

requestStub.restore();
});

it('returns body', function() {
var requestStub = sinon.stub(dav.Client.prototype, 'request');
var thenStub = sinon.stub();
requestStub.returns({
then: thenStub
});

client.propPatch(
'/rel/path',
[],
{},
{
'{DAV:}getetag': '"abc"',
'{http://example.org/ns}someprop': 'newvalue'
}
);

expect(requestStub.calledOnce).toEqual(true);
expect(thenStub.calledOnce).toEqual(true);

var result = thenStub.getCall(0).args[0].call(null, {
status: 207,
body: 'response',
xhr: 'dummyxhr'
});

expect(result).toEqual({
status: 207,
body: 'response',
xhr: 'dummyxhr'
});
requestStub.restore();
});
});
Expand Down

0 comments on commit 2fd4f11

Please sign in to comment.