Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Query string params #1449

Merged
merged 37 commits into from May 19, 2022
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
2b56a54
feat: redirect
ronag Mar 22, 2021
7607b90
fix linting
ronag Apr 27, 2022
a4d185e
fix: linting
ronag Apr 27, 2022
176cce2
Implement support for querystring params
kibertoad May 18, 2022
def801f
Merge branch 'main' of https://github.com/nodejs/undici into feat/144…
kibertoad May 18, 2022
bf7433a
Remove leaked change
kibertoad May 18, 2022
433a455
Fix test
kibertoad May 18, 2022
5afcdc8
Address code review comments
kibertoad May 18, 2022
05bc042
Address linting errors
kibertoad May 18, 2022
52089aa
Improve coverage
kibertoad May 18, 2022
a89e7aa
Comment out new tests to see if they are causing problems in CI
kibertoad May 18, 2022
06a0093
Improve coverage
kibertoad May 18, 2022
18744d4
Fix linting
kibertoad May 18, 2022
3fdd223
Adjust two other tests too
kibertoad May 18, 2022
8f6f998
Remove excessive condition
kibertoad May 18, 2022
4c711c6
Fix linting
kibertoad May 18, 2022
d270947
Merge remote-tracking branch 'origin/feat/1448-params' into feat/1448…
kibertoad May 18, 2022
5d35025
Handle additional use-cases
kibertoad May 18, 2022
babcf18
Simplify
kibertoad May 18, 2022
2eb2797
Try to fix tests in CI
kibertoad May 18, 2022
a471788
Make code more consistent
kibertoad May 18, 2022
7bb502a
Address code review comments
kibertoad May 19, 2022
edd2272
Fix linting
kibertoad May 19, 2022
fb9f354
Cleanup
kibertoad May 19, 2022
ad84f02
Cleanup and improve coverage
kibertoad May 19, 2022
86014dd
Restore encoding with explanation
kibertoad May 19, 2022
5e0bf8b
Fix linting
kibertoad May 19, 2022
ffc6db2
Fix docs
kibertoad May 19, 2022
54c97e5
Reduce amount of unescaped characters
kibertoad May 19, 2022
6f3802e
Simplify branch
kibertoad May 19, 2022
c3f71f9
Fix symbol
kibertoad May 19, 2022
63e7f07
Explicitly do not support #
kibertoad May 19, 2022
5dba6b7
Adjust based on latest comments
kibertoad May 19, 2022
8d2c7d4
Fix linting
kibertoad May 19, 2022
ed81bbb
Address code review comments
kibertoad May 19, 2022
a454d00
Fix linting
kibertoad May 19, 2022
79610dd
Clarify documentation
kibertoad May 19, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/api/Dispatcher.md
Expand Up @@ -194,6 +194,7 @@ Returns: `Boolean` - `false` if dispatcher is busy and further dispatch calls wo
* **method** `string`
* **body** `string | Buffer | Uint8Array | stream.Readable | Iterable | AsyncIterable | null` (optional) - Default: `null`
* **headers** `UndiciHeaders | string[]` (optional) - Default: `null`.
* **params** `Record<string, any | null` (optional) - Default: `null` - Query string params to be embedded in the request URL.
kibertoad marked this conversation as resolved.
Show resolved Hide resolved
* **idempotent** `boolean` (optional) - Default: `true` if `method` is `'HEAD'` or `'GET'` - Whether the requests can be safely retried or not. If `false` the request won't be sent until all preceding requests in the pipeline has completed.
* **blocking** `boolean` (optional) - Default: `false` - Whether the response is expected to take a long time and would end up blocking the pipeline. When this is set to `true` further pipelining will be avoided on the same connection until headers have been received.
* **upgrade** `string | null` (optional) - Default: `null` - Upgrade the request. Should be used to specify the kind of upgrade i.e. `'Websocket'`.
Expand Down
11 changes: 9 additions & 2 deletions lib/core/request.js
Expand Up @@ -4,8 +4,8 @@ const {
InvalidArgumentError,
NotSupportedError
} = require('./errors')
const util = require('./util')
const assert = require('assert')
const util = require('./util')

const kHandler = Symbol('handler')

Expand Down Expand Up @@ -38,6 +38,7 @@ class Request {
method,
body,
headers,
params,
idempotent,
blocking,
upgrade,
Expand Down Expand Up @@ -97,7 +98,13 @@ class Request {

this.upgrade = upgrade || null

this.path = path
if (params && Object.keys(params).length > 0) {
kibertoad marked this conversation as resolved.
Show resolved Hide resolved
const searchParams = new URLSearchParams(params)
const delimiter = path.indexOf('?') === -1 ? '?' : '&'
ronag marked this conversation as resolved.
Show resolved Hide resolved
this.path = `${path}${delimiter}${searchParams.toString()}`
} else {
this.path = path
}

this.origin = origin

Expand Down
130 changes: 127 additions & 3 deletions test/client.js
@@ -1,10 +1,10 @@
'use strict'

const { test } = require('tap')
const { Client, errors } = require('..')
const { createServer } = require('http')
const { readFileSync, createReadStream } = require('fs')
const { createServer } = require('http')
const { Readable } = require('stream')
const { test } = require('tap')
const { Client, errors } = require('..')
const { kSocket } = require('../lib/core/symbols')
const { wrapWithAsyncIterable } = require('./utils/async-iterators')
const EE = require('events')
Expand Down Expand Up @@ -80,6 +80,116 @@ test('basic get', (t) => {
})
})

test('basic get with query params', (t) => {
t.plan(4)

const server = createServer((req, res) => {
const [, paramString] = req.url.split('?')
const searchParams = new URLSearchParams(paramString)
const searchParamsObject = Object.fromEntries(searchParams.entries())
t.strictSame(searchParamsObject, {
foo: '1',
bar: 'bar',
multi: '1,2'
})

res.statusCode = 200
res.end('hello')
})
t.teardown(server.close.bind(server))

const params = {
foo: 1,
bar: 'bar',
multi: [1, 2]
}

server.listen(0, () => {
const client = new Client(`http://localhost:${server.address().port}`, {
keepAliveTimeout: 300e3
})
t.tearDown(client.close.bind(client))

const signal = new EE()
client.request({
signal,
path: '/',
method: 'GET',
params
}, (err, data) => {
t.error(err)
const { statusCode } = data
t.strictEqual(statusCode, 200)
})
t.strictEqual(signal.listenerCount('abort'), 1)
})
})

/*
test('basic get with empty query params', (t) => {
t.plan(4)

const server = createServer(serverRequestParams(t, {}))
t.tearDown(server.close.bind(server))

const params = {}

server.listen(0, () => {
const client = new Client(`http://localhost:${server.address().port}`, {
keepAliveTimeout: 300e3
})
t.tearDown(client.close.bind(client))

const signal = new EE()
client.request({
signal,
path: '/',
method: 'GET',
params
}, (err, data) => {
t.error(err)
const { statusCode } = data
t.strictEqual(statusCode, 200)
})
t.strictEqual(signal.listenerCount('abort'), 1)
})
})

test('basic get with query params partially in path', (t) => {
t.plan(4)

const server = createServer(serverRequestParams(t, {
foo: '1',
bar: '2'
}))
t.tearDown(server.close.bind(server))

const params = {
foo: 1
}

server.listen(0, () => {
const client = new Client(`http://localhost:${server.address().port}`, {
keepAliveTimeout: 300e3
})
t.tearDown(client.close.bind(client))

const signal = new EE()
client.request({
signal,
path: '/?bar=2',
method: 'GET',
params
}, (err, data) => {
t.error(err)
const { statusCode } = data
t.strictEqual(statusCode, 200)
})
t.strictEqual(signal.listenerCount('abort'), 1)
})
})
*/

test('basic head', (t) => {
t.plan(14)

Expand Down Expand Up @@ -251,6 +361,20 @@ test('head with host header', (t) => {
})
})

/*
function serverRequestParams (t, expected) {
return function (req, res) {
const [, paramString] = req.url.split('?')
const searchParams = new URLSearchParams(paramString)
const searchParamsObject = Object.fromEntries(searchParams.entries())
t.strictSame(searchParamsObject, expected)

res.statusCode = 200
res.end('hello')
}
}
*/

function postServer (t, expected) {
return function (req, res) {
t.equal(req.url, '/')
Expand Down