Tofu/tests/webdav/buildDavUrl.test.js

66 lines
2.7 KiB
JavaScript

import test from 'node:test'
import assert from 'node:assert/strict'
import { buildDavUrl } from '#tofu/tools/webdav/buildDavUrl.js'
test('builds the WebDAV URL of a file owned by the user', () => {
assert.equal(
buildDavUrl({
serverUrl: 'https://cloud.exemple.fr',
username: 'tofu',
path: 'Tofu/plannings/2026-W38.json',
}),
'https://cloud.exemple.fr/remote.php/dav/files/tofu/Tofu/plannings/2026-W38.json',
)
})
test('tolerates trailing slashes on the server URL', () => {
const url = buildDavUrl({ serverUrl: 'https://cloud.exemple.fr///', username: 'tofu', path: 'Tofu' })
assert.equal(url, 'https://cloud.exemple.fr/remote.php/dav/files/tofu/Tofu')
})
test('adds https:// when the scheme is missing', () => {
const url = buildDavUrl({ serverUrl: 'cloud.exemple.fr', username: 'tofu', path: 'Tofu' })
assert.equal(url, 'https://cloud.exemple.fr/remote.php/dav/files/tofu/Tofu')
})
test('keeps an explicit http scheme, for a local instance', () => {
const url = buildDavUrl({ serverUrl: 'http://localhost:8080', username: 'tofu', path: 'Tofu' })
assert.equal(url, 'http://localhost:8080/remote.php/dav/files/tofu/Tofu')
})
test('keeps a sub-path of the server URL', () => {
const url = buildDavUrl({ serverUrl: 'https://exemple.fr/nextcloud/', username: 'tofu', path: 'Tofu' })
assert.equal(url, 'https://exemple.fr/nextcloud/remote.php/dav/files/tofu/Tofu')
})
test('an empty path gives the user root, as a collection URL', () => {
const url = buildDavUrl({ serverUrl: 'https://cloud.exemple.fr', username: 'tofu', path: '' })
assert.equal(url, 'https://cloud.exemple.fr/remote.php/dav/files/tofu/')
})
test('encodes each path segment but keeps the separators', () => {
const url = buildDavUrl({
serverUrl: 'https://cloud.exemple.fr',
username: 'tofu',
path: 'Mes recettes/Gnocchis à la sauge.json',
})
assert.equal(
url,
'https://cloud.exemple.fr/remote.php/dav/files/tofu/Mes%20recettes/Gnocchis%20%C3%A0%20la%20sauge.json',
)
})
test('ignores leading, trailing and doubled slashes in the path', () => {
const url = buildDavUrl({ serverUrl: 'https://cloud.exemple.fr', username: 'tofu', path: '/Tofu//plannings/' })
assert.equal(url, 'https://cloud.exemple.fr/remote.php/dav/files/tofu/Tofu/plannings')
})
test('encodes the user name too', () => {
const url = buildDavUrl({ serverUrl: 'https://cloud.exemple.fr', username: 'jeanne dupré', path: '' })
assert.equal(url, 'https://cloud.exemple.fr/remote.php/dav/files/jeanne%20dupr%C3%A9/')
})
test('returns a usable string even without a server URL', () => {
const url = buildDavUrl({ serverUrl: '', username: 'tofu', path: 'Tofu' })
assert.equal(url, '/remote.php/dav/files/tofu/Tofu')
})