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 expect(open).toHaveBeenCalledWith('PROPFIND', 'http://foo.com/foo');
15 expect(setRequestHeader).toHaveBeenCalledWith('Authorization', 'Basic');
16 expect(setRequestHeader).toHaveBeenCalledWith('Cache-Control', 'no-cache');
17 expect(request).toBeInstanceOf(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 expect(open).toHaveBeenCalledWith('PROPFIND', 'http://foo.com/foo');
28 expect(setRequestHeader).toHaveBeenCalledWith('Authorization', 'Basic');
29 expect(setRequestHeader).toHaveBeenCalledWith('Cache-Control', 'no-cache');
30 expect(request).toBeInstanceOf(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 expect(open).toHaveBeenCalledWith('PROPFIND', 'foo');
40 expect(setRequestHeader).toHaveBeenCalledWith('Cache-Control', 'no-cache');
41 expect(request).toBeInstanceOf(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 expect(open).toHaveBeenCalledWith('PUT', 'foo');
52 expect(send).toHaveBeenCalledWith('Test data');
53 expect(setRequestHeader).toHaveBeenCalledWith('Cache-Control', 'no-cache');
54 expect(request).toBeInstanceOf(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 expect(open).toHaveBeenCalledWith('COPY', 'http://base/foo');
64 expect(setRequestHeader).toHaveBeenCalledWith('Destination', 'http://base/foo-copy');
65 expect(setRequestHeader).toHaveBeenCalledWith('Cache-Control', 'no-cache');
66 expect(request).toBeInstanceOf(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 expect(open).toHaveBeenCalledWith('COPY', 'http://base/foo');
76 expect(setRequestHeader).toHaveBeenCalledWith('Destination', 'http://base/foo-copy');
77 expect(setRequestHeader).toHaveBeenCalledWith('Cache-Control', 'no-cache');
78 expect(request).toBeInstanceOf(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 expect(open).toHaveBeenCalledWith('COPY', 'http://base/foo');
88 expect(setRequestHeader).toHaveBeenCalledWith('Destination', 'http://base/foo-copy');
89 expect(setRequestHeader).toHaveBeenCalledWith('Cache-Control', 'no-cache');
90 expect(request).toBeInstanceOf(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 expect(open).toHaveBeenCalledWith('MOVE', 'http://base/foo');
100 expect(setRequestHeader).toHaveBeenCalledWith('Destination', 'http://base/foo-moved');
101 expect(setRequestHeader).toHaveBeenCalledWith('Cache-Control', 'no-cache');
102 expect(request).toBeInstanceOf(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 expect(open).toHaveBeenCalledWith('MOVE', 'http://base/foo');
112 expect(setRequestHeader).toHaveBeenCalledWith('Destination', 'http://base/foo-moved');
113 expect(setRequestHeader).toHaveBeenCalledWith('Cache-Control', 'no-cache');
114 expect(request).toBeInstanceOf(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 expect(open).toHaveBeenCalledWith('MOVE', 'http://base/foo');
124 expect(setRequestHeader).toHaveBeenCalledWith('Destination', 'http://base/foo-moved');
125 expect(setRequestHeader).toHaveBeenCalledWith('Cache-Control', 'no-cache');
126 expect(request).toBeInstanceOf(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 expect(open).toHaveBeenCalledWith('DELETE', 'foo');
136 expect(setRequestHeader).toHaveBeenCalledWith('Cache-Control', 'no-cache');
137 expect(request).toBeInstanceOf(XMLHttpRequest);
141 const mockCreateRequest = () => {
142 const send = jest.fn();
143 const open = jest.fn();
144 const setRequestHeader = jest.fn();
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