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 webdav = new WebDAV(undefined, createRequest);
45 const promise = webdav.put('foo', 'Test data');
48 const request = await promise;
49 expect(open).toHaveBeenCalledWith('PUT', 'foo');
50 expect(send).toHaveBeenCalledWith('Test data');
51 expect(request).toBeInstanceOf(XMLHttpRequest);
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');
59 const request = await promise;
60 expect(open).toHaveBeenCalledWith('COPY', 'foo');
61 expect(setRequestHeader).toHaveBeenCalledWith('Destination', 'foo-copy');
62 expect(request).toBeInstanceOf(XMLHttpRequest);
65 it('COPY - adds baseURL with trailing slash 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');
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);
77 it('COPY - adds baseURL without trailing slash to Destination header', async () => {
78 const { open, setRequestHeader, load, createRequest } = mockCreateRequest();
79 const webdav = new WebDAV(undefined, createRequest);
80 webdav.defaults.baseURL = 'base';
81 const promise = webdav.copy('foo', 'foo-copy');
83 const request = await promise;
84 expect(open).toHaveBeenCalledWith('COPY', 'base/foo');
85 expect(setRequestHeader).toHaveBeenCalledWith('Destination', 'base/foo-copy');
86 expect(request).toBeInstanceOf(XMLHttpRequest);
89 it('MOVE', async () => {
90 const { open, setRequestHeader, load, createRequest } = mockCreateRequest();
91 const webdav = new WebDAV(undefined, createRequest);
92 const promise = webdav.move('foo', 'foo-moved');
94 const request = await promise;
95 expect(open).toHaveBeenCalledWith('MOVE', 'foo');
96 expect(setRequestHeader).toHaveBeenCalledWith('Destination', 'foo-moved');
97 expect(request).toBeInstanceOf(XMLHttpRequest);
100 it('MOVE - adds baseURL with trailing slash to Destination header', async () => {
101 const { open, setRequestHeader, load, createRequest } = mockCreateRequest();
102 const webdav = new WebDAV(undefined, createRequest);
103 webdav.defaults.baseURL = 'base/';
104 const promise = webdav.move('foo', 'foo-moved');
106 const request = await promise;
107 expect(open).toHaveBeenCalledWith('MOVE', 'base/foo');
108 expect(setRequestHeader).toHaveBeenCalledWith('Destination', 'base/foo-moved');
109 expect(request).toBeInstanceOf(XMLHttpRequest);
112 it('MOVE - adds baseURL without trailing slash to Destination header', async () => {
113 const { open, setRequestHeader, load, createRequest } = mockCreateRequest();
114 const webdav = new WebDAV(undefined, createRequest);
115 webdav.defaults.baseURL = 'base';
116 const promise = webdav.move('foo', 'foo-moved');
118 const request = await promise;
119 expect(open).toHaveBeenCalledWith('MOVE', 'base/foo');
120 expect(setRequestHeader).toHaveBeenCalledWith('Destination', 'base/foo-moved');
121 expect(request).toBeInstanceOf(XMLHttpRequest);
124 it('DELETE', async () => {
125 const { open, load, createRequest } = mockCreateRequest();
126 const webdav = new WebDAV(undefined, createRequest);
127 const promise = webdav.delete('foo');
129 const request = await promise;
130 expect(open).toHaveBeenCalledWith('DELETE', 'foo');
131 expect(request).toBeInstanceOf(XMLHttpRequest);
135 const mockCreateRequest = () => {
136 const send = jest.fn();
137 const open = jest.fn();
138 const setRequestHeader = jest.fn();
139 const request = new XMLHttpRequest();
142 request.setRequestHeader = setRequestHeader;
143 const load = () => request.dispatchEvent(new Event('load'));
144 const progress = () => request.dispatchEvent(new Event('progress'));
151 createRequest: () => request