13858-process-view-information-card
[arvados.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 webdav = new WebDAV(undefined, createRequest);
45         const promise = webdav.put('foo', 'Test data');
46         progress();
47         load();
48         const request = await promise;
49         expect(open).toHaveBeenCalledWith('PUT', 'foo');
50         expect(send).toHaveBeenCalledWith('Test data');
51         expect(request).toBeInstanceOf(XMLHttpRequest);
52     });
53
54     it('COPY', async () => {
55         const { open, setRequestHeader, load, createRequest } = mockCreateRequest();
56         const webdav = new WebDAV(undefined, createRequest);
57         const promise = webdav.copy('foo', 'foo-copy');
58         load();
59         const request = await promise;
60         expect(open).toHaveBeenCalledWith('COPY', 'foo');
61         expect(setRequestHeader).toHaveBeenCalledWith('Destination', 'foo-copy');
62         expect(request).toBeInstanceOf(XMLHttpRequest);
63     });
64
65     it('COPY - adds baseURL to Destination header', async () => {
66         const { open, setRequestHeader, load, createRequest } = mockCreateRequest();
67         const webdav = new WebDAV(undefined, createRequest);
68         webdav.defaults.baseURL = 'base/';
69         const promise = webdav.copy('foo', 'foo-copy');
70         load();
71         const request = await promise;
72         expect(open).toHaveBeenCalledWith('COPY', 'base/foo');
73         expect(setRequestHeader).toHaveBeenCalledWith('Destination', 'base/foo-copy');
74         expect(request).toBeInstanceOf(XMLHttpRequest);
75     });
76
77     it('MOVE', async () => {
78         const { open, setRequestHeader, load, createRequest } = mockCreateRequest();
79         const webdav = new WebDAV(undefined, createRequest);
80         const promise = webdav.move('foo', 'foo-copy');
81         load();
82         const request = await promise;
83         expect(open).toHaveBeenCalledWith('MOVE', 'foo');
84         expect(setRequestHeader).toHaveBeenCalledWith('Destination', 'foo-copy');
85         expect(request).toBeInstanceOf(XMLHttpRequest);
86     });
87
88     it('MOVE - adds baseURL to Destination header', async () => {
89         const { open, setRequestHeader, load, createRequest } = mockCreateRequest();
90         const webdav = new WebDAV(undefined, createRequest);
91         webdav.defaults.baseURL = 'base/';
92         const promise = webdav.move('foo', 'foo-moved');
93         load();
94         const request = await promise;
95         expect(open).toHaveBeenCalledWith('MOVE', 'base/foo');
96         expect(setRequestHeader).toHaveBeenCalledWith('Destination', 'base/foo-moved');
97         expect(request).toBeInstanceOf(XMLHttpRequest);
98     });
99
100     it('DELETE', async () => {
101         const { open, load, createRequest } = mockCreateRequest();
102         const webdav = new WebDAV(undefined, createRequest);
103         const promise = webdav.delete('foo');
104         load();
105         const request = await promise;
106         expect(open).toHaveBeenCalledWith('DELETE', 'foo');
107         expect(request).toBeInstanceOf(XMLHttpRequest);
108     });
109 });
110
111 const mockCreateRequest = () => {
112     const send = jest.fn();
113     const open = jest.fn();
114     const setRequestHeader = jest.fn();
115     const request = new XMLHttpRequest();
116     request.send = send;
117     request.open = open;
118     request.setRequestHeader = setRequestHeader;
119     const load = () => request.dispatchEvent(new Event('load'));
120     const progress = () => request.dispatchEvent(new Event('progress'));
121     return {
122         send,
123         open,
124         load,
125         progress,
126         setRequestHeader,
127         createRequest: () => request
128     };
129 };