Remvoe static create method, update webdav methods signatures, change Config props...
[arvados-workbench2.git] / src / common / webdav.test.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { WebDAV } from "./webdav";
6
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');
12         load();
13         const request = await promise;
14         expect(open).toHaveBeenCalledWith('PROPFIND', 'http://foo.com/foo');
15         expect(setRequestHeader).toHaveBeenCalledWith('Authorization', 'Basic');
16         expect(request).toBeInstanceOf(XMLHttpRequest);
17     });
18
19     it('allows to modify defaults after instantiation', async () => {
20         const { open, load, setRequestHeader, createRequest } = mockCreateRequest();
21         const webdav = new WebDAV(undefined, createRequest);
22         webdav.defaults.baseURL = 'http://foo.com/';
23         webdav.defaults.headers = { Authorization: 'Basic' };
24         const promise = webdav.propfind('foo');
25         load();
26         const request = await promise;
27         expect(open).toHaveBeenCalledWith('PROPFIND', 'http://foo.com/foo');
28         expect(setRequestHeader).toHaveBeenCalledWith('Authorization', 'Basic');
29         expect(request).toBeInstanceOf(XMLHttpRequest);
30     });
31
32     it('PROPFIND', async () => {
33         const { open, load, createRequest } = mockCreateRequest();
34         const webdav = new WebDAV(undefined, createRequest);
35         const promise = webdav.propfind('foo');
36         load();
37         const request = await promise;
38         expect(open).toHaveBeenCalledWith('PROPFIND', 'foo');
39         expect(request).toBeInstanceOf(XMLHttpRequest);
40     });
41
42     it('PUT', async () => {
43         const { open, send, load, progress, createRequest } = mockCreateRequest();
44         const onProgress = jest.fn();
45         const webdav = new WebDAV(undefined, createRequest);
46         const promise = webdav.put('foo', 'Test data', { onProgress });
47         progress();
48         load();
49         const request = await promise;
50         expect(open).toHaveBeenCalledWith('PUT', 'foo');
51         expect(send).toHaveBeenCalledWith('Test data');
52         expect(onProgress).toHaveBeenCalled();
53         expect(request).toBeInstanceOf(XMLHttpRequest);
54     });
55
56     it('COPY', async () => {
57         const { open, setRequestHeader, load, createRequest } = mockCreateRequest();
58         const webdav = new WebDAV(undefined, createRequest);
59         const promise = webdav.copy('foo', 'foo-copy');
60         load();
61         const request = await promise;
62         expect(open).toHaveBeenCalledWith('COPY', 'foo');
63         expect(setRequestHeader).toHaveBeenCalledWith('Destination', 'foo-copy');
64         expect(request).toBeInstanceOf(XMLHttpRequest);
65     });
66
67     it('COPY - adds baseURL to Destination header', async () => {
68         const { open, setRequestHeader, load, createRequest } = mockCreateRequest();
69         const webdav = new WebDAV(undefined, createRequest);
70         webdav.defaults.baseURL = 'base/';
71         const promise = webdav.copy('foo', 'foo-copy');
72         load();
73         const request = await promise;
74         expect(open).toHaveBeenCalledWith('COPY', 'base/foo');
75         expect(setRequestHeader).toHaveBeenCalledWith('Destination', 'base/foo-copy');
76         expect(request).toBeInstanceOf(XMLHttpRequest);
77     });
78
79     it('MOVE', async () => {
80         const { open, setRequestHeader, load, createRequest } = mockCreateRequest();
81         const webdav = new WebDAV(undefined, createRequest);
82         const promise = webdav.move('foo', 'foo-copy');
83         load();
84         const request = await promise;
85         expect(open).toHaveBeenCalledWith('MOVE', 'foo');
86         expect(setRequestHeader).toHaveBeenCalledWith('Destination', 'foo-copy');
87         expect(request).toBeInstanceOf(XMLHttpRequest);
88     });
89
90     it('MOVE - adds baseURL to Destination header', async () => {
91         const { open, setRequestHeader, load, createRequest } = mockCreateRequest();
92         const webdav = new WebDAV(undefined, createRequest);
93         webdav.defaults.baseURL = 'base/';
94         const promise = webdav.move('foo', 'foo-moved');
95         load();
96         const request = await promise;
97         expect(open).toHaveBeenCalledWith('MOVE', 'base/foo');
98         expect(setRequestHeader).toHaveBeenCalledWith('Destination', 'base/foo-moved');
99         expect(request).toBeInstanceOf(XMLHttpRequest);
100     });
101
102     it('DELETE', async () => {
103         const { open, load, createRequest } = mockCreateRequest();
104         const webdav = new WebDAV(undefined, createRequest);
105         const promise = webdav.delete('foo');
106         load();
107         const request = await promise;
108         expect(open).toHaveBeenCalledWith('DELETE', 'foo');
109         expect(request).toBeInstanceOf(XMLHttpRequest);
110     });
111 });
112
113 const mockCreateRequest = () => {
114     const send = jest.fn();
115     const open = jest.fn();
116     const setRequestHeader = jest.fn();
117     const request = new XMLHttpRequest();
118     request.send = send;
119     request.open = open;
120     request.setRequestHeader = setRequestHeader;
121     const load = () => request.dispatchEvent(new Event('load'));
122     const progress = () => request.dispatchEvent(new Event('progress'));
123     return {
124         send,
125         open,
126         load,
127         progress,
128         setRequestHeader,
129         createRequest: () => request
130     };
131 };