Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/__tests__/unit/account.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,38 @@ describe('account discovery fallback', () => {
});

describe('serviceDiscovery', () => {
describe.each(['PROPFIND', 'GET'])('%s redirects', (method) => {
it.each([
['caldav', '../dav/', 'https://example.com/dav/'],
['carddav', 'dav/', 'https://example.com/.well-known/dav/'],
['caldav', '?user=test', 'https://example.com/.well-known/caldav?user=test'],
] as const)(
'resolves %s Location %s against the discovery URL',
async (accountType, location, expected) => {
const mockFetch = vi.fn();
if (method === 'GET') {
mockFetch.mockResolvedValueOnce({ status: 405, headers: new Map() });
}
mockFetch.mockResolvedValueOnce({
status: 301,
headers: new Map([['Location', location]]),
});

const url = await serviceDiscovery({
account: { serverUrl: 'https://example.com/calendars/user1/', accountType },
fetch: mockFetch,
});

expect(url).toBe(expected);
expect(mockFetch).toHaveBeenCalledTimes(method === 'GET' ? 2 : 1);
expect(mockFetch).toHaveBeenLastCalledWith(
`https://example.com/.well-known/${accountType}`,
expect.objectContaining({ method, redirect: 'manual' }),
);
},
);
});

it('should follow redirect from PROPFIND', async () => {
const mockFetch = vi.fn().mockResolvedValueOnce({
status: 301,
Expand Down
33 changes: 33 additions & 0 deletions src/__tests__/unit/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,39 @@ describe('DAVClient fetch override', () => {
});
});

describe('client account discovery', () => {
it.each(['factory', 'class'])(
'resolves relative discovery redirects through the %s API',
async (api) => {
const mockFetch = vi.fn().mockResolvedValueOnce({
status: 301,
headers: new Map([['Location', '../dav/']]),
});
const options = {
serverUrl: 'https://example.com/calendars/user1/',
credentials: { username: 'test', password: 'password' },
fetch: mockFetch,
};
const client = api === 'factory' ? await createDAVClient(options) : new DAVClient(options);

const account = await client.createAccount({
account: {
accountType: 'caldav',
principalUrl: 'https://example.com/principals/user1/',
homeUrl: 'https://example.com/calendars/user1/',
},
});

expect(account.rootUrl).toBe('https://example.com/dav/');
expect(mockFetch).toHaveBeenCalledTimes(1);
expect(mockFetch).toHaveBeenCalledWith(
'https://example.com/.well-known/caldav',
expect.objectContaining({ method: 'PROPFIND', redirect: 'manual' }),
);
},
);
});

describe('createDAVClient auth methods', () => {
const mockCredentials = {
username: 'test',
Expand Down
2 changes: 1 addition & 1 deletion src/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export const serviceDiscovery = async (params: {
// protocol; explicit ones must be honored verbatim (never downgrade
// https → http, never silently upgrade either).
const hasExplicitScheme = /^[a-z][a-z0-9+.-]*:/i.test(location);
const serviceURL = new URL(location, endpoint);
const serviceURL = new URL(location, uri);

if (serviceURL.hostname === uri.hostname && uri.port && !serviceURL.port) {
serviceURL.port = uri.port;
Expand Down
Loading