@@ -46,6 +46,95 @@ describe("client", function() {
4646 resolver = null ;
4747 } ) ;
4848
49+ function getRequestedProps ( xmlBody , path ) {
50+ var doc = parser . parseFromString ( xmlBody , "application/xml" ) ;
51+
52+ var requestedProps = [ ] ;
53+ var propsIterator = doc . evaluate ( path , doc , resolver , XPathResult . ANY_TYPE , null ) ;
54+ var propNode ;
55+ while ( propNode = propsIterator . iterateNext ( ) ) {
56+ requestedProps . push ( propNode ) ;
57+ }
58+
59+ return requestedProps ;
60+ }
61+
62+ function testPropSet ( clientMethod , httpMethod , xmlPath ) {
63+ it ( 'sends given properties as XML body' , function ( ) {
64+ var requestStub = sinon . stub ( dav . Client . prototype , 'request' ) ;
65+ var thenStub = sinon . stub ( ) ;
66+ requestStub . returns ( {
67+ then : thenStub
68+ } ) ;
69+ client [ clientMethod ] (
70+ '/rel/path' ,
71+ {
72+ '{DAV:}getetag' : '"abc"' ,
73+ '{http://example.org/ns}someprop' : 'newvalue'
74+ } ,
75+ {
76+ 'CustomHeader' : 'Value' ,
77+ 'CustomHeader2' : 'Value2'
78+ }
79+ ) ;
80+
81+ expect ( requestStub . calledOnce ) . toEqual ( true ) ;
82+ expect ( requestStub . getCall ( 0 ) . args [ 0 ] ) . toEqual ( httpMethod ) ;
83+ expect ( requestStub . getCall ( 0 ) . args [ 1 ] ) . toEqual ( '/rel/path' ) ;
84+ expect ( requestStub . getCall ( 0 ) . args [ 2 ] ) . toEqual ( {
85+ 'Content-Type' : 'application/xml; charset=utf-8' ,
86+ 'CustomHeader' : 'Value' ,
87+ 'CustomHeader2' : 'Value2'
88+ } ) ;
89+
90+ var xmlBody = requestStub . getCall ( 0 ) . args [ 3 ] ;
91+ expect ( xmlBody ) . toBeDefined ( ) ;
92+ var requestedProps = getRequestedProps ( xmlBody , xmlPath ) ;
93+
94+ expect ( requestedProps . length ) . toEqual ( 2 ) ;
95+ expect ( requestedProps [ 0 ] . nodeName ) . toEqual ( 'd:getetag' ) ;
96+ expect ( requestedProps [ 0 ] . textContent ) . toEqual ( '"abc"' ) ;
97+ expect ( requestedProps [ 1 ] . nodeName ) . toEqual ( 'e:someprop' ) ;
98+ expect ( requestedProps [ 1 ] . textContent ) . toEqual ( 'newvalue' ) ;
99+
100+ requestStub . restore ( ) ;
101+ } ) ;
102+
103+ it ( 'returns body' , function ( ) {
104+ var requestStub = sinon . stub ( dav . Client . prototype , 'request' ) ;
105+ var thenStub = sinon . stub ( ) ;
106+ requestStub . returns ( {
107+ then : thenStub
108+ } ) ;
109+
110+ client [ clientMethod ] (
111+ '/rel/path' ,
112+ [ ] ,
113+ { } ,
114+ {
115+ '{DAV:}getetag' : '"abc"' ,
116+ '{http://example.org/ns}someprop' : 'newvalue'
117+ }
118+ ) ;
119+
120+ expect ( requestStub . calledOnce ) . toEqual ( true ) ;
121+ expect ( thenStub . calledOnce ) . toEqual ( true ) ;
122+
123+ var result = thenStub . getCall ( 0 ) . args [ 0 ] . call ( null , {
124+ status : 207 ,
125+ body : 'response' ,
126+ xhr : 'dummyxhr'
127+ } ) ;
128+
129+ expect ( result ) . toEqual ( {
130+ status : 207 ,
131+ body : 'response' ,
132+ xhr : 'dummyxhr'
133+ } ) ;
134+ requestStub . restore ( ) ;
135+ } ) ;
136+ } ;
137+
49138 describe ( 'request' , function ( ) {
50139 it ( 'sends the given request' , function ( ) {
51140
@@ -270,22 +359,6 @@ describe("client", function() {
270359 } ) ;
271360
272361 describe ( 'PROPFIND' , function ( ) {
273- /**
274- * Returns requested properties from a propfind body
275- */
276- function getRequestedProps ( xmlBody ) {
277- var doc = parser . parseFromString ( xmlBody , "application/xml" ) ;
278-
279- var requestedProps = [ ] ;
280- var propsIterator = doc . evaluate ( '/d:propfind/d:prop/*' , doc , resolver , XPathResult . ANY_TYPE , null ) ;
281- var propNode ;
282- while ( propNode = propsIterator . iterateNext ( ) ) {
283- requestedProps . push ( propNode ) ;
284- }
285-
286- return requestedProps ;
287- }
288-
289362 it ( 'submits the given properties as XML' , function ( ) {
290363 var requestStub = sinon . stub ( dav . Client . prototype , 'request' ) ;
291364 var thenStub = sinon . stub ( ) ;
@@ -318,7 +391,7 @@ describe("client", function() {
318391
319392 var xmlBody = requestStub . getCall ( 0 ) . args [ 3 ] ;
320393 expect ( xmlBody ) . toBeDefined ( ) ;
321- var requestedProps = getRequestedProps ( xmlBody ) ;
394+ var requestedProps = getRequestedProps ( xmlBody , '/d:propfind/d:prop/*' ) ;
322395
323396 expect ( requestedProps . length ) . toEqual ( 3 ) ;
324397 expect ( requestedProps [ 0 ] . nodeName ) . toEqual ( 'd:getetag' ) ;
@@ -368,90 +441,31 @@ describe("client", function() {
368441 } ) ;
369442
370443 describe ( 'PROPPATCH' , function ( ) {
371- function getRequestedProps ( xmlBody ) {
372- var doc = parser . parseFromString ( xmlBody , "application/xml" ) ;
373-
374- var requestedProps = [ ] ;
375- var propsIterator = doc . evaluate ( '/d:propertyupdate/d:set/d:prop/*' , doc , resolver , XPathResult . ANY_TYPE , null ) ;
376- var propNode ;
377- while ( propNode = propsIterator . iterateNext ( ) ) {
378- requestedProps . push ( propNode ) ;
379- }
444+ testPropSet ( 'propPatch' , 'PROPPATCH' , '/d:propertyupdate/d:set/d:prop/*' ) ;
445+ } ) ;
380446
381- return requestedProps ;
382- }
447+ describe ( 'MKCOL' , function ( ) {
448+ testPropSet ( 'mkcol' , 'MKCOL' , '/d:mkcol/d:set/d:prop/*' ) ;
383449
384- it ( 'sends given properties as XML body' , function ( ) {
450+ it ( 'does not send any body when no properties given ' , function ( ) {
385451 var requestStub = sinon . stub ( dav . Client . prototype , 'request' ) ;
386452 var thenStub = sinon . stub ( ) ;
387453 requestStub . returns ( {
388454 then : thenStub
389455 } ) ;
390- client . propPatch (
456+ client . mkcol (
391457 '/rel/path' ,
392- {
393- '{DAV:}getetag' : '"abc"' ,
394- '{http://example.org/ns}someprop' : 'newvalue'
395- } ,
396- {
397- 'CustomHeader' : 'Value' ,
398- 'CustomHeader2' : 'Value2'
399- }
458+ null
400459 ) ;
401460
402461 expect ( requestStub . calledOnce ) . toEqual ( true ) ;
403- expect ( requestStub . getCall ( 0 ) . args [ 0 ] ) . toEqual ( 'PROPPATCH ' ) ;
462+ expect ( requestStub . getCall ( 0 ) . args [ 0 ] ) . toEqual ( 'MKCOL ' ) ;
404463 expect ( requestStub . getCall ( 0 ) . args [ 1 ] ) . toEqual ( '/rel/path' ) ;
405464 expect ( requestStub . getCall ( 0 ) . args [ 2 ] ) . toEqual ( {
406- 'Content-Type' : 'application/xml; charset=utf-8' ,
407- 'CustomHeader' : 'Value' ,
408- 'CustomHeader2' : 'Value2'
465+ 'Content-Type' : 'application/xml; charset=utf-8'
409466 } ) ;
467+ expect ( requestStub . getCall ( 0 ) . args [ 3 ] ) . toEqual ( '' ) ;
410468
411- var xmlBody = requestStub . getCall ( 0 ) . args [ 3 ] ;
412- expect ( xmlBody ) . toBeDefined ( ) ;
413- var requestedProps = getRequestedProps ( xmlBody ) ;
414-
415- expect ( requestedProps . length ) . toEqual ( 2 ) ;
416- expect ( requestedProps [ 0 ] . nodeName ) . toEqual ( 'd:getetag' ) ;
417- expect ( requestedProps [ 0 ] . textContent ) . toEqual ( '"abc"' ) ;
418- expect ( requestedProps [ 1 ] . nodeName ) . toEqual ( 'e:someprop' ) ;
419- expect ( requestedProps [ 1 ] . textContent ) . toEqual ( 'newvalue' ) ;
420-
421- requestStub . restore ( ) ;
422- } ) ;
423-
424- it ( 'returns body' , function ( ) {
425- var requestStub = sinon . stub ( dav . Client . prototype , 'request' ) ;
426- var thenStub = sinon . stub ( ) ;
427- requestStub . returns ( {
428- then : thenStub
429- } ) ;
430-
431- client . propPatch (
432- '/rel/path' ,
433- [ ] ,
434- { } ,
435- {
436- '{DAV:}getetag' : '"abc"' ,
437- '{http://example.org/ns}someprop' : 'newvalue'
438- }
439- ) ;
440-
441- expect ( requestStub . calledOnce ) . toEqual ( true ) ;
442- expect ( thenStub . calledOnce ) . toEqual ( true ) ;
443-
444- var result = thenStub . getCall ( 0 ) . args [ 0 ] . call ( null , {
445- status : 207 ,
446- body : 'response' ,
447- xhr : 'dummyxhr'
448- } ) ;
449-
450- expect ( result ) . toEqual ( {
451- status : 207 ,
452- body : 'response' ,
453- xhr : 'dummyxhr'
454- } ) ;
455469 requestStub . restore ( ) ;
456470 } ) ;
457471 } ) ;
0 commit comments