1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { customEncodeURI } from "./url";
6 import { WebDAV } from "./webdav";
8 describe('WebDAV', () => {
9 it('makes use of provided config', async () => {
10 const { open, load, setRequestHeader, createRequest } = mockCreateRequest();
11 const webdav = new WebDAV({ baseURL: 'http://foo.com/', headers: { Authorization: 'Basic' } }, createRequest);
12 const promise = webdav.propfind('foo');
14 const request = await promise;
15 expect(open).toHaveBeenCalledWith('PROPFIND', 'http://foo.com/foo');
16 expect(setRequestHeader).toHaveBeenCalledWith('Authorization', 'Basic');
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(undefined, createRequest);
23 webdav.defaults.baseURL = 'http://foo.com/';
24 webdav.defaults.headers = { Authorization: 'Basic' };
25 const promise = webdav.propfind('foo');
27 const request = await promise;
28 expect(open).toHaveBeenCalledWith('PROPFIND', 'http://foo.com/foo');
29 expect(setRequestHeader).toHaveBeenCalledWith('Authorization', 'Basic');
30 expect(request).toBeInstanceOf(XMLHttpRequest);
33 it('PROPFIND', async () => {
34 const { open, load, 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(request).toBeInstanceOf(XMLHttpRequest);
43 it('PUT', async () => {
44 const { open, send, load, progress, createRequest } = mockCreateRequest();
45 const webdav = new WebDAV(undefined, createRequest);
46 const promise = webdav.put('foo', 'Test data');
49 const request = await promise;
50 expect(open).toHaveBeenCalledWith('PUT', 'foo');
51 expect(send).toHaveBeenCalledWith('Test data');
52 expect(request).toBeInstanceOf(XMLHttpRequest);
55 it('COPY', async () => {
56 const { open, setRequestHeader, load, createRequest } = mockCreateRequest();
57 const webdav = new WebDAV(undefined, createRequest);
58 webdav.defaults.baseURL = 'http://base';
59 const promise = webdav.copy('foo', 'foo-copy');
61 const request = await promise;
62 expect(open).toHaveBeenCalledWith('COPY', 'http://base/foo');
63 expect(setRequestHeader).toHaveBeenCalledWith('Destination', 'http://base/foo-copy');
64 expect(request).toBeInstanceOf(XMLHttpRequest);
67 it('COPY - adds baseURL with trailing slash to Destination header', async () => {
68 const { open, setRequestHeader, load, createRequest } = mockCreateRequest();
69 const webdav = new WebDAV(undefined, createRequest);
70 webdav.defaults.baseURL = 'http://base';
71 const promise = webdav.copy('foo', 'foo-copy');
73 const request = await promise;
74 expect(open).toHaveBeenCalledWith('COPY', 'http://base/foo');
75 expect(setRequestHeader).toHaveBeenCalledWith('Destination', 'http://base/foo-copy');
76 expect(request).toBeInstanceOf(XMLHttpRequest);
79 it('COPY - adds baseURL without trailing slash to Destination header', async () => {
80 const { open, setRequestHeader, load, createRequest } = mockCreateRequest();
81 const webdav = new WebDAV(undefined, createRequest);
82 webdav.defaults.baseURL = 'http://base';
83 const promise = webdav.copy('foo', 'foo-copy');
85 const request = await promise;
86 expect(open).toHaveBeenCalledWith('COPY', 'http://base/foo');
87 expect(setRequestHeader).toHaveBeenCalledWith('Destination', 'http://base/foo-copy');
88 expect(request).toBeInstanceOf(XMLHttpRequest);
91 it('MOVE', async () => {
92 const { open, setRequestHeader, load, createRequest } = mockCreateRequest();
93 const webdav = new WebDAV(undefined, createRequest);
94 webdav.defaults.baseURL = 'http://base';
95 const promise = webdav.move('foo', 'foo-moved');
97 const request = await promise;
98 expect(open).toHaveBeenCalledWith('MOVE', 'http://base/foo');
99 expect(setRequestHeader).toHaveBeenCalledWith('Destination', 'http://base/foo-moved');
100 expect(request).toBeInstanceOf(XMLHttpRequest);
103 it('MOVE - adds baseURL with trailing slash to Destination header', async () => {
104 const { open, setRequestHeader, load, createRequest } = mockCreateRequest();
105 const webdav = new WebDAV(undefined, createRequest);
106 webdav.defaults.baseURL = 'http://base';
107 const promise = webdav.move('foo', 'foo-moved');
109 const request = await promise;
110 expect(open).toHaveBeenCalledWith('MOVE', 'http://base/foo');
111 expect(setRequestHeader).toHaveBeenCalledWith('Destination', 'http://base/foo-moved');
112 expect(request).toBeInstanceOf(XMLHttpRequest);
115 it('MOVE - adds baseURL without trailing slash to Destination header', async () => {
116 const { open, setRequestHeader, load, createRequest } = mockCreateRequest();
117 const webdav = new WebDAV(undefined, createRequest);
118 webdav.defaults.baseURL = 'http://base';
119 const promise = webdav.move('foo', 'foo-moved');
121 const request = await promise;
122 expect(open).toHaveBeenCalledWith('MOVE', 'http://base/foo');
123 expect(setRequestHeader).toHaveBeenCalledWith('Destination', 'http://base/foo-moved');
124 expect(request).toBeInstanceOf(XMLHttpRequest);
127 it('DELETE', async () => {
128 const { open, load, createRequest } = mockCreateRequest();
129 const webdav = new WebDAV(undefined, createRequest);
130 const promise = webdav.delete('foo');
132 const request = await promise;
133 expect(open).toHaveBeenCalledWith('DELETE', 'foo');
134 expect(request).toBeInstanceOf(XMLHttpRequest);
138 const mockCreateRequest = () => {
139 const send = jest.fn();
140 const open = jest.fn();
141 const setRequestHeader = jest.fn();
142 const request = new XMLHttpRequest();
145 request.setRequestHeader = setRequestHeader;
146 const load = () => request.dispatchEvent(new Event('load'));
147 const progress = () => request.dispatchEvent(new Event('progress'));
154 createRequest: () => request