123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- /**
- * @jest-environment jsdom
- */
- import Collection from '../../src/lib/Collection';
- import DAV, { RequestCache } from '../../src/lib/DAV';
- import { DOMParser } from '@xmldom/xmldom';
- import Entry from '../../src/lib/Entry';
- import HTTP from '../../src/lib/HTTP';
- describe('DAV', () => {
- const getSpies = (
- SpyHTTPReturns = {},
- SpyCacheReturns = {}
- ): [HTTP, RequestCache, Map<string, string | Collection>] => {
- const SpyHTTP = new HTTP(),
- SpyCache = new Map();
- [
- 'GET',
- 'HEAD',
- 'PUT',
- 'PROPFIND',
- 'DELETE',
- 'MKCOL',
- 'COPY',
- 'MOVE',
- ].forEach(
- (methodName) =>
- (SpyHTTP[methodName] = jest.fn(
- () =>
- new Promise((resolve) =>
- resolve(SpyHTTPReturns[methodName] ?? null)
- )
- ))
- );
- ['delete', 'get', 'has', 'set'].forEach(
- (methodName) =>
- (SpyCache[methodName] = jest.fn(
- () => SpyCacheReturns[methodName] ?? null
- ))
- );
- const cache = new Map();
- cache.set('GET', SpyCache);
- cache.set('PROPFIND', SpyCache);
- return [SpyHTTP, cache, SpyCache];
- };
- if (typeof window === 'undefined') {
- global.location = {
- ...global.location,
- protocol: 'http:',
- host: 'localhost',
- hostname: 'localhost',
- };
- global.DOMParser = DOMParser;
- }
- it('should fire a HEAD request on check', () => {
- const [SpyHTTP, SpyCache] = getSpies(),
- dav = new DAV({}, SpyCache, SpyHTTP);
- dav.check('/checkHeadRequest');
- expect(SpyHTTP.HEAD).toHaveBeenCalledWith('/checkHeadRequest');
- });
- it('should fire a COPY request on copy', () => {
- const [SpyHTTP, SpyCache] = getSpies(),
- dav = new DAV({}, SpyCache, SpyHTTP);
- dav.copy('/copySource', '/copyDestination');
- expect(SpyHTTP.COPY).toHaveBeenCalledWith('/copySource', {
- headers: {
- Destination: `${location.protocol}//${location.hostname}${
- location.port ? `:${location.port}` : ''
- }/copyDestination`,
- },
- });
- });
- it('should fire a DELETE request on del', () => {
- const [SpyHTTP, SpyCache] = getSpies(),
- dav = new DAV({}, SpyCache, SpyHTTP);
- dav.del('/checkDeleteRequest');
- expect(SpyHTTP.DELETE).toHaveBeenCalledWith('/checkDeleteRequest');
- });
- it('should fire a GET request on get', () => {
- const [SpyHTTP, SpyCache] = getSpies(),
- dav = new DAV({}, SpyCache, SpyHTTP);
- dav.get('/checkGetRequest');
- expect(SpyHTTP.GET).toHaveBeenCalledWith('/checkGetRequest');
- });
- it('should fire a PROPFIND request and store cache on list', async () => {
- const [SpyHTTP, cache, SpyCache] = getSpies(
- {
- HEAD: {
- ok: true,
- },
- PROPFIND: {
- text: () =>
- '<?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>',
- },
- },
- {
- get: false,
- }
- ),
- dav = new DAV({}, cache, SpyHTTP),
- collection = await dav.list('/checkPropfindRequest');
- expect(SpyCache.has).toHaveBeenCalledWith('/checkPropfindRequest/');
- expect(SpyHTTP.HEAD).toHaveBeenCalledWith('/checkPropfindRequest/');
- expect(SpyHTTP.PROPFIND).toHaveBeenCalledWith('/checkPropfindRequest/');
- expect(collection).toBeInstanceOf(Collection);
- expect(SpyCache.set).toHaveBeenCalledWith(
- '/checkPropfindRequest/',
- collection
- );
- });
- it('should fire an MKCOL request on mkcol', () => {
- const [SpyHTTP, SpyCache] = getSpies(),
- dav = new DAV({}, SpyCache, SpyHTTP);
- 0;
- dav.mkcol('/checkMkcolRequest');
- expect(SpyHTTP.MKCOL).toHaveBeenCalledWith('/checkMkcolRequest');
- });
- it('should fire a MOVE request on move', () => {
- const [SpyHTTP, SpyCache] = getSpies(),
- dav = new DAV({}, SpyCache, SpyHTTP);
- dav.move(
- '/moveSource',
- '/moveDestination',
- new Entry({
- fullPath: '/moveSource',
- directory: false,
- })
- );
- expect(SpyHTTP.MOVE).toHaveBeenCalledWith('/moveSource', {
- headers: {
- Destination: `${location.protocol}//${location.hostname}${
- location.port ? `:${location.port}` : ''
- }/moveDestination`,
- },
- });
- });
- it('should not fire a HEAD request on list when `bypassCheck` is set', async () => {
- const [SpyHTTP, SpyCache] = getSpies(
- {
- HEAD: {
- ok: true,
- },
- PROPFIND: {
- text: () =>
- '<?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>',
- },
- },
- {
- get: false,
- }
- ),
- dav = new DAV(
- {
- bypassCheck: true,
- },
- SpyCache,
- SpyHTTP
- );
- await dav.list('/checkPropfindRequest');
- expect(SpyHTTP.HEAD).not.toHaveBeenCalledWith('/checkPropfindRequest/');
- });
- });
|