1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { WebDAV } from "./webdav";
7 describe('WebDAV', () => {
8 it('makes use of provided config', async () => {
9 const { open, load, setRequestHeader, createRequest } = mockCreateRequest();
10 const webdav = new WebDAV({ baseURL: 'http://foo.com/', headers: { Authorization: 'Basic' } }, createRequest);
11 const promise = webdav.propfind('foo');
13 const request = await promise;
14 cy.get('@open').should('have.been.calledWith', 'PROPFIND', 'http://foo.com/foo');
15 cy.get('@setRequestHeader').should('have.been.calledWith', 'Authorization', 'Basic');
16 cy.get('@setRequestHeader').should('have.been.calledWith', 'Cache-Control', 'no-cache');
17 expect(request).to.be.instanceOf(XMLHttpRequest);
20 it('allows to modify defaults after instantiation', async () => {
21 const { open, load, setRequestHeader, createRequest } = mockCreateRequest();
22 const webdav = new WebDAV({ baseURL: 'http://foo.com/' }, createRequest);
23 webdav.setAuthorization('Basic');
24 const promise = webdav.propfind('foo');
26 const request = await promise;
27 cy.get('@open').should('have.been.calledWith', 'PROPFIND', 'http://foo.com/foo');
28 cy.get('@setRequestHeader').should('have.been.calledWith', 'Authorization', 'Basic');
29 cy.get('@setRequestHeader').should('have.been.calledWith', 'Cache-Control', 'no-cache');
30 expect(request).to.be.instanceOf(XMLHttpRequest);
33 it('PROPFIND', async () => {
34 const { open, load, setRequestHeader, createRequest } = mockCreateRequest();
35 const webdav = new WebDAV(undefined, createRequest);
36 const promise = webdav.propfind('foo');
38 const request = await promise;
39 cy.get('@open').should('have.been.calledWith', 'PROPFIND', 'foo');
40 cy.get('@setRequestHeader').should('have.been.calledWith', 'Cache-Control', 'no-cache');
41 expect(request).to.be.instanceOf(XMLHttpRequest);
44 it('PUT', async () => {
45 const { open, send, load, progress, setRequestHeader, createRequest } = mockCreateRequest();
46 const webdav = new WebDAV(undefined, createRequest);
47 const promise = webdav.put('foo', 'Test data');
50 const request = await promise;
51 cy.get('@open').should('have.been.calledWith', 'PUT', 'foo');
52 cy.get('@send').should('have.been.calledWith', 'Test data');
53 cy.get('@setRequestHeader').should('have.been.calledWith', 'Cache-Control', 'no-cache');
54 expect(request).to.be.instanceOf(XMLHttpRequest);
57 it('COPY', async () => {
58 const { open, setRequestHeader, load, createRequest } = mockCreateRequest();
59 const webdav = new WebDAV({ baseURL: 'http://base' }, createRequest);
60 const promise = webdav.copy('foo', 'foo-copy');
62 const request = await promise;
63 cy.get('@open').should('have.been.calledWith', 'COPY', 'http://base/foo');
64 cy.get('@setRequestHeader').should('have.been.calledWith', 'Destination', 'http://base/foo-copy');
65 cy.get('@setRequestHeader').should('have.been.calledWith', 'Cache-Control', 'no-cache');
66 expect(request).to.be.instanceOf(XMLHttpRequest);
69 it('COPY - adds baseURL with trailing slash to Destination header', async () => {
70 const { open, setRequestHeader, load, createRequest } = mockCreateRequest();
71 const webdav = new WebDAV({ baseURL: 'http://base' }, createRequest);
72 const promise = webdav.copy('foo', 'foo-copy');
74 const request = await promise;
75 cy.get('@open').should('have.been.calledWith', 'COPY', 'http://base/foo');
76 cy.get('@setRequestHeader').should('have.been.calledWith', 'Destination', 'http://base/foo-copy');
77 cy.get('@setRequestHeader').should('have.been.calledWith', 'Cache-Control', 'no-cache');
78 expect(request).to.be.instanceOf(XMLHttpRequest);
81 it('COPY - adds baseURL without trailing slash to Destination header', async () => {
82 const { open, setRequestHeader, load, createRequest } = mockCreateRequest();
83 const webdav = new WebDAV({ baseURL: 'http://base' }, createRequest);
84 const promise = webdav.copy('foo', 'foo-copy');
86 const request = await promise;
87 cy.get('@open').should('have.been.calledWith', 'COPY', 'http://base/foo');
88 cy.get('@setRequestHeader').should('have.been.calledWith', 'Destination', 'http://base/foo-copy');
89 cy.get('@setRequestHeader').should('have.been.calledWith', 'Cache-Control', 'no-cache');
90 expect(request).to.be.instanceOf(XMLHttpRequest);
93 it('MOVE', async () => {
94 const { open, setRequestHeader, load, createRequest } = mockCreateRequest();
95 const webdav = new WebDAV({ baseURL: 'http://base' }, createRequest);
96 const promise = webdav.move('foo', 'foo-moved');
98 const request = await promise;
99 cy.get('@open').should('have.been.calledWith', 'MOVE', 'http://base/foo');
100 cy.get('@setRequestHeader').should('have.been.calledWith', 'Destination', 'http://base/foo-moved');
101 cy.get('@setRequestHeader').should('have.been.calledWith', 'Cache-Control', 'no-cache');
102 expect(request).to.be.instanceOf(XMLHttpRequest);
105 it('MOVE - adds baseURL with trailing slash to Destination header', async () => {
106 const { open, setRequestHeader, load, createRequest } = mockCreateRequest();
107 const webdav = new WebDAV({ baseURL: 'http://base' }, createRequest);
108 const promise = webdav.move('foo', 'foo-moved');
110 const request = await promise;
111 cy.get('@open').should('have.been.calledWith', 'MOVE', 'http://base/foo');
112 cy.get('@setRequestHeader').should('have.been.calledWith', 'Destination', 'http://base/foo-moved');
113 cy.get('@setRequestHeader').should('have.been.calledWith', 'Cache-Control', 'no-cache');
114 expect(request).to.be.instanceOf(XMLHttpRequest);
117 it('MOVE - adds baseURL without trailing slash to Destination header', async () => {
118 const { open, setRequestHeader, load, createRequest } = mockCreateRequest();
119 const webdav = new WebDAV({ baseURL: 'http://base' }, createRequest);
120 const promise = webdav.move('foo', 'foo-moved');
122 const request = await promise;
123 cy.get('@open').should('have.been.calledWith', 'MOVE', 'http://base/foo');
124 cy.get('@setRequestHeader').should('have.been.calledWith', 'Destination', 'http://base/foo-moved');
125 cy.get('@setRequestHeader').should('have.been.calledWith', 'Cache-Control', 'no-cache');
126 expect(request).to.be.instanceOf(XMLHttpRequest);
129 it('DELETE', async () => {
130 const { open, load, setRequestHeader, createRequest } = mockCreateRequest();
131 const webdav = new WebDAV(undefined, createRequest);
132 const promise = webdav.delete('foo');
134 const request = await promise;
135 cy.get('@open').should('have.been.calledWith', 'DELETE', 'foo');
136 cy.get('@setRequestHeader').should('have.been.calledWith', 'Cache-Control', 'no-cache');
137 expect(request).to.be.instanceOf(XMLHttpRequest);
141 const mockCreateRequest = () => {
142 const send = cy.stub().as('send');
143 const open = cy.stub().as('open');
144 const setRequestHeader = cy.stub().as('setRequestHeader');
145 const request = new XMLHttpRequest();
148 request.setRequestHeader = setRequestHeader;
149 const load = () => request.dispatchEvent(new Event('load'));
150 const progress = () => request.dispatchEvent(new Event('progress'));
157 createRequest: () => request