18874: Fix arvados-boot source dir.
[arvados.git] / services / workbench2 / 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(setRequestHeader).toHaveBeenCalledWith('Cache-Control', 'no-cache');
17         expect(request).toBeInstanceOf(XMLHttpRequest);
18     });
19
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');
25         load();
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);
31     });
32
33     it('PROPFIND', async () => {
34         const { open, load, setRequestHeader, createRequest } = mockCreateRequest();
35         const webdav = new WebDAV(undefined, createRequest);
36         const promise = webdav.propfind('foo');
37         load();
38         const request = await promise;
39         expect(open).toHaveBeenCalledWith('PROPFIND', 'foo');
40         expect(setRequestHeader).toHaveBeenCalledWith('Cache-Control', 'no-cache');
41         expect(request).toBeInstanceOf(XMLHttpRequest);
42     });
43
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');
48         progress();
49         load();
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);
55     });
56
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');
61         load();
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);
67     });
68
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');
73         load();
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);
79     });
80
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');
85         load();
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);
91     });
92
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');
97         load();
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);
103     });
104
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');
109         load();
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);
115     });
116
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');
121         load();
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);
127     });
128
129     it('DELETE', async () => {
130         const { open, load, setRequestHeader, createRequest } = mockCreateRequest();
131         const webdav = new WebDAV(undefined, createRequest);
132         const promise = webdav.delete('foo');
133         load();
134         const request = await promise;
135         expect(open).toHaveBeenCalledWith('DELETE', 'foo');
136         expect(setRequestHeader).toHaveBeenCalledWith('Cache-Control', 'no-cache');
137         expect(request).toBeInstanceOf(XMLHttpRequest);
138     });
139 });
140
141 const mockCreateRequest = () => {
142     const send = jest.fn();
143     const open = jest.fn();
144     const setRequestHeader = jest.fn();
145     const request = new XMLHttpRequest();
146     request.send = send;
147     request.open = open;
148     request.setRequestHeader = setRequestHeader;
149     const load = () => request.dispatchEvent(new Event('load'));
150     const progress = () => request.dispatchEvent(new Event('progress'));
151     return {
152         send,
153         open,
154         load,
155         progress,
156         setRequestHeader,
157         createRequest: () => request
158     };
159 };