1
+ "use strict" ;
2
+ var webdav = require ( '../../lib/index.js' ) ,
3
+ request = require ( 'request' )
4
+
5
+ module . exports = ( test , options , index ) => test ( 'move a virtual resource (overwrite test)' , ( isValid , server ) =>
6
+ {
7
+ isValid = isValid . multiple ( 2 , server ) ;
8
+ const _ = ( e , cb ) => {
9
+ if ( e )
10
+ isValid ( false , e ) ;
11
+ else
12
+ cb ( ) ;
13
+ }
14
+
15
+ function move ( source , dest , overwrite , callback )
16
+ {
17
+ request ( {
18
+ url : 'http://localhost:' + ( options . port + index ) + source ,
19
+ method : 'MOVE' ,
20
+ headers : {
21
+ Destination : 'http://localhost:' + ( options . port + index ) + dest ,
22
+ Overwrite : overwrite ? 'T' : 'F'
23
+ }
24
+ } , ( e , res , body ) => _ ( e , ( ) => {
25
+ callback ( res . statusCode < 300 ) ;
26
+ } ) )
27
+ }
28
+
29
+ function exist ( path , callback )
30
+ {
31
+ request ( {
32
+ url : 'http://localhost:' + ( options . port + index ) + path ,
33
+ method : 'PROPFIND'
34
+ } , ( e , res , body ) => _ ( e , ( ) => {
35
+ callback ( res . statusCode < 300 ) ;
36
+ } ) )
37
+ }
38
+
39
+ const fileName = 'file.txt' ;
40
+ const fileNameDest = 'file.dest.txt' ;
41
+ server . addResourceTree ( [
42
+ new webdav . VirtualFile ( fileName ) ,
43
+ new webdav . VirtualFile ( fileNameDest ) ,
44
+ ] , e => _ ( e , ( ) => {
45
+ move ( '/' + fileName , '/' + fileNameDest , false , ( moved ) => {
46
+ if ( moved )
47
+ {
48
+ isValid ( false , 'Must not overwrite a file resource when not allowed in the headers.' ) ;
49
+ return ;
50
+ }
51
+
52
+ exist ( '/' + fileName , ( exists ) => {
53
+ if ( ! exists )
54
+ {
55
+ isValid ( false , 'The file must still exist after a failed move' ) ;
56
+ return ;
57
+ }
58
+
59
+ exist ( '/' + fileNameDest , ( exists ) => {
60
+ isValid ( exists , 'The destination file must still exist after a failed move' ) ;
61
+ } )
62
+ } )
63
+ } )
64
+ } ) ) ;
65
+
66
+ const fileName2 = 'file2.txt' ;
67
+ const fileNameDest2 = 'file2.dest.txt' ;
68
+ server . addResourceTree ( [
69
+ new webdav . VirtualFile ( fileName2 ) ,
70
+ new webdav . VirtualFile ( fileNameDest2 ) ,
71
+ ] , e => _ ( e , ( ) => {
72
+ move ( '/' + fileName2 , '/' + fileNameDest2 , true , ( moved ) => {
73
+ if ( ! moved )
74
+ {
75
+ isValid ( false , 'Must overwrite a file resource when allowed in the headers.' ) ;
76
+ return ;
77
+ }
78
+
79
+ exist ( '/' + fileName2 , ( exists ) => {
80
+ if ( exists )
81
+ {
82
+ isValid ( false , 'The file must have been moved, but still exists at its old url' ) ;
83
+ return ;
84
+ }
85
+
86
+ exist ( '/' + fileNameDest2 , ( exists ) => {
87
+ isValid ( exists , 'The destination file must be replaced by the source resource' ) ;
88
+ } )
89
+ } )
90
+ } )
91
+ } ) ) ;
92
+ } )
0 commit comments