Skip to content
Open
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
3 changes: 2 additions & 1 deletion core/src/core-plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,8 @@ export class CapacitorHttpPluginWeb extends WebPlugin implements CapacitorHttpPl
async request(options: HttpOptions): Promise<HttpResponse> {
const requestInit = buildRequestInit(options, options.webFetchExtra);
const urlParams = buildUrlParams(options.params, options.shouldEncodeUrlParams);
const url = urlParams ? `${options.url}?${urlParams}` : options.url;
const separator = options.url.includes('?') ? '&' : '?';
const url = urlParams ? `${options.url}${separator}${urlParams}` : options.url;

const response = await fetch(url, requestInit);
const contentType = response.headers.get('content-type') || '';
Expand Down
42 changes: 42 additions & 0 deletions core/src/tests/http.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* @jest-environment jsdom
*/

import { CapacitorHttpPluginWeb } from '../core-plugins';

describe('CapacitorHttpPluginWeb', () => {
const originalFetch = (globalThis as any).fetch;
let fetchMock: jest.Mock;

beforeEach(() => {
fetchMock = jest.fn(async (url: string) => ({
ok: true,
status: 200,
url,
headers: {
get: () => 'text/plain',
forEach: () => undefined,
},
text: async () => 'ok',
}));
(globalThis as any).fetch = fetchMock;
});

afterEach(() => {
(globalThis as any).fetch = originalFetch;
});

it('appends params to a url without a query string', async () => {
const http = new CapacitorHttpPluginWeb();
await http.get({ url: 'https://example.com/api', params: { b: '2' } });

expect(fetchMock.mock.calls[0][0]).toEqual('https://example.com/api?b=2');
});

it('keeps the existing query string when adding params', async () => {
const http = new CapacitorHttpPluginWeb();
await http.get({ url: 'https://example.com/api?a=1', params: { b: '2' } });

expect(fetchMock.mock.calls[0][0]).toEqual('https://example.com/api?a=1&b=2');
});
});