-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(uploader): Allow to specify custom headers
Use case: file-request need to set `X-NC-Nickname` header on upload. Signed-off-by: Ferdinand Thiessen <[email protected]>
- Loading branch information
Showing
4 changed files
with
155 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
import { beforeEach, describe, expect, it, test, vi } from 'vitest' | ||
import { Uploader } from '../lib/uploader' | ||
import * as nextcloudAuth from '@nextcloud/auth' | ||
import * as nextcloudFiles from '@nextcloud/files' | ||
|
||
// This mocks auth to always return the `test` user by default | ||
vi.mock('@nextcloud/auth') | ||
|
||
describe('Uploader', () => { | ||
beforeEach(() => { | ||
vi.restoreAllMocks() | ||
// Reset mocks of DOM | ||
document.body.innerHTML = '' | ||
}) | ||
|
||
describe('Constructor', () => { | ||
it('sets default target folder for user', async () => { | ||
const uploader = new Uploader() | ||
expect(uploader.destination.source).match(/\/remote\.php\/dav\/files\/test\/?$/) | ||
}) | ||
|
||
it('sets default target folder for public share', async () => { | ||
// no logged in user | ||
vi.spyOn(nextcloudAuth, 'getCurrentUser').mockImplementationOnce(() => null) | ||
// public share values | ||
vi.spyOn(nextcloudFiles, 'davRemoteURL', 'get').mockReturnValue('http://example.com/public.php/dav') | ||
vi.spyOn(nextcloudFiles, 'davRootPath', 'get').mockReturnValue('/files/share-token') | ||
|
||
const uploader = new Uploader(true) | ||
expect(uploader.destination.source).match(/\/public\.php\/dav\/files\/share-token\/?$/) | ||
expect(uploader.destination.owner).toBe('anonymous') | ||
}) | ||
|
||
it('fails if not logged in and not on public share', async () => { | ||
vi.spyOn(nextcloudAuth, 'getCurrentUser').mockImplementationOnce(() => null) | ||
expect(async () => new Uploader()).rejects.toThrow(/User is not logged in/) | ||
}) | ||
}) | ||
|
||
describe('custom headers', () => { | ||
test('default to none', () => { | ||
const uploader = new Uploader() | ||
expect(uploader.customHeaders).toEqual({}) | ||
}) | ||
|
||
test('can set custom header', () => { | ||
const uploader = new Uploader() | ||
uploader.setCustomHeader('X-NC-Nickname', 'jane') | ||
expect(uploader.customHeaders).toEqual({ 'X-NC-Nickname': 'jane' }) | ||
}) | ||
|
||
test('can unset custom header', () => { | ||
const uploader = new Uploader() | ||
uploader.setCustomHeader('X-NC-Nickname', 'jane') | ||
expect(uploader.customHeaders).toEqual({ 'X-NC-Nickname': 'jane' }) | ||
uploader.deleteCustomerHeader('X-NC-Nickname') | ||
expect(uploader.customHeaders).toEqual({}) | ||
}) | ||
|
||
test('can unset custom header', () => { | ||
const uploader = new Uploader() | ||
uploader.setCustomHeader('X-NC-Nickname', 'jane') | ||
expect(uploader.customHeaders).toEqual({ 'X-NC-Nickname': 'jane' }) | ||
uploader.deleteCustomerHeader('X-NC-Nickname') | ||
expect(uploader.customHeaders).toEqual({}) | ||
}) | ||
|
||
test('can set an empty header', () => { | ||
// This is valid as per RFC7230 | ||
const uploader = new Uploader() | ||
uploader.setCustomHeader('Host', '') | ||
expect(uploader.customHeaders).toEqual({ 'Host': '' }) | ||
}) | ||
}) | ||
|
||
describe('destination', () => { | ||
test('can overwrite the destination', () => { | ||
const uploader = new Uploader() | ||
expect(uploader.destination.path).toBe('/') | ||
|
||
const newDestination = new nextcloudFiles.Folder({ | ||
owner: 'test', | ||
source: 'http://example.com/remote.php/dav/files/test/some/folder', | ||
root: '/files/test', | ||
}) | ||
|
||
expect(() => { uploader.destination = newDestination }).not.toThrow() | ||
expect(uploader.destination.path).toBe('/some/folder') | ||
}) | ||
|
||
test('cannot unset destination', () => { | ||
const uploader = new Uploader() | ||
expect(uploader.destination.path).toBe('/') | ||
|
||
expect(() => { uploader.destination = undefined as any }).toThrowError(/invalid destination/i) | ||
}) | ||
|
||
test('cannot set file as destination', () => { | ||
const uploader = new Uploader() | ||
expect(uploader.destination.path).toBe('/') | ||
|
||
const newDestination = new nextcloudFiles.File({ | ||
owner: 'test', | ||
source: 'http://example.com/remote.php/dav/files/test/some/folder-like', | ||
root: '/files/test', | ||
mime: 'text/plain', | ||
}) | ||
|
||
expect(() => { uploader.destination = newDestination as nextcloudFiles.Folder }).toThrowError(/invalid destination/i) | ||
}) | ||
}) | ||
}) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters