DAV.test.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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(SpyHTTPReturns[methodName] ?? null)
  31. )
  32. ))
  33. );
  34. ['delete', 'get', 'has', 'set'].forEach(
  35. (methodName) =>
  36. (SpyCache[methodName] = jest.fn(
  37. () => SpyCacheReturns[methodName] ?? null
  38. ))
  39. );
  40. const cache = new Map();
  41. cache.set('GET', SpyCache);
  42. cache.set('PROPFIND', SpyCache);
  43. return [SpyHTTP, cache, SpyCache];
  44. };
  45. if (typeof window === 'undefined') {
  46. global.location = {
  47. ...global.location,
  48. protocol: 'http:',
  49. host: 'localhost',
  50. hostname: 'localhost',
  51. };
  52. global.DOMParser = DOMParser;
  53. }
  54. it('should fire a HEAD request on check', () => {
  55. const [SpyHTTP, SpyCache] = getSpies(),
  56. dav = new DAV({}, SpyCache, SpyHTTP);
  57. dav.check('/checkHeadRequest');
  58. expect(SpyHTTP.HEAD).toHaveBeenCalledWith('/checkHeadRequest');
  59. });
  60. it('should fire a COPY request on copy', () => {
  61. const [SpyHTTP, SpyCache] = getSpies(),
  62. dav = new DAV({}, SpyCache, SpyHTTP);
  63. dav.copy('/copySource', '/copyDestination');
  64. expect(SpyHTTP.COPY).toHaveBeenCalledWith('/copySource', {
  65. headers: {
  66. Destination: `${location.protocol}//${location.hostname}${
  67. location.port ? `:${location.port}` : ''
  68. }/copyDestination`,
  69. },
  70. });
  71. });
  72. it('should fire a DELETE request on del', () => {
  73. const [SpyHTTP, SpyCache] = getSpies(),
  74. dav = new DAV({}, SpyCache, SpyHTTP);
  75. dav.del('/checkDeleteRequest');
  76. expect(SpyHTTP.DELETE).toHaveBeenCalledWith('/checkDeleteRequest');
  77. });
  78. it('should fire a GET request on get', () => {
  79. const [SpyHTTP, SpyCache] = getSpies(),
  80. dav = new DAV({}, SpyCache, SpyHTTP);
  81. dav.get('/checkGetRequest');
  82. expect(SpyHTTP.GET).toHaveBeenCalledWith('/checkGetRequest');
  83. });
  84. it('should fire a PROPFIND request and store cache on list', async () => {
  85. const [SpyHTTP, cache, SpyCache] = getSpies(
  86. {
  87. HEAD: {
  88. ok: true,
  89. },
  90. PROPFIND: {
  91. text: () =>
  92. '<?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>',
  93. },
  94. },
  95. {
  96. get: false,
  97. }
  98. ),
  99. dav = new DAV({}, cache, SpyHTTP),
  100. collection = await dav.list('/checkPropfindRequest');
  101. expect(SpyCache.has).toHaveBeenCalledWith('/checkPropfindRequest/');
  102. expect(SpyHTTP.HEAD).toHaveBeenCalledWith('/checkPropfindRequest/');
  103. expect(SpyHTTP.PROPFIND).toHaveBeenCalledWith('/checkPropfindRequest/');
  104. expect(collection).toBeInstanceOf(Collection);
  105. expect(SpyCache.set).toHaveBeenCalledWith(
  106. '/checkPropfindRequest/',
  107. collection
  108. );
  109. });
  110. it('should fire an MKCOL request on mkcol', () => {
  111. const [SpyHTTP, SpyCache] = getSpies(),
  112. dav = new DAV({}, SpyCache, SpyHTTP);
  113. 0;
  114. dav.mkcol('/checkMkcolRequest');
  115. expect(SpyHTTP.MKCOL).toHaveBeenCalledWith('/checkMkcolRequest');
  116. });
  117. it('should fire a MOVE request on move', () => {
  118. const [SpyHTTP, SpyCache] = getSpies(),
  119. dav = new DAV({}, SpyCache, SpyHTTP);
  120. dav.move(
  121. '/moveSource',
  122. '/moveDestination',
  123. new Entry({
  124. fullPath: '/moveSource',
  125. directory: false,
  126. })
  127. );
  128. expect(SpyHTTP.MOVE).toHaveBeenCalledWith('/moveSource', {
  129. headers: {
  130. Destination: `${location.protocol}//${location.hostname}${
  131. location.port ? `:${location.port}` : ''
  132. }/moveDestination`,
  133. },
  134. });
  135. });
  136. it('should not fire a HEAD request on list when `bypassCheck` is set', async () => {
  137. const [SpyHTTP, SpyCache] = getSpies(
  138. {
  139. HEAD: {
  140. ok: true,
  141. },
  142. PROPFIND: {
  143. text: () =>
  144. '<?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>',
  145. },
  146. },
  147. {
  148. get: false,
  149. }
  150. ),
  151. dav = new DAV(
  152. {
  153. bypassCheck: true,
  154. },
  155. SpyCache,
  156. SpyHTTP
  157. );
  158. await dav.list('/checkPropfindRequest');
  159. expect(SpyHTTP.HEAD).not.toHaveBeenCalledWith('/checkPropfindRequest/');
  160. });
  161. });