Skip to content
Myr

Fix `searchParams` option merging with `URLSearchParams` instances (#755)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Sindre Sorhus · committed by GitHub · parent 2b0a100 · Browse files · 2 files+137 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+137 added−0 removed

  • tests:+80 added−0 removed
  • other:+57 added−0 removed

2 files+137 added−0 removed

modified:source/utils/merge.ts+57 added
modified:test/main.ts+80 added
433 unmodified lines
434435 await server.close();436});437438test('merges plain object searchParams with URLSearchParams', async t => {439 const server = await createHttpTestServer();440 server.get('/', (request, response) => {441 response.end(request.url);442 });443444 const client = ky.create({searchParams: {api: '123'}});445 const response = await client.get(server.url, {446 searchParams: new URLSearchParams({_limit_: '1'}),447 });448449 const url = await response.text();450 t.true(url.includes('api=123'), `URL should contain api=123, got: ${url}`);451 t.true(url.includes('_limit_=1'), `URL should contain _limit_=1, got: ${url}`);452 t.false(url.includes('[object Object]'), `URL should not contain [object Object], got: ${url}`);453 t.false(url.includes('headers'), `URL should not contain 'headers', got: ${url}`);454455 await server.close();456});457458test('merges URLSearchParams with plain object searchParams', async t => {459 const server = await createHttpTestServer();460 server.get('/', (request, response) => {461 response.end(request.url);462 });463464 const client = ky.create({searchParams: new URLSearchParams({api: '123'})});465 const response = await client.get(server.url, {466 searchParams: {_limit_: '1'},467 });468469 const url = await response.text();470 t.true(url.includes('api=123'), `URL should contain api=123, got: ${url}`);471 t.true(url.includes('_limit_=1'), `URL should contain _limit_=1, got: ${url}`);472 t.false(url.includes('[object Object]'), `URL should not contain [object Object], got: ${url}`);473474 await server.close();475});476477test('merges URLSearchParams with URLSearchParams', async t => {478 const server = await createHttpTestServer();479 server.get('/', (request, response) => {480 response.end(request.url);481 });482483 const client = ky.create({searchParams: new URLSearchParams({api: '123'})});484 const response = await client.get(server.url, {485 searchParams: new URLSearchParams({_limit_: '1'}),486 });487488 const url = await response.text();489 t.true(url.includes('api=123'), `URL should contain api=123, got: ${url}`);490 t.true(url.includes('_limit_=1'), `URL should contain _limit_=1, got: ${url}`);491 t.false(url.includes('[object Object]'), `URL should not contain [object Object], got: ${url}`);492493 await server.close();494});495496test('merges searchParams with duplicate keys', async t => {497 const server = await createHttpTestServer();498 server.get('/', (request, response) => {499 response.end(request.url);500 });501502 const client = ky.create({searchParams: new URLSearchParams({filter: 'active'})});503 const response = await client.get(server.url, {504 searchParams: new URLSearchParams({filter: 'recent', _limit_: '10'}),505 });506507 const urlString = await response.text();508 const url = new URL(urlString, server.url);509 const filterValues = url.searchParams.getAll('filter');510511 t.deepEqual(filterValues.sort(), ['active', 'recent'], `Both filter values should be present, got: ${JSON.stringify(filterValues)}`);512 t.is(url.searchParams.get('_limit_'), '10', `URL should contain _limit_=10, got: ${urlString}`);513 t.false(urlString.includes('[object Object]'), `URL should not contain [object Object], got: ${urlString}`);514515 await server.close();516});517518test('throwHttpErrors option', async t => {519 const server = await createHttpTestServer();520 server.get('/', (_request, response) => {521 response.sendStatus(500);
524 unmodified lines