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 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('MOVE', async () => {
78 const { open, setRequestHeader, load, createRequest } = mockCreateRequest();
79 const webdav = new WebDAV(undefined, createRequest);
80 const promise = webdav.move('foo', 'foo-copy');
82 const request = await promise;
83 expect(open).toHaveBeenCalledWith('MOVE', 'foo');
84 expect(setRequestHeader).toHaveBeenCalledWith('Destination', 'foo-copy');
85 expect(request).toBeInstanceOf(XMLHttpRequest);
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');
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);
100 it('DELETE', async () => {
101 const { open, load, createRequest } = mockCreateRequest();
102 const webdav = new WebDAV(undefined, createRequest);
103 const promise = webdav.delete('foo');
105 const request = await promise;
106 expect(open).toHaveBeenCalledWith('DELETE', 'foo');
107 expect(request).toBeInstanceOf(XMLHttpRequest);
111 const mockCreateRequest = () => {
112 const send = jest.fn();
113 const open = jest.fn();
114 const setRequestHeader = jest.fn();
115 const request = new XMLHttpRequest();
118 request.setRequestHeader = setRequestHeader;
119 const load = () => request.dispatchEvent(new Event('load'));
120 const progress = () => request.dispatchEvent(new Event('progress'));
127 createRequest: () => request