1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { WebDAV } from "./webdav";
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');
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);
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');
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);
32 it('PROPFIND', async () => {
33 const { open, load, createRequest } = mockCreateRequest();
34 const webdav = new WebDAV(undefined, createRequest);
35 const promise = webdav.propfind('foo');
37 const request = await promise;
38 expect(open).toHaveBeenCalledWith('PROPFIND', 'foo');
39 expect(request).toBeInstanceOf(XMLHttpRequest);
42 it('PUT', async () => {
43 const { open, send, load, progress, createRequest } = mockCreateRequest();
44 const onUploadProgress = jest.fn();
45 const webdav = new WebDAV(undefined, createRequest);
46 const promise = webdav.put('foo', 'Test data', { onUploadProgress });
49 const request = await promise;
50 expect(open).toHaveBeenCalledWith('PUT', 'foo');
51 expect(send).toHaveBeenCalledWith('Test data');
52 expect(onUploadProgress).toHaveBeenCalled();
53 expect(request).toBeInstanceOf(XMLHttpRequest);
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');
61 const request = await promise;
62 expect(open).toHaveBeenCalledWith('COPY', 'foo');
63 expect(setRequestHeader).toHaveBeenCalledWith('Destination', 'foo-copy');
64 expect(request).toBeInstanceOf(XMLHttpRequest);
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');
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);
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');
84 const request = await promise;
85 expect(open).toHaveBeenCalledWith('MOVE', 'foo');
86 expect(setRequestHeader).toHaveBeenCalledWith('Destination', 'foo-copy');
87 expect(request).toBeInstanceOf(XMLHttpRequest);
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');
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);
102 it('DELETE', async () => {
103 const { open, load, createRequest } = mockCreateRequest();
104 const webdav = new WebDAV(undefined, createRequest);
105 const promise = webdav.delete('foo');
107 const request = await promise;
108 expect(open).toHaveBeenCalledWith('DELETE', 'foo');
109 expect(request).toBeInstanceOf(XMLHttpRequest);
113 const mockCreateRequest = () => {
114 const send = jest.fn();
115 const open = jest.fn();
116 const setRequestHeader = jest.fn();
117 const request = new XMLHttpRequest();
120 request.setRequestHeader = setRequestHeader;
121 const load = () => request.dispatchEvent(new Event('load'));
122 const progress = () => request.dispatchEvent(new Event('progress'));
129 createRequest: () => request