-
-
Notifications
You must be signed in to change notification settings - Fork 150
/
Copy pathgetDirectoryContents.spec.ts
355 lines (318 loc) · 13.8 KB
/
getDirectoryContents.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
import { expect } from "chai";
import { AuthType, FileStat } from "../../../source/index.js";
import {
SERVER_PASSWORD,
SERVER_PORT,
SERVER_USERNAME,
clean,
createWebDAVClient,
createWebDAVServer,
restoreRequests,
returnFakeResponse,
useCustomXmlResponse,
useRequestSpy
} from "../../helpers.node.js";
import { readFileSync } from "fs";
describe("getDirectoryContents", function () {
beforeEach(async function () {
clean();
this.client = createWebDAVClient(`http://localhost:${SERVER_PORT}/webdav/server`, {
username: SERVER_USERNAME,
password: SERVER_PASSWORD
});
this.server = createWebDAVServer();
this.requestSpy = useRequestSpy();
await this.server.start();
});
afterEach(async function () {
await this.server.stop();
restoreRequests();
clean();
});
it("returns an array of items", function () {
return this.client.getDirectoryContents("/").then(function (contents) {
expect(contents).to.be.an("array");
expect(contents[0]).to.be.an("object");
});
});
it("returns correct directory results", function () {
return this.client.getDirectoryContents("/").then(function (contents) {
const sub1 = contents.find(function (item) {
return item.basename === "sub1";
});
expect(sub1.filename).to.equal("/sub1");
expect(sub1.size).to.equal(0);
expect(sub1.type).to.equal("directory");
});
});
it("returns results not including base directory", function () {
return this.client.getDirectoryContents("/sub1").then(function (contents) {
const sub1 = contents.find(function (item) {
return item.basename === "sub1";
});
expect(sub1).to.be.undefined;
});
});
it("returns only expected results when using trailing slash", function () {
return this.client.getDirectoryContents("/webdav/").then(function (contents) {
const items = contents.map(item => item.filename);
expect(items).to.deep.equal(["/webdav/server"]);
});
});
it("returns correct file results", function () {
return this.client.getDirectoryContents("/").then(function (contents) {
const sub1 = contents.find(item => item.basename === "alrighty.jpg");
const sub2 = contents.find(item => item.basename === "file&name.txt");
expect(sub1.filename).to.equal("/alrighty.jpg");
expect(sub1.size).to.equal(52130);
expect(sub1.type).to.equal("file");
expect(sub2.filename).to.equal("/file&name.txt");
});
});
it("returns correct file results in sub-directory", function () {
return this.client.getDirectoryContents("/sub1").then(function (contents) {
const sub1 = contents.find(function (item) {
return item.basename === "irrelephant.jpg";
});
expect(sub1.filename).to.equal("/sub1/irrelephant.jpg");
expect(sub1.size).to.equal(138008);
expect(sub1.type).to.equal("file");
});
});
it("returns correct results when calling without root slash", function () {
return this.client.getDirectoryContents("sub1").then(function (contents) {
expect(contents).to.have.lengthOf(2);
const sub1 = contents.find(item => item.basename === "irrelephant.jpg");
expect(sub1).to.be.ok;
const sub2 = contents.find(item => item.basename === "ยากจน #1.txt");
expect(sub2).to.be.ok;
});
});
it("returns correct file results for files with special characters", function () {
return this.client.getDirectoryContents("/sub1").then(function (contents) {
const sub1 = contents.find(function (item) {
return item.basename === "ยากจน #1.txt";
});
expect(sub1.filename).to.equal("/sub1/ยากจน #1.txt");
});
});
it("returns correct file results for files with HTML entities in their names", function () {
returnFakeResponse(
readFileSync(
new URL("../../responses/propfind-href-html-entities.xml", import.meta.url)
).toString()
);
return this.client.getDirectoryContents("/files").then(function (contents) {
const file = contents.find(function (item) {
return item.basename === "&.md";
});
expect(file).not.to.equal(undefined, "Expected file does not exist");
expect(file.filename).to.match(/\/files\/&\.md$/);
});
});
it("returns correct file results for files with query in href", function () {
returnFakeResponse(
readFileSync(
new URL("../../responses/propfind-href-with-query.xml", import.meta.url)
).toString()
);
return this.client.getDirectoryContents("/files").then(function (contents) {
expect(contents).to.have.length(1);
expect(contents[0].filename).to.match(/\/files\/some file\?foo=1&bar=2$/);
});
});
it("correctly parses the displayname property", function () {
returnFakeResponse(
readFileSync(
new URL("../../responses/propfind-numeric-displayname.xml", import.meta.url)
).toString()
);
return this.client.getDirectoryContents("/1", { details: true }).then(function (result) {
expect(result.data).to.have.length(1);
expect(result.data[0]).to.have.property("props").that.is.an("object");
expect(result.data[0].props)
.to.have.property("displayname")
.that.is.a("string")
.and.equal("1");
});
});
it("returns the contents of a directory with repetitive naming", function () {
return this.client.getDirectoryContents("/webdav/server").then(function (contents) {
expect(contents).to.be.an("array");
expect(contents[0]).to.be.an("object");
expect(contents[0]).to.have.property("basename", "notreal.txt");
});
});
it("returns only the directory contents (issue #68)", function () {
return this.client.getDirectoryContents("/two words").then(function (contents) {
expect(contents).to.have.lengthOf(1);
expect(contents[0].basename).to.equal("file.txt");
});
});
it("returns only the directory contents for directory with & in name", function () {
return this.client.getDirectoryContents("/with & in path").then(function (contents) {
expect(contents).to.have.lengthOf(1);
expect(contents[0].basename).to.equal("file.txt");
});
});
it("returns correct directory contents when path contains encoded sequences (issue #93)", function () {
return this.client.getDirectoryContents("/two%20words").then(contents => {
expect(contents).to.have.lengthOf(1);
expect(contents[0].basename).to.equal("file2.txt");
});
});
it("returns correct names for directories that contain % in the name", function () {
return this.client.getDirectoryContents("/").then(function (contents) {
const noPercent = contents.find(item => item.basename === "two words");
const percent = contents.find(item => item.basename === "two%20words");
expect(noPercent).to.have.property("type", "directory");
expect(percent).to.have.property("type", "directory");
});
});
it("allows specifying custom headers", async function () {
await this.client.getDirectoryContents("/", {
headers: {
"X-test": "test"
}
});
const [, requestOptions] = this.requestSpy.firstCall.args;
expect(requestOptions).to.have.property("headers").that.has.property("X-test", "test");
});
describe("when using details: true", function () {
it("returns data and headers properties", function () {
return this.client
.getDirectoryContents("/", { details: true })
.then(function (details) {
expect(details).to.have.property("data").that.is.an("array");
expect(details).to.have.property("headers").that.is.an("object");
});
});
it("returns props on each directory item", function () {
return this.client
.getDirectoryContents("/", { details: true })
.then(function (details) {
const alrightyJpg = details.data.find(item => item.basename === "alrighty.jpg");
expect(alrightyJpg).to.have.property("props").that.is.an("object");
expect(alrightyJpg.props)
.to.have.property("getlastmodified")
.that.matches(/GMT$/);
});
});
});
describe("when connected to Seafile server", function () {
beforeEach(function () {
this.client = createWebDAVClient("https://cloud.ascal-strasbg.fr/seafdav", {
username: SERVER_USERNAME,
password: SERVER_PASSWORD
});
useCustomXmlResponse("seafile-propfind");
});
afterEach(function () {
restoreRequests();
});
it("returns the correct response", function () {
return this.client.getDirectoryContents("/").then(function (contents) {
expect(contents).to.be.an("array");
expect(contents).to.deep.equal([
{
filename: "/Ma bibliothèque",
etag: "2920f985ebc6692632c7c3ab46b3919556239d37",
basename: "Ma bibliothèque",
lastmod: null,
size: 0,
type: "directory"
}
]);
});
});
});
describe("when fetching an empty multistatus", function () {
beforeEach(function () {
this.client = createWebDAVClient(`http://localhost:${SERVER_PORT}/webdav/server`, {
username: SERVER_USERNAME,
password: SERVER_PASSWORD
});
useCustomXmlResponse("empty-multistatus");
});
afterEach(function () {
restoreRequests();
});
it("returns the correct response", function () {
return this.client.getDirectoryContents("/").then(function (contents) {
expect(contents).to.be.an("array");
expect(contents).to.deep.equal([]);
});
});
});
it("supports globbing files", function () {
const options = {
deep: true,
glob: "/webdav/**/*.txt"
};
return this.client.getDirectoryContents("/", options).then(function (contents) {
expect(contents).to.have.lengthOf(1);
expect(contents[0].filename).to.equal("/webdav/server/notreal.txt");
});
});
describe("using Digest authentication", function () {
beforeEach(async function () {
await this.server.stop();
this.client = createWebDAVClient(`http://localhost:${SERVER_PORT}/webdav/server`, {
username: SERVER_USERNAME,
password: SERVER_PASSWORD,
authType: AuthType.Digest
});
this.server = createWebDAVServer("digest");
await this.server.start();
});
afterEach(async function () {
await this.server.stop();
clean();
});
it("returns an array of results", function () {
return this.client.getDirectoryContents("/").then(function (contents) {
expect(contents).to.be.an("array");
expect(contents[0]).to.be.an("object");
});
});
});
describe("when using includeSelf: true", function () {
it("returns correct directory results with directory itself", function () {
return this.client.getDirectoryContents("/", { includeSelf: true }).then(function (
contents: Array<FileStat>
) {
const root = contents.find(item => item.basename === "");
expect(root.filename).to.equal("/");
expect(root.size).to.equal(0);
expect(root.type).to.equal("directory");
expect(contents.length).to.equal(12);
});
});
it("returns correct file results in sub-directory", function () {
return this.client.getDirectoryContents("/sub1", { includeSelf: true }).then(function (
contents: Array<FileStat>
) {
const sub1 = contents.find(item => item.basename === "sub1");
expect(sub1.filename).to.equal("/sub1");
expect(sub1.size).to.equal(0);
expect(sub1.type).to.equal("directory");
expect(contents.length).to.equal(3);
});
});
});
describe("when using custom dir base path", function () {
beforeEach(async function () {
this.client = createWebDAVClient(`http://localhost:${SERVER_PORT}/webdav/server`, {
username: SERVER_USERNAME,
password: SERVER_PASSWORD,
remoteBasePath: "/webdav/server/custom"
});
});
it("return correct filename with custom path", function () {
return this.client.getDirectoryContents("/").then(function (contents: Array<FileStat>) {
const file = contents.find(item => item.basename === "notes.txt");
expect(file.filename).to.equal("/../notes.txt");
});
});
});
});