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

chore: migrate a batch of tests to node test runner no. 6 #2741

Merged
merged 1 commit into from
Feb 12, 2024
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
120 changes: 68 additions & 52 deletions test/content-length.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
'use strict'

const { test } = require('tap')
const { tspl } = require('@matteo.collina/tspl')
const { test, after } = require('node:test')
const { Client, errors } = require('..')
const { createServer } = require('node:http')
const { Readable } = require('node:stream')
const { maybeWrapStream, consts } = require('./utils/async-iterators')

test('request invalid content-length', (t) => {
t.plan(7)
test('request invalid content-length', async (t) => {
t = tspl(t, { plan: 7 })

const server = createServer((req, res) => {
res.end()
})
t.teardown(server.close.bind(server))
after(() => server.close())
server.listen(0, () => {
const client = new Client(`http://localhost:${server.address().port}`)
t.teardown(client.destroy.bind(client))
after(() => client.close())

client.request({
path: '/',
Expand Down Expand Up @@ -94,19 +95,21 @@ test('request invalid content-length', (t) => {
t.ok(err instanceof errors.RequestContentLengthMismatchError)
})
})

await t.completed
})

function invalidContentLength (bodyType) {
test(`request streaming ${bodyType} invalid content-length`, (t) => {
t.plan(4)
test(`request streaming ${bodyType} invalid content-length`, async (t) => {
t = tspl(t, { plan: 4 })

const server = createServer((req, res) => {
res.end()
})
t.teardown(server.close.bind(server))
after(() => server.close())
server.listen(0, () => {
const client = new Client(`http://localhost:${server.address().port}`)
t.teardown(client.destroy.bind(client))
after(() => client.close())

client.once('disconnect', () => {
t.ok(true, 'pass')
Expand Down Expand Up @@ -151,23 +154,24 @@ function invalidContentLength (bodyType) {
t.ok(err instanceof errors.RequestContentLengthMismatchError)
})
})
await t.completed
})
}

invalidContentLength(consts.STREAM)
invalidContentLength(consts.ASYNC_ITERATOR)

function zeroContentLength (bodyType) {
test(`request ${bodyType} streaming data when content-length=0`, (t) => {
t.plan(1)
test(`request ${bodyType} streaming data when content-length=0`, async (t) => {
t = tspl(t, { plan: 1 })

const server = createServer((req, res) => {
res.end()
})
t.teardown(server.close.bind(server))
after(() => server.close())
server.listen(0, () => {
const client = new Client(`http://localhost:${server.address().port}`)
t.teardown(client.destroy.bind(client))
after(() => client.close())

client.request({
path: '/',
Expand All @@ -187,22 +191,23 @@ function zeroContentLength (bodyType) {
t.ok(err instanceof errors.RequestContentLengthMismatchError)
})
})
await t.completed
})
}

zeroContentLength(consts.STREAM)
zeroContentLength(consts.ASYNC_ITERATOR)

test('request streaming no body data when content-length=0', (t) => {
t.plan(2)
test('request streaming no body data when content-length=0', async (t) => {
t = tspl(t, { plan: 2 })

const server = createServer((req, res) => {
req.pipe(res)
})
t.teardown(server.close.bind(server))
after(() => server.close())
server.listen(0, () => {
const client = new Client(`http://localhost:${server.address().port}`)
t.teardown(client.destroy.bind(client))
after(() => client.close())

client.request({
path: '/',
Expand All @@ -211,7 +216,7 @@ test('request streaming no body data when content-length=0', (t) => {
'content-length': 0
}
}, (err, data) => {
t.error(err)
t.ifError(err)
data.body
.on('data', () => {
t.fail()
Expand All @@ -221,86 +226,92 @@ test('request streaming no body data when content-length=0', (t) => {
})
})
})

await t.completed
})

test('response invalid content length with close', (t) => {
t.plan(3)
test('response invalid content length with close', async (t) => {
t = tspl(t, { plan: 3 })

const server = createServer((req, res) => {
res.writeHead(200, {
'content-length': 10
})
res.end('123')
})
t.teardown(server.close.bind(server))
after(() => server.close())
server.listen(0, () => {
const client = new Client(`http://localhost:${server.address().port}`, {
pipelining: 0
})
t.teardown(client.destroy.bind(client))
after(() => client.close())

client.on('disconnect', (origin, client, err) => {
t.equal(err.code, 'UND_ERR_RES_CONTENT_LENGTH_MISMATCH')
t.strictEqual(err.code, 'UND_ERR_RES_CONTENT_LENGTH_MISMATCH')
})

client.request({
path: '/',
method: 'GET'
}, (err, data) => {
t.error(err)
t.ifError(err)
data.body
.on('end', () => {
t.fail()
})
.on('error', (err) => {
t.equal(err.code, 'UND_ERR_RES_CONTENT_LENGTH_MISMATCH')
t.strictEqual(err.code, 'UND_ERR_RES_CONTENT_LENGTH_MISMATCH')
})
.resume()
})
})

await t.completed
})

test('request streaming with Readable.from(buf)', (t) => {
test('request streaming with Readable.from(buf)', async (t) => {
const server = createServer((req, res) => {
req.pipe(res)
})
t.teardown(server.close.bind(server))
after(() => server.close())
server.listen(0, () => {
const client = new Client(`http://localhost:${server.address().port}`)
t.teardown(client.destroy.bind(client))
after(() => client.close())

client.request({
path: '/',
method: 'PUT',
body: Readable.from(Buffer.from('hello'))
}, (err, data) => {
const chunks = []
t.error(err)
t.ifError(err)
data.body
.on('data', (chunk) => {
chunks.push(chunk)
})
.on('end', () => {
t.equal(Buffer.concat(chunks).toString(), 'hello')
t.strictEqual(Buffer.concat(chunks).toString(), 'hello')
t.ok(true, 'pass')
t.end()
})
})
})

await t.completed
})

test('request DELETE, content-length=0, with body', (t) => {
t.plan(5)
test('request DELETE, content-length=0, with body', async (t) => {
t = tspl(t, { plan: 5 })
const server = createServer((req, res) => {
res.end()
})
server.on('request', (req, res) => {
t.equal(req.headers['content-length'], undefined)
t.strictEqual(req.headers['content-length'], undefined)
})
t.teardown(server.close.bind(server))
after(() => server.close())
server.listen(0, () => {
const client = new Client(`http://localhost:${server.address().port}`)
t.teardown(client.destroy.bind(client))
after(() => client.close())

client.request({
path: '/',
Expand All @@ -325,38 +336,41 @@ test('request DELETE, content-length=0, with body', (t) => {
'content-length': 0
}
}, (err, resp) => {
t.equal(resp.headers['content-length'], '0')
t.error(err)
t.strictEqual(resp.headers['content-length'], '0')
t.ifError(err)
})

client.on('disconnect', () => {
t.ok(true, 'pass')
})
})

await t.completed
})

test('content-length shouldSendContentLength=false', (t) => {
t.plan(15)
test('content-length shouldSendContentLength=false', async (t) => {
t = tspl(t, { plan: 15 })

const server = createServer((req, res) => {
res.end()
})
server.on('request', (req, res) => {
switch (req.url) {
case '/put0':
t.equal(req.headers['content-length'], '0')
t.strictEqual(req.headers['content-length'], '0')
break
case '/head':
t.equal(req.headers['content-length'], undefined)
t.strictEqual(req.headers['content-length'], undefined)
break
case '/get':
t.equal(req.headers['content-length'], undefined)
t.strictEqual(req.headers['content-length'], undefined)
break
}
})
t.teardown(server.close.bind(server))
after(() => server.close())
server.listen(0, () => {
const client = new Client(`http://localhost:${server.address().port}`)
t.teardown(client.destroy.bind(client))
after(() => client.close())

client.request({
path: '/put0',
Expand All @@ -365,8 +379,8 @@ test('content-length shouldSendContentLength=false', (t) => {
'content-length': 0
}
}, (err, resp) => {
t.equal(resp.headers['content-length'], '0')
t.error(err)
t.strictEqual(resp.headers['content-length'], '0')
t.ifError(err)
})

client.request({
Expand All @@ -376,8 +390,8 @@ test('content-length shouldSendContentLength=false', (t) => {
'content-length': 10
}
}, (err, resp) => {
t.equal(resp.headers['content-length'], undefined)
t.error(err)
t.strictEqual(resp.headers['content-length'], undefined)
t.ifError(err)
})

client.request({
Expand All @@ -387,7 +401,7 @@ test('content-length shouldSendContentLength=false', (t) => {
'content-length': 0
}
}, (err) => {
t.error(err)
t.ifError(err)
})

client.request({
Expand All @@ -403,7 +417,7 @@ test('content-length shouldSendContentLength=false', (t) => {
}
})
}, (err) => {
t.error(err)
t.ifError(err)
})

client.request({
Expand All @@ -419,7 +433,7 @@ test('content-length shouldSendContentLength=false', (t) => {
}
})
}, (err) => {
t.error(err)
t.ifError(err)
})

client.request({
Expand All @@ -435,11 +449,13 @@ test('content-length shouldSendContentLength=false', (t) => {
}
})
}, (err) => {
t.error(err)
t.ifError(err)
})

client.on('disconnect', () => {
t.ok(true, 'pass')
})
})

await t.completed
})
2 changes: 1 addition & 1 deletion test/esm-wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
await import('./utils/esm-wrapper.mjs')
} catch (e) {
if (e.message === 'Not supported') {
require('tap') // shows skipped
require('node:test') // shows skipped
return
}
console.error(e.stack)
Expand Down
Loading
Loading