DAV.test.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /**
  2. * @jest-environment jsdom
  3. */
  4. import Collection from '../../src/lib/Collection';
  5. import DAV, { RequestCache } from '../../src/lib/DAV';
  6. import { DOMParser } from '@xmldom/xmldom';
  7. import Entry from '../../src/lib/Entry';
  8. import HTTP from '../../src/lib/HTTP';
  9. describe('DAV', () => {
  10. const getSpies = (
  11. SpyHTTPReturns = {},
  12. SpyCacheReturns = {}
  13. ): [HTTP, RequestCache, Map<string, string | Collection>] => {
  14. const SpyHTTP = new HTTP(),
  15. SpyCache = new Map();
  16. [
  17. 'GET',
  18. 'HEAD',
  19. 'PUT',
  20. 'PROPFIND',
  21. 'DELETE',
  22. 'MKCOL',
  23. 'COPY',
  24. 'MOVE',
  25. ].forEach(
  26. (methodName) =>
  27. (SpyHTTP[methodName] = jest.fn(
  28. () =>
  29. new Promise((resolve) =>
  30. resolve(
  31. SpyHTTPReturns[methodName] ?? {
  32. ok: true,
  33. status: 200,
  34. async text(): Promise<string> {
  35. return '';
  36. },
  37. }
  38. )
  39. )
  40. ))
  41. );
  42. ['delete', 'get', 'has', 'set'].forEach(
  43. (methodName) =>
  44. (SpyCache[methodName] = jest.fn(
  45. () => SpyCacheReturns[methodName] ?? null
  46. ))
  47. );
  48. const cache = new Map();
  49. cache.set('GET', SpyCache);
  50. cache.set('PROPFIND', SpyCache);
  51. return [SpyHTTP, cache, SpyCache];
  52. };
  53. if (typeof window === 'undefined') {
  54. global.location = {
  55. ...global.location,
  56. protocol: 'http:',
  57. host: 'localhost',
  58. hostname: 'localhost',
  59. };
  60. global.DOMParser = DOMParser;
  61. }
  62. it('should fire a HEAD request on check', () => {
  63. const [SpyHTTP, SpyCache] = getSpies(),
  64. dav = new DAV({}, SpyCache, SpyHTTP);
  65. dav.check('/checkHeadRequest');
  66. expect(SpyHTTP.HEAD).toHaveBeenCalledWith('/checkHeadRequest');
  67. });
  68. it('should fire a COPY request on copy', () => {
  69. const [SpyHTTP, SpyCache] = getSpies(),
  70. dav = new DAV({}, SpyCache, SpyHTTP);
  71. dav.copy(
  72. '/copySource',
  73. '/copyDestination',
  74. new Entry({
  75. fullPath: '/copySource',
  76. })
  77. );
  78. expect(SpyHTTP.COPY).toHaveBeenCalledWith('/copySource', {
  79. headers: {
  80. Destination: `${location.protocol}//${location.hostname}${
  81. location.port ? `:${location.port}` : ''
  82. }/copyDestination`,
  83. Overwrite: 'F',
  84. },
  85. });
  86. });
  87. it('should fire a DELETE request on del', () => {
  88. const [SpyHTTP, SpyCache] = getSpies(),
  89. dav = new DAV({}, SpyCache, SpyHTTP);
  90. dav.del('/checkDeleteRequest');
  91. expect(SpyHTTP.DELETE).toHaveBeenCalledWith('/checkDeleteRequest', {
  92. headers: { Depth: 'infinity' },
  93. });
  94. });
  95. it('should fire a GET request on get', () => {
  96. const [SpyHTTP, SpyCache] = getSpies(),
  97. dav = new DAV({}, SpyCache, SpyHTTP);
  98. dav.get('/checkGetRequest');
  99. expect(SpyHTTP.GET).toHaveBeenCalledWith('/checkGetRequest');
  100. });
  101. it('should fire a PROPFIND request and store cache on list', async () => {
  102. const [SpyHTTP, cache, SpyCache] = getSpies(
  103. {
  104. HEAD: {
  105. ok: true,
  106. },
  107. PROPFIND: {
  108. text: () =>
  109. '<?xml version="1.0" encoding="utf-8"?><D:multistatus xmlns:D="DAV:"><D:response xmlns:lp1="DAV:" xmlns:lp2="http://apache.org/dav/props/"><D:href>/Directory%20name/</D:href><D:propstat><D:prop><lp1:resourcetype><D:collection/></lp1:resourcetype><lp1:creationdate>2019-11-06T16:29:46Z</lp1:creationdate><lp1:getlastmodified>Wed, 06 Nov 2019 16:29:46 GMT</lp1:getlastmodified><lp1:getetag>"6-596b00e926ba3"</lp1:getetag><D:supportedlock><D:lockentry><D:lockscope><D:exclusive/></D:lockscope><D:locktype><D:write/></D:locktype></D:lockentry><D:lockentry><D:lockscope><D:shared/></D:lockscope><D:locktype><D:write/></D:locktype></D:lockentry></D:supportedlock><D:lockdiscovery/><D:getcontenttype>httpd/unix-directory</D:getcontenttype></D:prop><D:status>HTTP/1.1 200 OK</D:status></D:propstat></D:response></D:multistatus>',
  110. },
  111. },
  112. {
  113. get: false,
  114. }
  115. ),
  116. dav = new DAV({}, cache, SpyHTTP),
  117. collection = await dav.list('/checkPropfindRequest');
  118. expect(SpyCache.has).toHaveBeenCalledWith('/checkPropfindRequest/');
  119. expect(SpyHTTP.HEAD).toHaveBeenCalledWith('/checkPropfindRequest/');
  120. expect(SpyHTTP.PROPFIND).toHaveBeenCalledWith('/checkPropfindRequest/');
  121. expect(collection).toBeInstanceOf(Collection);
  122. expect(SpyCache.set).toHaveBeenCalledWith(
  123. '/checkPropfindRequest/',
  124. collection
  125. );
  126. });
  127. it('should fire an MKCOL request on mkcol', () => {
  128. const [SpyHTTP, SpyCache] = getSpies(),
  129. dav = new DAV({}, SpyCache, SpyHTTP);
  130. 0;
  131. dav.createDirectory('/checkMkcolRequest');
  132. expect(SpyHTTP.MKCOL).toHaveBeenCalledWith('/checkMkcolRequest');
  133. });
  134. it('should fire a MOVE request on move', () => {
  135. const [SpyHTTP, SpyCache] = getSpies(),
  136. dav = new DAV({}, SpyCache, SpyHTTP);
  137. dav.move(
  138. '/moveSource',
  139. '/moveDestination',
  140. new Entry({
  141. fullPath: '/moveSource',
  142. directory: false,
  143. })
  144. );
  145. expect(SpyHTTP.MOVE).toHaveBeenCalledWith('/moveSource', {
  146. headers: {
  147. Destination: `${location.protocol}//${location.hostname}${
  148. location.port ? `:${location.port}` : ''
  149. }/moveDestination`,
  150. Overwrite: 'F',
  151. },
  152. });
  153. });
  154. it('should not fire a HEAD request on list when `bypassCheck` is set', async () => {
  155. const [SpyHTTP, SpyCache] = getSpies(
  156. {
  157. HEAD: {
  158. ok: true,
  159. },
  160. PROPFIND: {
  161. text: () =>
  162. '<?xml version="1.0" encoding="utf-8"?><D:multistatus xmlns:D="DAV:"><D:response xmlns:lp1="DAV:" xmlns:lp2="http://apache.org/dav/props/"><D:href>/Directory%20name/</D:href><D:propstat><D:prop><lp1:resourcetype><D:collection/></lp1:resourcetype><lp1:creationdate>2019-11-06T16:29:46Z</lp1:creationdate><lp1:getlastmodified>Wed, 06 Nov 2019 16:29:46 GMT</lp1:getlastmodified><lp1:getetag>"6-596b00e926ba3"</lp1:getetag><D:supportedlock><D:lockentry><D:lockscope><D:exclusive/></D:lockscope><D:locktype><D:write/></D:locktype></D:lockentry><D:lockentry><D:lockscope><D:shared/></D:lockscope><D:locktype><D:write/></D:locktype></D:lockentry></D:supportedlock><D:lockdiscovery/><D:getcontenttype>httpd/unix-directory</D:getcontenttype></D:prop><D:status>HTTP/1.1 200 OK</D:status></D:propstat></D:response></D:multistatus>',
  163. },
  164. },
  165. {
  166. get: false,
  167. }
  168. ),
  169. dav = new DAV(
  170. {
  171. bypassCheck: true,
  172. },
  173. SpyCache,
  174. SpyHTTP
  175. );
  176. await dav.list('/checkPropfindRequest');
  177. expect(SpyHTTP.HEAD).not.toHaveBeenCalledWith('/checkPropfindRequest/');
  178. });
  179. });