Skip to content

Commit

Permalink
Resolve issue #493 (#494)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsumners committed Aug 4, 2023
1 parent ba3a212 commit b55848a
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/httpRequestBuilder.js
Expand Up @@ -72,6 +72,8 @@ function requestBuilder (defaults) {

if (typeof body === 'string') {
bodyBuf = Buffer.from(body)
} else if (typeof body === 'number') {
bodyBuf = Buffer.from(body + '')
} else if (Buffer.isBuffer(body)) {
bodyBuf = body
} else if (body && Array.isArray(body._)) {
Expand Down
94 changes: 94 additions & 0 deletions test/cli.test.js
Expand Up @@ -254,3 +254,97 @@ test('run with workers', { skip: !hasWorkerSupport }, (t) => {
})
.on('end', t.end)
})

test('should run handle PUT bodies', (t) => {
t.test('"number" bodies work', t => {
t.plan(2)

const server = helper.startServer()
const url = 'http://localhost:' + server.address().port

const cmd = [
path.join(__dirname, '..'),
'-d', '1',
'-m', 'PUT',
'-H', 'content-type="application/x-www-form-urlencoded"',
'-b', '1',
url
]
const child = childProcess.spawn(process.execPath, cmd, {
cwd: __dirname,
env: process.env,
stdio: ['ignore', 'pipe', 'pipe'],
detached: false
})

t.teardown(() => {
child.kill()
})

const outputLines = []
child
.stderr
.pipe(split())
.on('data', (line) => {
outputLines.push(line)
})
.on('end', () => {
t.equal(
outputLines.some(l => l === 'body must be either a string or a buffer'),
false
)
t.equal(
/.* requests in ([0-9]|\.)+s, .* read/.test(outputLines.pop()),
true
)
t.end()
})
})

t.test('"string" bodies work', t => {
t.plan(2)

const server = helper.startServer()
const url = 'http://localhost:' + server.address().port

const cmd = [
path.join(__dirname, '..'),
'-d', '1',
'-m', 'PUT',
'-H', 'content-type="application/x-www-form-urlencoded"',
'-b', '"1"',
url
]
const child = childProcess.spawn(process.execPath, cmd, {
cwd: __dirname,
env: process.env,
stdio: ['ignore', 'pipe', 'pipe'],
detached: false
})

t.teardown(() => {
child.kill()
})

const outputLines = []
child
.stderr
.pipe(split())
.on('data', (line) => {
outputLines.push(line)
})
.on('end', () => {
t.equal(
outputLines.some(l => l === 'body must be either a string or a buffer'),
false
)
t.equal(
/.* requests in ([0-9]|\.)+s, .* read/.test(outputLines.pop()),
true
)
t.end()
})
})

t.end()
})

0 comments on commit b55848a

Please sign in to comment.