18848: update the Dockerfile for package building to automatically pick
[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 { customEncodeURI } from "./url";
6 import { WebDAV } from "./webdav";
7
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');
13         load();
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);
18     });
19
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');
26         load();
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);
31     });
32
33     it('PROPFIND', async () => {
34         const { open, load, 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(request).toBeInstanceOf(XMLHttpRequest);
41     });
42
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');
47         progress();
48         load();
49         const request = await promise;
50         expect(open).toHaveBeenCalledWith('PUT', 'foo');
51         expect(send).toHaveBeenCalledWith('Test data');
52         expect(request).toBeInstanceOf(XMLHttpRequest);
53     });
54
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');
60         load();
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);
65     });
66
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');
72         load();
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);
77     });
78
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');
84         load();
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);
89     });
90
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');
96         load();
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);
101     });
102
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');
108         load();
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);
113     });
114
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');
120         load();
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);
125     });
126
127     it('DELETE', async () => {
128         const { open, load, createRequest } = mockCreateRequest();
129         const webdav = new WebDAV(undefined, createRequest);
130         const promise = webdav.delete('foo');
131         load();
132         const request = await promise;
133         expect(open).toHaveBeenCalledWith('DELETE', 'foo');
134         expect(request).toBeInstanceOf(XMLHttpRequest);
135     });
136 });
137
138 const mockCreateRequest = () => {
139     const send = jest.fn();
140     const open = jest.fn();
141     const setRequestHeader = jest.fn();
142     const request = new XMLHttpRequest();
143     request.send = send;
144     request.open = open;
145     request.setRequestHeader = setRequestHeader;
146     const load = () => request.dispatchEvent(new Event('load'));
147     const progress = () => request.dispatchEvent(new Event('progress'));
148     return {
149         send,
150         open,
151         load,
152         progress,
153         setRequestHeader,
154         createRequest: () => request
155     };
156 };