fatsify核心功能示例测试!!!
This commit is contained in:
35
node_modules/fastify/test/http-methods/copy.test.js
generated
vendored
Normal file
35
node_modules/fastify/test/http-methods/copy.test.js
generated
vendored
Normal file
@@ -0,0 +1,35 @@
|
||||
'use strict'
|
||||
|
||||
const { test } = require('node:test')
|
||||
const fastify = require('../../fastify')()
|
||||
fastify.addHttpMethod('COPY')
|
||||
|
||||
test('can be created - copy', async t => {
|
||||
t.plan(3)
|
||||
|
||||
t.after(() => fastify.close())
|
||||
|
||||
try {
|
||||
fastify.route({
|
||||
method: 'COPY',
|
||||
url: '*',
|
||||
handler: function (req, reply) {
|
||||
reply.code(204).send()
|
||||
}
|
||||
})
|
||||
t.assert.ok(true)
|
||||
} catch (e) {
|
||||
t.assert.fail()
|
||||
}
|
||||
|
||||
const fastifyServer = await fastify.listen({ port: 0 })
|
||||
|
||||
const result = await fetch(`${fastifyServer}/test.txt`, {
|
||||
method: 'COPY',
|
||||
headers: {
|
||||
Destination: '/test2.txt'
|
||||
}
|
||||
})
|
||||
t.assert.ok(result.ok)
|
||||
t.assert.strictEqual(result.status, 204)
|
||||
})
|
||||
114
node_modules/fastify/test/http-methods/custom-http-methods.test.js
generated
vendored
Normal file
114
node_modules/fastify/test/http-methods/custom-http-methods.test.js
generated
vendored
Normal file
@@ -0,0 +1,114 @@
|
||||
'use strict'
|
||||
|
||||
const http = require('node:http')
|
||||
const { test } = require('node:test')
|
||||
const Fastify = require('../../fastify')
|
||||
|
||||
function addEcho (fastify, method) {
|
||||
fastify.route({
|
||||
method,
|
||||
url: '/',
|
||||
handler: function (req, reply) {
|
||||
reply.send(req.body)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
test('missing method from http client', (t, done) => {
|
||||
t.plan(2)
|
||||
const fastify = Fastify()
|
||||
|
||||
fastify.listen({ port: 3000 }, (err) => {
|
||||
t.assert.ifError(err)
|
||||
|
||||
const port = fastify.server.address().port
|
||||
const req = http.request({
|
||||
port,
|
||||
method: 'REBIND',
|
||||
path: '/'
|
||||
}, (res) => {
|
||||
t.assert.strictEqual(res.statusCode, 404)
|
||||
fastify.close()
|
||||
done()
|
||||
})
|
||||
|
||||
req.end()
|
||||
})
|
||||
})
|
||||
|
||||
test('addHttpMethod increase the supported HTTP methods supported', (t, done) => {
|
||||
t.plan(8)
|
||||
const app = Fastify()
|
||||
|
||||
t.assert.throws(() => { addEcho(app, 'REBIND') }, /REBIND method is not supported./)
|
||||
t.assert.ok(!app.supportedMethods.includes('REBIND'))
|
||||
t.assert.ok(!app.rebind)
|
||||
|
||||
app.addHttpMethod('REBIND')
|
||||
t.assert.doesNotThrow(() => { addEcho(app, 'REBIND') }, 'REBIND method is supported.')
|
||||
t.assert.ok(app.supportedMethods.includes('REBIND'))
|
||||
t.assert.ok(app.rebind)
|
||||
|
||||
app.rebind('/foo', () => 'hello')
|
||||
|
||||
app.inject({
|
||||
method: 'REBIND',
|
||||
url: '/foo'
|
||||
}, (err, response) => {
|
||||
t.assert.ifError(err)
|
||||
t.assert.strictEqual(response.payload, 'hello')
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
test('addHttpMethod adds a new custom method without body', t => {
|
||||
t.plan(3)
|
||||
const app = Fastify()
|
||||
|
||||
t.assert.throws(() => { addEcho(app, 'REBIND') }, /REBIND method is not supported./)
|
||||
|
||||
app.addHttpMethod('REBIND')
|
||||
t.assert.doesNotThrow(() => { addEcho(app, 'REBIND') }, 'REBIND method is supported.')
|
||||
|
||||
t.assert.throws(() => {
|
||||
app.route({
|
||||
url: '/',
|
||||
method: 'REBIND',
|
||||
schema: {
|
||||
body: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
hello: { type: 'string' }
|
||||
}
|
||||
}
|
||||
},
|
||||
handler: function (req, reply) {
|
||||
reply.send(req.body)
|
||||
}
|
||||
})
|
||||
}, /Body validation schema for REBIND:\/ route is not supported!/)
|
||||
})
|
||||
|
||||
test('addHttpMethod adds a new custom method with body', (t, done) => {
|
||||
t.plan(3)
|
||||
const app = Fastify()
|
||||
|
||||
app.addHttpMethod('REBIND', { hasBody: true })
|
||||
t.assert.doesNotThrow(() => { addEcho(app, 'REBIND') }, 'REBIND method is supported.')
|
||||
|
||||
app.inject({
|
||||
method: 'REBIND',
|
||||
url: '/',
|
||||
payload: { hello: 'world' }
|
||||
}, (err, response) => {
|
||||
t.assert.ifError(err)
|
||||
t.assert.deepStrictEqual(response.json(), { hello: 'world' })
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
test('addHttpMethod rejects fake http method', t => {
|
||||
t.plan(1)
|
||||
const fastify = Fastify()
|
||||
t.assert.throws(() => { fastify.addHttpMethod('FOOO') }, /Provided method is invalid!/)
|
||||
})
|
||||
412
node_modules/fastify/test/http-methods/get.test.js
generated
vendored
Normal file
412
node_modules/fastify/test/http-methods/get.test.js
generated
vendored
Normal file
@@ -0,0 +1,412 @@
|
||||
'use strict'
|
||||
|
||||
const { test } = require('node:test')
|
||||
const { Client } = require('undici')
|
||||
const fastify = require('../../fastify')()
|
||||
|
||||
const schema = {
|
||||
schema: {
|
||||
response: {
|
||||
'2xx': {
|
||||
type: 'object',
|
||||
properties: {
|
||||
hello: {
|
||||
type: 'string'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const nullSchema = {
|
||||
schema: {
|
||||
response: {
|
||||
'2xx': {
|
||||
type: 'null'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const numberSchema = {
|
||||
schema: {
|
||||
response: {
|
||||
'2xx': {
|
||||
type: 'object',
|
||||
properties: {
|
||||
hello: {
|
||||
type: 'number'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const querySchema = {
|
||||
schema: {
|
||||
querystring: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
hello: {
|
||||
type: 'integer'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const paramsSchema = {
|
||||
schema: {
|
||||
params: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
foo: {
|
||||
type: 'string'
|
||||
},
|
||||
test: {
|
||||
type: 'integer'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const headersSchema = {
|
||||
schema: {
|
||||
headers: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
'x-test': {
|
||||
type: 'number'
|
||||
},
|
||||
'Y-Test': {
|
||||
type: 'number'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
test('shorthand - get', t => {
|
||||
t.plan(1)
|
||||
try {
|
||||
fastify.get('/', schema, function (req, reply) {
|
||||
reply.code(200).send({ hello: 'world' })
|
||||
})
|
||||
t.assert.ok(true)
|
||||
} catch (e) {
|
||||
t.assert.fail()
|
||||
}
|
||||
})
|
||||
|
||||
test('shorthand - get (return null)', t => {
|
||||
t.plan(1)
|
||||
try {
|
||||
fastify.get('/null', nullSchema, function (req, reply) {
|
||||
reply.code(200).send(null)
|
||||
})
|
||||
t.assert.ok(true)
|
||||
} catch (e) {
|
||||
t.assert.fail()
|
||||
}
|
||||
})
|
||||
|
||||
test('shorthand - get params', t => {
|
||||
t.plan(1)
|
||||
try {
|
||||
fastify.get('/params/:foo/:test', paramsSchema, function (req, reply) {
|
||||
reply.code(200).send(req.params)
|
||||
})
|
||||
t.assert.ok(true)
|
||||
} catch (e) {
|
||||
t.assert.fail()
|
||||
}
|
||||
})
|
||||
|
||||
test('shorthand - get, querystring schema', t => {
|
||||
t.plan(1)
|
||||
try {
|
||||
fastify.get('/query', querySchema, function (req, reply) {
|
||||
reply.code(200).send(req.query)
|
||||
})
|
||||
t.assert.ok(true)
|
||||
} catch (e) {
|
||||
t.assert.fail()
|
||||
}
|
||||
})
|
||||
|
||||
test('shorthand - get, headers schema', t => {
|
||||
t.plan(1)
|
||||
try {
|
||||
fastify.get('/headers', headersSchema, function (req, reply) {
|
||||
reply.code(200).send(req.headers)
|
||||
})
|
||||
t.assert.ok(true)
|
||||
} catch (e) {
|
||||
t.assert.fail()
|
||||
}
|
||||
})
|
||||
|
||||
test('missing schema - get', t => {
|
||||
t.plan(1)
|
||||
try {
|
||||
fastify.get('/missing', function (req, reply) {
|
||||
reply.code(200).send({ hello: 'world' })
|
||||
})
|
||||
t.assert.ok(true)
|
||||
} catch (e) {
|
||||
t.assert.fail()
|
||||
}
|
||||
})
|
||||
|
||||
test('custom serializer - get', t => {
|
||||
t.plan(1)
|
||||
|
||||
function customSerializer (data) {
|
||||
return JSON.stringify(data)
|
||||
}
|
||||
|
||||
try {
|
||||
fastify.get('/custom-serializer', numberSchema, function (req, reply) {
|
||||
reply.code(200).serializer(customSerializer).send({ hello: 'world' })
|
||||
})
|
||||
t.assert.ok(true)
|
||||
} catch (e) {
|
||||
t.assert.fail()
|
||||
}
|
||||
})
|
||||
|
||||
test('empty response', t => {
|
||||
t.plan(1)
|
||||
try {
|
||||
fastify.get('/empty', function (req, reply) {
|
||||
reply.code(200).send()
|
||||
})
|
||||
t.assert.ok(true)
|
||||
} catch (e) {
|
||||
t.assert.fail()
|
||||
}
|
||||
})
|
||||
|
||||
test('send a falsy boolean', t => {
|
||||
t.plan(1)
|
||||
try {
|
||||
fastify.get('/boolean', function (req, reply) {
|
||||
reply.code(200).send(false)
|
||||
})
|
||||
t.assert.ok(true)
|
||||
} catch (e) {
|
||||
t.assert.fail()
|
||||
}
|
||||
})
|
||||
|
||||
test('shorthand - get, set port', t => {
|
||||
t.plan(1)
|
||||
try {
|
||||
fastify.get('/port', headersSchema, function (req, reply) {
|
||||
reply.code(200).send({ port: req.port })
|
||||
})
|
||||
t.assert.ok(true)
|
||||
} catch (e) {
|
||||
t.assert.fail()
|
||||
}
|
||||
})
|
||||
|
||||
test('get test', async t => {
|
||||
t.after(() => { fastify.close() })
|
||||
|
||||
await fastify.listen({ port: 0 })
|
||||
|
||||
await t.test('shorthand - request get', async t => {
|
||||
t.plan(4)
|
||||
|
||||
const response = await fetch('http://localhost:' + fastify.server.address().port, {
|
||||
method: 'GET'
|
||||
})
|
||||
t.assert.ok(response.ok)
|
||||
t.assert.strictEqual(response.status, 200)
|
||||
const body = await response.text()
|
||||
t.assert.strictEqual(response.headers.get('content-length'), '' + body.length)
|
||||
t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
|
||||
})
|
||||
|
||||
await t.test('shorthand - request get params schema', async t => {
|
||||
t.plan(4)
|
||||
|
||||
const response = await fetch('http://localhost:' + fastify.server.address().port + '/params/world/123', {
|
||||
method: 'GET'
|
||||
})
|
||||
t.assert.ok(response.ok)
|
||||
t.assert.strictEqual(response.status, 200)
|
||||
const body = await response.text()
|
||||
t.assert.strictEqual(response.headers.get('content-length'), '' + body.length)
|
||||
t.assert.deepStrictEqual(JSON.parse(body), { foo: 'world', test: 123 })
|
||||
})
|
||||
|
||||
await t.test('shorthand - request get params schema error', async t => {
|
||||
t.plan(3)
|
||||
|
||||
const response = await fetch('http://localhost:' + fastify.server.address().port + '/params/world/string', {
|
||||
method: 'GET'
|
||||
})
|
||||
t.assert.ok(!response.ok)
|
||||
t.assert.strictEqual(response.status, 400)
|
||||
const body = await response.text()
|
||||
t.assert.deepStrictEqual(JSON.parse(body), {
|
||||
error: 'Bad Request',
|
||||
code: 'FST_ERR_VALIDATION',
|
||||
message: 'params/test must be integer',
|
||||
statusCode: 400
|
||||
})
|
||||
})
|
||||
|
||||
await t.test('shorthand - request get headers schema', async t => {
|
||||
t.plan(4)
|
||||
|
||||
const response = await fetch('http://localhost:' + fastify.server.address().port + '/headers', {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'x-test': '1',
|
||||
'Y-Test': '3'
|
||||
}
|
||||
})
|
||||
t.assert.ok(response.ok)
|
||||
t.assert.strictEqual(response.status, 200)
|
||||
const body = await response.json()
|
||||
t.assert.strictEqual(body['x-test'], 1)
|
||||
t.assert.strictEqual(body['y-test'], 3)
|
||||
})
|
||||
|
||||
await t.test('shorthand - request get headers schema error', async t => {
|
||||
t.plan(3)
|
||||
|
||||
const response = await fetch('http://localhost:' + fastify.server.address().port + '/headers', {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'x-test': 'abc'
|
||||
}
|
||||
})
|
||||
t.assert.ok(!response.ok)
|
||||
t.assert.strictEqual(response.status, 400)
|
||||
const body = await response.text()
|
||||
t.assert.deepStrictEqual(JSON.parse(body), {
|
||||
error: 'Bad Request',
|
||||
code: 'FST_ERR_VALIDATION',
|
||||
message: 'headers/x-test must be number',
|
||||
statusCode: 400
|
||||
})
|
||||
})
|
||||
|
||||
await t.test('shorthand - request get querystring schema', async t => {
|
||||
t.plan(4)
|
||||
|
||||
const response = await fetch('http://localhost:' + fastify.server.address().port + '/query?hello=123', {
|
||||
method: 'GET'
|
||||
})
|
||||
t.assert.ok(response.ok)
|
||||
t.assert.strictEqual(response.status, 200)
|
||||
const body = await response.text()
|
||||
t.assert.strictEqual(response.headers.get('content-length'), '' + body.length)
|
||||
t.assert.deepStrictEqual(JSON.parse(body), { hello: 123 })
|
||||
})
|
||||
|
||||
await t.test('shorthand - request get querystring schema error', async t => {
|
||||
t.plan(3)
|
||||
|
||||
const response = await fetch('http://localhost:' + fastify.server.address().port + '/query?hello=world', {
|
||||
method: 'GET'
|
||||
})
|
||||
t.assert.ok(!response.ok)
|
||||
t.assert.strictEqual(response.status, 400)
|
||||
const body = await response.text()
|
||||
t.assert.deepStrictEqual(JSON.parse(body), {
|
||||
error: 'Bad Request',
|
||||
code: 'FST_ERR_VALIDATION',
|
||||
message: 'querystring/hello must be integer',
|
||||
statusCode: 400
|
||||
})
|
||||
})
|
||||
|
||||
await t.test('shorthand - request get missing schema', async t => {
|
||||
t.plan(4)
|
||||
|
||||
const response = await fetch('http://localhost:' + fastify.server.address().port + '/missing', {
|
||||
method: 'GET'
|
||||
})
|
||||
t.assert.ok(response.ok)
|
||||
t.assert.strictEqual(response.status, 200)
|
||||
const body = await response.text()
|
||||
t.assert.strictEqual(response.headers.get('content-length'), '' + body.length)
|
||||
t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
|
||||
})
|
||||
|
||||
await t.test('shorthand - custom serializer', async t => {
|
||||
t.plan(4)
|
||||
|
||||
const response = await fetch('http://localhost:' + fastify.server.address().port + '/custom-serializer', {
|
||||
method: 'GET'
|
||||
})
|
||||
t.assert.ok(response.ok)
|
||||
t.assert.strictEqual(response.status, 200)
|
||||
const body = await response.text()
|
||||
t.assert.strictEqual(response.headers.get('content-length'), '' + body.length)
|
||||
t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
|
||||
})
|
||||
|
||||
await t.test('shorthand - empty response', async t => {
|
||||
t.plan(4)
|
||||
|
||||
const response = await fetch('http://localhost:' + fastify.server.address().port + '/empty', {
|
||||
method: 'GET'
|
||||
})
|
||||
t.assert.ok(response.ok)
|
||||
t.assert.strictEqual(response.status, 200)
|
||||
const body = await response.text()
|
||||
t.assert.strictEqual(response.headers.get('content-length'), '0')
|
||||
t.assert.deepStrictEqual(body.toString(), '')
|
||||
})
|
||||
|
||||
await t.test('shorthand - send a falsy boolean', async t => {
|
||||
t.plan(3)
|
||||
|
||||
const response = await fetch('http://localhost:' + fastify.server.address().port + '/boolean', {
|
||||
method: 'GET'
|
||||
})
|
||||
t.assert.ok(response.ok)
|
||||
t.assert.strictEqual(response.status, 200)
|
||||
const body = await response.text()
|
||||
t.assert.deepStrictEqual(body.toString(), 'false')
|
||||
})
|
||||
|
||||
await t.test('shorthand - send null value', async t => {
|
||||
t.plan(3)
|
||||
|
||||
const response = await fetch('http://localhost:' + fastify.server.address().port + '/null', {
|
||||
method: 'GET'
|
||||
})
|
||||
t.assert.ok(response.ok)
|
||||
t.assert.strictEqual(response.status, 200)
|
||||
const body = await response.text()
|
||||
t.assert.deepStrictEqual(body.toString(), 'null')
|
||||
})
|
||||
|
||||
await t.test('shorthand - request get headers - test fall back port', async t => {
|
||||
t.plan(2)
|
||||
|
||||
const instance = new Client('http://localhost:' + fastify.server.address().port)
|
||||
|
||||
const response = await instance.request({
|
||||
path: '/port',
|
||||
method: 'GET',
|
||||
headers: {
|
||||
host: 'example.com'
|
||||
}
|
||||
})
|
||||
|
||||
t.assert.strictEqual(response.statusCode, 200)
|
||||
const body = JSON.parse(await response.body.text())
|
||||
t.assert.strictEqual(body.port, null)
|
||||
})
|
||||
})
|
||||
263
node_modules/fastify/test/http-methods/head.test.js
generated
vendored
Normal file
263
node_modules/fastify/test/http-methods/head.test.js
generated
vendored
Normal file
@@ -0,0 +1,263 @@
|
||||
'use strict'
|
||||
|
||||
const { test } = require('node:test')
|
||||
const fastify = require('../../fastify')()
|
||||
|
||||
const schema = {
|
||||
schema: {
|
||||
response: {
|
||||
'2xx': {
|
||||
type: 'null'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const querySchema = {
|
||||
schema: {
|
||||
querystring: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
hello: {
|
||||
type: 'integer'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const paramsSchema = {
|
||||
schema: {
|
||||
params: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
foo: {
|
||||
type: 'string'
|
||||
},
|
||||
test: {
|
||||
type: 'integer'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
test('shorthand - head', t => {
|
||||
t.plan(1)
|
||||
try {
|
||||
fastify.head('/', schema, function (req, reply) {
|
||||
reply.code(200).send(null)
|
||||
})
|
||||
t.assert.ok(true)
|
||||
} catch (e) {
|
||||
t.assert.fail()
|
||||
}
|
||||
})
|
||||
|
||||
test('shorthand - custom head', t => {
|
||||
t.plan(1)
|
||||
try {
|
||||
fastify.head('/proxy/*', function (req, reply) {
|
||||
reply.headers({ 'x-foo': 'bar' })
|
||||
reply.code(200).send(null)
|
||||
})
|
||||
|
||||
fastify.get('/proxy/*', function (req, reply) {
|
||||
reply.code(200).send(null)
|
||||
})
|
||||
|
||||
t.assert.ok(true)
|
||||
} catch (e) {
|
||||
t.assert.fail()
|
||||
}
|
||||
})
|
||||
|
||||
test('shorthand - custom head with constraints', t => {
|
||||
t.plan(1)
|
||||
try {
|
||||
fastify.head('/proxy/*', { constraints: { version: '1.0.0' } }, function (req, reply) {
|
||||
reply.headers({ 'x-foo': 'bar' })
|
||||
reply.code(200).send(null)
|
||||
})
|
||||
|
||||
fastify.get('/proxy/*', { constraints: { version: '1.0.0' } }, function (req, reply) {
|
||||
reply.code(200).send(null)
|
||||
})
|
||||
|
||||
t.assert.ok(true)
|
||||
} catch (e) {
|
||||
t.assert.fail()
|
||||
}
|
||||
})
|
||||
|
||||
test('shorthand - should not reset a head route', t => {
|
||||
t.plan(1)
|
||||
try {
|
||||
fastify.get('/query1', function (req, reply) {
|
||||
reply.code(200).send(null)
|
||||
})
|
||||
|
||||
fastify.put('/query1', function (req, reply) {
|
||||
reply.code(200).send(null)
|
||||
})
|
||||
|
||||
t.assert.ok(true)
|
||||
} catch (e) {
|
||||
t.assert.fail()
|
||||
}
|
||||
})
|
||||
|
||||
test('shorthand - should set get and head route in the same api call', t => {
|
||||
t.plan(1)
|
||||
try {
|
||||
fastify.route({
|
||||
method: ['HEAD', 'GET'],
|
||||
url: '/query4',
|
||||
handler: function (req, reply) {
|
||||
reply.headers({ 'x-foo': 'bar' })
|
||||
reply.code(200).send(null)
|
||||
}
|
||||
})
|
||||
|
||||
t.assert.ok(true)
|
||||
} catch (e) {
|
||||
t.assert.fail()
|
||||
}
|
||||
})
|
||||
|
||||
test('shorthand - head params', t => {
|
||||
t.plan(1)
|
||||
try {
|
||||
fastify.head('/params/:foo/:test', paramsSchema, function (req, reply) {
|
||||
reply.send(null)
|
||||
})
|
||||
t.assert.ok(true)
|
||||
} catch (e) {
|
||||
t.assert.fail()
|
||||
}
|
||||
})
|
||||
|
||||
test('shorthand - head, querystring schema', t => {
|
||||
t.plan(1)
|
||||
try {
|
||||
fastify.head('/query', querySchema, function (req, reply) {
|
||||
reply.code(200).send(null)
|
||||
})
|
||||
t.assert.ok(true)
|
||||
} catch (e) {
|
||||
t.assert.fail()
|
||||
}
|
||||
})
|
||||
|
||||
test('missing schema - head', t => {
|
||||
t.plan(1)
|
||||
try {
|
||||
fastify.head('/missing', function (req, reply) {
|
||||
reply.code(200).send(null)
|
||||
})
|
||||
t.assert.ok(true)
|
||||
} catch (e) {
|
||||
t.assert.fail()
|
||||
}
|
||||
})
|
||||
|
||||
test('head test', async t => {
|
||||
t.after(() => { fastify.close() })
|
||||
const fastifyServer = await fastify.listen({ port: 0 })
|
||||
|
||||
await t.test('shorthand - request head', async t => {
|
||||
t.plan(2)
|
||||
const result = await fetch(fastifyServer, {
|
||||
method: 'HEAD'
|
||||
})
|
||||
t.assert.ok(result.ok)
|
||||
t.assert.strictEqual(result.status, 200)
|
||||
})
|
||||
|
||||
await t.test('shorthand - request head params schema', async t => {
|
||||
t.plan(2)
|
||||
const result = await fetch(`${fastifyServer}/params/world/123`, {
|
||||
method: 'HEAD'
|
||||
})
|
||||
t.assert.ok(result.ok)
|
||||
t.assert.strictEqual(result.status, 200)
|
||||
})
|
||||
|
||||
await t.test('shorthand - request head params schema error', async t => {
|
||||
t.plan(2)
|
||||
const result = await fetch(`${fastifyServer}/params/world/string`, {
|
||||
method: 'HEAD'
|
||||
})
|
||||
t.assert.ok(!result.ok)
|
||||
t.assert.strictEqual(result.status, 400)
|
||||
})
|
||||
|
||||
await t.test('shorthand - request head querystring schema', async t => {
|
||||
t.plan(2)
|
||||
const result = await fetch(`${fastifyServer}/query?hello=123`, {
|
||||
method: 'HEAD'
|
||||
})
|
||||
t.assert.ok(result.ok)
|
||||
t.assert.strictEqual(result.status, 200)
|
||||
})
|
||||
|
||||
await t.test('shorthand - request head querystring schema error', async t => {
|
||||
t.plan(2)
|
||||
const result = await fetch(`${fastifyServer}/query?hello=world`, {
|
||||
method: 'HEAD'
|
||||
})
|
||||
t.assert.ok(!result.ok)
|
||||
t.assert.strictEqual(result.status, 400)
|
||||
})
|
||||
|
||||
await t.test('shorthand - request head missing schema', async t => {
|
||||
t.plan(2)
|
||||
const result = await fetch(`${fastifyServer}/missing`, {
|
||||
method: 'HEAD'
|
||||
})
|
||||
t.assert.ok(result.ok)
|
||||
t.assert.strictEqual(result.status, 200)
|
||||
})
|
||||
|
||||
await t.test('shorthand - request head custom head', async t => {
|
||||
t.plan(3)
|
||||
const result = await fetch(`${fastifyServer}/proxy/test`, {
|
||||
method: 'HEAD'
|
||||
})
|
||||
t.assert.ok(result.ok)
|
||||
t.assert.strictEqual(result.headers.get('x-foo'), 'bar')
|
||||
t.assert.strictEqual(result.status, 200)
|
||||
})
|
||||
|
||||
await t.test('shorthand - request head custom head with constraints', async t => {
|
||||
t.plan(3)
|
||||
const result = await fetch(`${fastifyServer}/proxy/test`, {
|
||||
method: 'HEAD',
|
||||
headers: {
|
||||
version: '1.0.0'
|
||||
}
|
||||
})
|
||||
t.assert.ok(result.ok)
|
||||
t.assert.strictEqual(result.headers.get('x-foo'), 'bar')
|
||||
t.assert.strictEqual(result.status, 200)
|
||||
})
|
||||
|
||||
await t.test('shorthand - should not reset a head route', async t => {
|
||||
t.plan(2)
|
||||
const result = await fetch(`${fastifyServer}/query1`, {
|
||||
method: 'HEAD'
|
||||
})
|
||||
t.assert.ok(result.ok)
|
||||
t.assert.strictEqual(result.status, 200)
|
||||
})
|
||||
|
||||
await t.test('shorthand - should set get and head route in the same api call', async t => {
|
||||
t.plan(3)
|
||||
const result = await fetch(`${fastifyServer}/query4`, {
|
||||
method: 'HEAD'
|
||||
})
|
||||
t.assert.ok(result.ok)
|
||||
t.assert.strictEqual(result.headers.get('x-foo'), 'bar')
|
||||
t.assert.strictEqual(result.status, 200)
|
||||
})
|
||||
})
|
||||
108
node_modules/fastify/test/http-methods/lock.test.js
generated
vendored
Normal file
108
node_modules/fastify/test/http-methods/lock.test.js
generated
vendored
Normal file
@@ -0,0 +1,108 @@
|
||||
'use strict'
|
||||
|
||||
const { test } = require('node:test')
|
||||
const fastify = require('../../fastify')()
|
||||
fastify.addHttpMethod('LOCK', { hasBody: true })
|
||||
|
||||
const bodySample = `<?xml version="1.0" encoding="utf-8" ?>
|
||||
<D:lockinfo xmlns:D='DAV:'>
|
||||
<D:lockscope> <D:exclusive/> </D:lockscope>
|
||||
<D:locktype> <D:write/> </D:locktype>
|
||||
<D:owner>
|
||||
<D:href>http://example.org/~ejw/contact.html</D:href>
|
||||
</D:owner>
|
||||
</D:lockinfo> `
|
||||
|
||||
test('can be created - lock', t => {
|
||||
t.plan(1)
|
||||
try {
|
||||
fastify.route({
|
||||
method: 'LOCK',
|
||||
url: '*',
|
||||
handler: function (req, reply) {
|
||||
reply
|
||||
.code(200)
|
||||
.send(`<?xml version="1.0" encoding="utf-8" ?>
|
||||
<D:prop xmlns:D="DAV:">
|
||||
<D:lockdiscovery>
|
||||
<D:activelock>
|
||||
<D:locktype>
|
||||
<D:write/>
|
||||
</D:locktype>
|
||||
<D:lockscope>
|
||||
<D:exclusive/>
|
||||
</D:lockscope>
|
||||
<D:depth>infinity</D:depth>
|
||||
<D:owner>
|
||||
<D:href>http://example.org/~ejw/contact.html</D:href>
|
||||
</D:owner>
|
||||
<D:timeout>Second-604800</D:timeout>
|
||||
<D:locktoken>
|
||||
<D:href>urn:uuid:e71d4fae-5dec-22d6-fea5-00a0c91e6be4</:href>
|
||||
</D:locktoken>
|
||||
<D:lockroot>
|
||||
<D:href>http://example.com/workspace/webdav/proposal.oc</D:href>
|
||||
</D:lockroot>
|
||||
</D:activelock>
|
||||
</D:lockdiscovery>
|
||||
</D:prop>`
|
||||
)
|
||||
}
|
||||
})
|
||||
t.assert.ok(true)
|
||||
} catch (e) {
|
||||
t.assert.fail()
|
||||
}
|
||||
})
|
||||
|
||||
test('lock test', async t => {
|
||||
const fastifyServer = await fastify.listen({ port: 0 })
|
||||
t.after(() => {
|
||||
fastify.close()
|
||||
})
|
||||
// the body test uses a text/plain content type instead of application/xml because it requires
|
||||
// a specific content type parser
|
||||
await t.test('request with body - lock', async (t) => {
|
||||
t.plan(3)
|
||||
|
||||
const result = await fetch(`${fastifyServer}/test/a.txt`, {
|
||||
method: 'LOCK',
|
||||
headers: { 'content-type': 'text/plain' },
|
||||
body: bodySample
|
||||
})
|
||||
|
||||
t.assert.ok(result.ok)
|
||||
t.assert.strictEqual(result.status, 200)
|
||||
const body = await result.text()
|
||||
t.assert.strictEqual(result.headers.get('content-length'), '' + body.length)
|
||||
})
|
||||
|
||||
await t.test('request with body and no content type (415 error) - lock', async (t) => {
|
||||
t.plan(3)
|
||||
|
||||
const result = await fetch(`${fastifyServer}/test/a.txt`, {
|
||||
method: 'LOCK',
|
||||
body: bodySample,
|
||||
headers: { 'content-type': undefined }
|
||||
})
|
||||
|
||||
t.assert.ok(!result.ok)
|
||||
t.assert.strictEqual(result.status, 415)
|
||||
const body = await result.text()
|
||||
t.assert.strictEqual(result.headers.get('content-length'), '' + body.length)
|
||||
})
|
||||
|
||||
await t.test('request without body - lock', async (t) => {
|
||||
t.plan(3)
|
||||
|
||||
const result = await fetch(`${fastifyServer}/test/a.txt`, {
|
||||
method: 'LOCK',
|
||||
headers: { 'content-type': 'text/plain' }
|
||||
})
|
||||
|
||||
t.assert.ok(result.ok)
|
||||
t.assert.strictEqual(result.status, 200)
|
||||
const body = await result.text()
|
||||
t.assert.strictEqual(result.headers.get('content-length'), '' + body.length)
|
||||
})
|
||||
})
|
||||
143
node_modules/fastify/test/http-methods/mkcalendar.test.js
generated
vendored
Normal file
143
node_modules/fastify/test/http-methods/mkcalendar.test.js
generated
vendored
Normal file
@@ -0,0 +1,143 @@
|
||||
'use strict'
|
||||
|
||||
const { test } = require('node:test')
|
||||
const fastify = require('../../fastify')()
|
||||
fastify.addHttpMethod('MKCALENDAR', { hasBody: true })
|
||||
|
||||
const bodySample = `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<B:mkcalendar xmlns:B="urn:ietf:params:xml:ns:caldav">
|
||||
<A:set xmlns:A="DAV:">
|
||||
<A:prop>
|
||||
<B:calendar-free-busy-set>
|
||||
<NO/>
|
||||
</B:calendar-free-busy-set>
|
||||
<E:calendar-order xmlns:E="http://apple.com/ns/ical/">0</E:calendar-order>
|
||||
<B:supported-calendar-component-set>
|
||||
<B:comp name="VEVENT"/>
|
||||
</B:supported-calendar-component-set>
|
||||
<A:displayname>CALENDAR_NAME</A:displayname>
|
||||
<B:calendar-timezone>BEGIN:VCALENDAR
|
||||
VERSION:2.0
|
||||
</B:calendar-timezone>
|
||||
</A:prop>
|
||||
</A:set>
|
||||
</B:mkcalendar>
|
||||
`
|
||||
|
||||
test('can be created - mkcalendar', (t) => {
|
||||
t.plan(1)
|
||||
try {
|
||||
fastify.route({
|
||||
method: 'MKCALENDAR',
|
||||
url: '*',
|
||||
handler: function (req, reply) {
|
||||
return reply.code(207).send(`<?xml version="1.0" encoding="utf-8"?>
|
||||
<D:multistatus xmlns:D="DAV:">
|
||||
<D:response xmlns:lp1="DAV:">
|
||||
<D:href>/</D:href>
|
||||
<D:propstat>
|
||||
<D:prop>
|
||||
<lp1:resourcetype>
|
||||
<D:collection/>
|
||||
</lp1:resourcetype>
|
||||
<lp1:creationdate>2022-04-13T12:35:30Z</lp1:creationdate>
|
||||
<lp1:getlastmodified>Wed, 13 Apr 2022 12:35:30 GMT</lp1:getlastmodified>
|
||||
<lp1:getetag>"e0-5dc8869b53ef1"</lp1:getetag>
|
||||
<D:supportedlock>
|
||||
<D:lockentry>
|
||||
<D:lockscope>
|
||||
<D:exclusive/>
|
||||
</D:lockscope>
|
||||
<D:locktype>
|
||||
<D:write/>
|
||||
</D:locktype>
|
||||
</D:lockentry>
|
||||
<D:lockentry>
|
||||
<D:lockscope>
|
||||
<D:shared/>
|
||||
</D:lockscope>
|
||||
<D:locktype>
|
||||
<D:write/>
|
||||
</D:locktype>
|
||||
</D:lockentry>
|
||||
</D:supportedlock>
|
||||
<D:lockdiscovery/>
|
||||
<D:getcontenttype>httpd/unix-directory</D:getcontenttype>
|
||||
</D:prop>
|
||||
<D:status>HTTP/1.1 200 OK</D:status>
|
||||
</D:propstat>
|
||||
</D:response>
|
||||
</D:multistatus>`)
|
||||
}
|
||||
})
|
||||
t.assert.ok(true)
|
||||
} catch (e) {
|
||||
t.assert.fail()
|
||||
}
|
||||
})
|
||||
|
||||
test('mkcalendar test', async t => {
|
||||
const fastifyServer = await fastify.listen({ port: 0 })
|
||||
t.after(() => {
|
||||
fastify.close()
|
||||
})
|
||||
|
||||
await t.test('request - mkcalendar', async t => {
|
||||
t.plan(2)
|
||||
const result = await fetch(`${fastifyServer}/`, {
|
||||
method: 'MKCALENDAR'
|
||||
})
|
||||
t.assert.strictEqual(result.status, 207)
|
||||
const body = await result.text()
|
||||
t.assert.strictEqual(result.headers.get('content-length'), '' + body.length)
|
||||
})
|
||||
|
||||
await t.test('request with other path - mkcalendar', async t => {
|
||||
t.plan(2)
|
||||
const result = await fetch(`${fastifyServer}/test`, {
|
||||
method: 'MKCALENDAR'
|
||||
})
|
||||
t.assert.strictEqual(result.status, 207)
|
||||
const body = await result.text()
|
||||
t.assert.strictEqual(result.headers.get('content-length'), '' + body.length)
|
||||
})
|
||||
|
||||
// the body test uses a text/plain content type instead of application/xml because it requires
|
||||
// a specific content type parser
|
||||
await t.test('request with body - mkcalendar', async t => {
|
||||
t.plan(3)
|
||||
const result = await fetch(`${fastifyServer}/test`, {
|
||||
method: 'MKCALENDAR',
|
||||
headers: { 'content-type': 'text/plain' },
|
||||
body: bodySample
|
||||
})
|
||||
t.assert.ok(result.ok)
|
||||
t.assert.strictEqual(result.status, 207)
|
||||
const body = await result.text()
|
||||
t.assert.strictEqual(result.headers.get('content-length'), '' + body.length)
|
||||
})
|
||||
|
||||
await t.test('request with body and no content type (415 error) - mkcalendar', async t => {
|
||||
t.plan(3)
|
||||
const result = await fetch(`${fastifyServer}/test`, {
|
||||
method: 'MKCALENDAR',
|
||||
body: bodySample,
|
||||
headers: { 'content-type': undefined }
|
||||
})
|
||||
t.assert.ok(!result.ok)
|
||||
t.assert.strictEqual(result.status, 415)
|
||||
const body = await result.text()
|
||||
t.assert.strictEqual(result.headers.get('content-length'), '' + body.length)
|
||||
})
|
||||
|
||||
await t.test('request without body - mkcalendar', async t => {
|
||||
t.plan(3)
|
||||
const result = await fetch(`${fastifyServer}/test`, {
|
||||
method: 'MKCALENDAR'
|
||||
})
|
||||
t.assert.ok(result.ok)
|
||||
t.assert.strictEqual(result.status, 207)
|
||||
const body = await result.text()
|
||||
t.assert.strictEqual(result.headers.get('content-length'), '' + body.length)
|
||||
})
|
||||
})
|
||||
35
node_modules/fastify/test/http-methods/mkcol.test.js
generated
vendored
Normal file
35
node_modules/fastify/test/http-methods/mkcol.test.js
generated
vendored
Normal file
@@ -0,0 +1,35 @@
|
||||
'use strict'
|
||||
|
||||
const { test } = require('node:test')
|
||||
const fastify = require('../../')()
|
||||
fastify.addHttpMethod('MKCOL')
|
||||
|
||||
test('can be created - mkcol', t => {
|
||||
t.plan(1)
|
||||
try {
|
||||
fastify.route({
|
||||
method: 'MKCOL',
|
||||
url: '*',
|
||||
handler: function (req, reply) {
|
||||
reply.code(201).send()
|
||||
}
|
||||
})
|
||||
t.assert.ok(true)
|
||||
} catch (e) {
|
||||
t.assert.fail()
|
||||
}
|
||||
})
|
||||
|
||||
test('mkcol test', async t => {
|
||||
const fastifyServer = await fastify.listen({ port: 0 })
|
||||
t.after(() => { fastify.close() })
|
||||
|
||||
await t.test('request - mkcol', async t => {
|
||||
t.plan(2)
|
||||
const result = await fetch(`${fastifyServer}/test/`, {
|
||||
method: 'MKCOL'
|
||||
})
|
||||
t.assert.ok(result.ok)
|
||||
t.assert.strictEqual(result.status, 201)
|
||||
})
|
||||
})
|
||||
42
node_modules/fastify/test/http-methods/move.test.js
generated
vendored
Normal file
42
node_modules/fastify/test/http-methods/move.test.js
generated
vendored
Normal file
@@ -0,0 +1,42 @@
|
||||
'use strict'
|
||||
|
||||
const { test } = require('node:test')
|
||||
const fastify = require('../../')()
|
||||
fastify.addHttpMethod('MOVE')
|
||||
|
||||
test('shorthand - move', t => {
|
||||
t.plan(1)
|
||||
try {
|
||||
fastify.route({
|
||||
method: 'MOVE',
|
||||
url: '*',
|
||||
handler: function (req, reply) {
|
||||
const destination = req.headers.destination
|
||||
reply.code(201)
|
||||
.header('location', destination)
|
||||
.send()
|
||||
}
|
||||
})
|
||||
t.assert.ok(true)
|
||||
} catch (e) {
|
||||
t.assert.fail()
|
||||
}
|
||||
})
|
||||
test('move test', async t => {
|
||||
const fastifyServer = await fastify.listen({ port: 0 })
|
||||
|
||||
t.after(() => { fastify.close() })
|
||||
|
||||
await t.test('request - move', async t => {
|
||||
t.plan(3)
|
||||
const result = await fetch(`${fastifyServer}/test.txt`, {
|
||||
method: 'MOVE',
|
||||
headers: {
|
||||
Destination: '/test2.txt'
|
||||
}
|
||||
})
|
||||
t.assert.ok(result.ok)
|
||||
t.assert.strictEqual(result.status, 201)
|
||||
t.assert.strictEqual(result.headers.get('location'), '/test2.txt')
|
||||
})
|
||||
})
|
||||
136
node_modules/fastify/test/http-methods/propfind.test.js
generated
vendored
Normal file
136
node_modules/fastify/test/http-methods/propfind.test.js
generated
vendored
Normal file
@@ -0,0 +1,136 @@
|
||||
'use strict'
|
||||
|
||||
const { test } = require('node:test')
|
||||
const fastify = require('../../')()
|
||||
fastify.addHttpMethod('PROPFIND', { hasBody: true })
|
||||
|
||||
const bodySample = `<?xml version="1.0" encoding="utf-8" ?>
|
||||
<D:propfind xmlns:D="DAV:">
|
||||
<D:prop xmlns:R="http://ns.example.com/boxschema/">
|
||||
<R:bigbox/> <R:author/> <R:DingALing/> <R:Random/>
|
||||
</D:prop>
|
||||
</D:propfind>
|
||||
`
|
||||
|
||||
test('can be created - propfind', t => {
|
||||
t.plan(1)
|
||||
try {
|
||||
fastify.route({
|
||||
method: 'PROPFIND',
|
||||
url: '*',
|
||||
handler: function (req, reply) {
|
||||
return reply.code(207)
|
||||
.send(`<?xml version="1.0" encoding="utf-8"?>
|
||||
<D:multistatus xmlns:D="DAV:">
|
||||
<D:response xmlns:lp1="DAV:">
|
||||
<D:href>/</D:href>
|
||||
<D:propstat>
|
||||
<D:prop>
|
||||
<lp1:resourcetype>
|
||||
<D:collection/>
|
||||
</lp1:resourcetype>
|
||||
<lp1:creationdate>2022-04-13T12:35:30Z</lp1:creationdate>
|
||||
<lp1:getlastmodified>Wed, 13 Apr 2022 12:35:30 GMT</lp1:getlastmodified>
|
||||
<lp1:getetag>"e0-5dc8869b53ef1"</lp1:getetag>
|
||||
<D:supportedlock>
|
||||
<D:lockentry>
|
||||
<D:lockscope>
|
||||
<D:exclusive/>
|
||||
</D:lockscope>
|
||||
<D:locktype>
|
||||
<D:write/>
|
||||
</D:locktype>
|
||||
</D:lockentry>
|
||||
<D:lockentry>
|
||||
<D:lockscope>
|
||||
<D:shared/>
|
||||
</D:lockscope>
|
||||
<D:locktype>
|
||||
<D:write/>
|
||||
</D:locktype>
|
||||
</D:lockentry>
|
||||
</D:supportedlock>
|
||||
<D:lockdiscovery/>
|
||||
<D:getcontenttype>httpd/unix-directory</D:getcontenttype>
|
||||
</D:prop>
|
||||
<D:status>HTTP/1.1 200 OK</D:status>
|
||||
</D:propstat>
|
||||
</D:response>
|
||||
</D:multistatus>`
|
||||
)
|
||||
}
|
||||
})
|
||||
t.assert.ok(true)
|
||||
} catch (e) {
|
||||
t.assert.fail()
|
||||
}
|
||||
})
|
||||
|
||||
test('propfind test', async t => {
|
||||
await fastify.listen({ port: 0 })
|
||||
|
||||
t.after(() => {
|
||||
fastify.close()
|
||||
})
|
||||
|
||||
await t.test('request - propfind', async t => {
|
||||
t.plan(3)
|
||||
const result = await fetch(`http://localhost:${fastify.server.address().port}/`, {
|
||||
method: 'PROPFIND'
|
||||
})
|
||||
t.assert.ok(result.ok)
|
||||
t.assert.strictEqual(result.status, 207)
|
||||
const body = await result.text()
|
||||
t.assert.strictEqual(result.headers.get('content-length'), '' + body.length)
|
||||
})
|
||||
|
||||
await t.test('request with other path - propfind', async t => {
|
||||
t.plan(3)
|
||||
const result = await fetch(`http://localhost:${fastify.server.address().port}/test`, {
|
||||
method: 'PROPFIND'
|
||||
})
|
||||
t.assert.ok(result.ok)
|
||||
t.assert.strictEqual(result.status, 207)
|
||||
const body = await result.text()
|
||||
t.assert.strictEqual(result.headers.get('content-length'), '' + body.length)
|
||||
})
|
||||
|
||||
// the body test uses a text/plain content type instead of application/xml because it requires
|
||||
// a specific content type parser
|
||||
await t.test('request with body - propfind', async t => {
|
||||
t.plan(3)
|
||||
const result = await fetch(`http://localhost:${fastify.server.address().port}/test`, {
|
||||
method: 'PROPFIND',
|
||||
headers: { 'content-type': 'text/plain' },
|
||||
body: bodySample
|
||||
})
|
||||
t.assert.ok(result.ok)
|
||||
t.assert.strictEqual(result.status, 207)
|
||||
const body = await result.text()
|
||||
t.assert.strictEqual(result.headers.get('content-length'), '' + body.length)
|
||||
})
|
||||
|
||||
await t.test('request with body and no content type (415 error) - propfind', async t => {
|
||||
t.plan(3)
|
||||
const result = await fetch(`http://localhost:${fastify.server.address().port}/test`, {
|
||||
method: 'PROPFIND',
|
||||
body: bodySample,
|
||||
headers: { 'content-type': '' }
|
||||
})
|
||||
t.assert.ok(!result.ok)
|
||||
t.assert.strictEqual(result.status, 415)
|
||||
const body = await result.text()
|
||||
t.assert.strictEqual(result.headers.get('content-length'), '' + body.length)
|
||||
})
|
||||
|
||||
await t.test('request without body - propfind', async t => {
|
||||
t.plan(3)
|
||||
const result = await fetch(`http://localhost:${fastify.server.address().port}/test`, {
|
||||
method: 'PROPFIND'
|
||||
})
|
||||
t.assert.ok(result.ok)
|
||||
t.assert.strictEqual(result.status, 207)
|
||||
const body = await result.text()
|
||||
t.assert.strictEqual(result.headers.get('content-length'), '' + body.length)
|
||||
})
|
||||
})
|
||||
105
node_modules/fastify/test/http-methods/proppatch.test.js
generated
vendored
Normal file
105
node_modules/fastify/test/http-methods/proppatch.test.js
generated
vendored
Normal file
@@ -0,0 +1,105 @@
|
||||
'use strict'
|
||||
|
||||
const { test } = require('node:test')
|
||||
const fastify = require('../../')()
|
||||
fastify.addHttpMethod('PROPPATCH', { hasBody: true })
|
||||
|
||||
const bodySample = `<?xml version="1.0" encoding="utf-8" ?>
|
||||
<D:propertyupdate xmlns:D="DAV:"
|
||||
xmlns:Z="http://ns.example.com/standards/z39.50/">
|
||||
<D:set>
|
||||
<D:prop>
|
||||
<Z:Authors>
|
||||
<Z:Author>Jim Whitehead</Z:Author>
|
||||
<Z:Author>Roy Fielding</Z:Author>
|
||||
</Z:Authors>
|
||||
</D:prop>
|
||||
</D:set>
|
||||
<D:remove>
|
||||
<D:prop>
|
||||
<Z:Copyright-Owner/>
|
||||
</D:prop>
|
||||
</D:remove>
|
||||
</D:propertyupdate>`
|
||||
|
||||
test('shorthand - proppatch', t => {
|
||||
t.plan(1)
|
||||
try {
|
||||
fastify.route({
|
||||
method: 'PROPPATCH',
|
||||
url: '*',
|
||||
handler: function (req, reply) {
|
||||
reply
|
||||
.code(207)
|
||||
.send(`<?xml version="1.0" encoding="utf-8" ?>
|
||||
<D:multistatus xmlns:D="DAV:"
|
||||
xmlns:Z="http://ns.example.com/standards/z39.50/">
|
||||
<D:response>
|
||||
<D:href>http://www.example.com/bar.html</D:href>
|
||||
<D:propstat>
|
||||
<D:prop>
|
||||
<Z:Authors/>
|
||||
</D:prop>
|
||||
<D:status>HTTP/1.1 424 Failed Dependency</D:status>
|
||||
</D:propstat>
|
||||
<D:propstat>
|
||||
<D:prop>
|
||||
<Z:Copyright-Owner/>
|
||||
</D:prop>
|
||||
<D:status>HTTP/1.1 409 Conflict</D:status>
|
||||
</D:propstat>
|
||||
<D:responsedescription> Copyright Owner cannot be deleted or altered.</D:responsedescription>
|
||||
</D:response>
|
||||
</D:multistatus>`
|
||||
)
|
||||
}
|
||||
})
|
||||
t.assert.ok(true)
|
||||
} catch (e) {
|
||||
t.assert.fail()
|
||||
}
|
||||
})
|
||||
|
||||
test('proppatch test', async t => {
|
||||
const fastifyServer = await fastify.listen({ port: 0 })
|
||||
|
||||
t.after(() => { fastify.close() })
|
||||
// the body test uses a text/plain content type instead of application/xml because it requires
|
||||
// a specific content type parser
|
||||
await t.test('request with body - proppatch', async t => {
|
||||
t.plan(3)
|
||||
const result = await fetch(`${fastifyServer}/test/a.txt`, {
|
||||
method: 'PROPPATCH',
|
||||
headers: { 'content-type': 'text/plain' },
|
||||
body: bodySample
|
||||
})
|
||||
t.assert.ok(result.ok)
|
||||
t.assert.strictEqual(result.status, 207)
|
||||
const body = await result.text()
|
||||
t.assert.strictEqual(result.headers.get('content-length'), '' + body.length)
|
||||
})
|
||||
|
||||
await t.test('request with body and no content type (415 error) - proppatch', async t => {
|
||||
t.plan(3)
|
||||
const result = await fetch(`${fastifyServer}/test/a.txt`, {
|
||||
method: 'PROPPATCH',
|
||||
body: bodySample,
|
||||
headers: { 'content-type': undefined }
|
||||
})
|
||||
t.assert.ok(!result.ok)
|
||||
t.assert.strictEqual(result.status, 415)
|
||||
const body = await result.text()
|
||||
t.assert.strictEqual(result.headers.get('content-length'), '' + body.length)
|
||||
})
|
||||
|
||||
await t.test('request without body - proppatch', async t => {
|
||||
t.plan(3)
|
||||
const result = await fetch(`${fastifyServer}/test/a.txt`, {
|
||||
method: 'PROPPATCH'
|
||||
})
|
||||
t.assert.ok(result.ok)
|
||||
t.assert.strictEqual(result.status, 207)
|
||||
const body = await result.text()
|
||||
t.assert.strictEqual(result.headers.get('content-length'), '' + body.length)
|
||||
})
|
||||
})
|
||||
142
node_modules/fastify/test/http-methods/report.test.js
generated
vendored
Normal file
142
node_modules/fastify/test/http-methods/report.test.js
generated
vendored
Normal file
@@ -0,0 +1,142 @@
|
||||
'use strict'
|
||||
|
||||
const { test } = require('node:test')
|
||||
const fastify = require('../../fastify')()
|
||||
fastify.addHttpMethod('REPORT', { hasBody: true })
|
||||
|
||||
const bodySample = `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<B:calendar-query xmlns:B="urn:ietf:params:xml:ns:caldav">
|
||||
<A:prop xmlns:A="DAV:">
|
||||
<A:getetag/>
|
||||
<A:getcontenttype/>
|
||||
</A:prop>
|
||||
<B:filter>
|
||||
<B:comp-filter name="VCALENDAR">
|
||||
<B:comp-filter name="VEVENT">
|
||||
<B:time-range start="20170215T000000Z"/>
|
||||
</B:comp-filter>
|
||||
</B:comp-filter>
|
||||
</B:filter>
|
||||
</B:calendar-query>
|
||||
`
|
||||
|
||||
test('can be created - report', (t) => {
|
||||
t.plan(1)
|
||||
try {
|
||||
fastify.route({
|
||||
method: 'REPORT',
|
||||
url: '*',
|
||||
handler: function (req, reply) {
|
||||
return reply.code(207).send(`<?xml version="1.0" encoding="utf-8"?>
|
||||
<D:multistatus xmlns:D="DAV:">
|
||||
<D:response xmlns:lp1="DAV:">
|
||||
<D:href>/</D:href>
|
||||
<D:propstat>
|
||||
<D:prop>
|
||||
<lp1:resourcetype>
|
||||
<D:collection/>
|
||||
</lp1:resourcetype>
|
||||
<lp1:creationdate>2022-04-13T12:35:30Z</lp1:creationdate>
|
||||
<lp1:getlastmodified>Wed, 13 Apr 2022 12:35:30 GMT</lp1:getlastmodified>
|
||||
<lp1:getetag>"e0-5dc8869b53ef1"</lp1:getetag>
|
||||
<D:supportedlock>
|
||||
<D:lockentry>
|
||||
<D:lockscope>
|
||||
<D:exclusive/>
|
||||
</D:lockscope>
|
||||
<D:locktype>
|
||||
<D:write/>
|
||||
</D:locktype>
|
||||
</D:lockentry>
|
||||
<D:lockentry>
|
||||
<D:lockscope>
|
||||
<D:shared/>
|
||||
</D:lockscope>
|
||||
<D:locktype>
|
||||
<D:write/>
|
||||
</D:locktype>
|
||||
</D:lockentry>
|
||||
</D:supportedlock>
|
||||
<D:lockdiscovery/>
|
||||
<D:getcontenttype>httpd/unix-directory</D:getcontenttype>
|
||||
</D:prop>
|
||||
<D:status>HTTP/1.1 200 OK</D:status>
|
||||
</D:propstat>
|
||||
</D:response>
|
||||
</D:multistatus>`)
|
||||
}
|
||||
})
|
||||
t.assert.ok(true)
|
||||
} catch (e) {
|
||||
t.assert.fail()
|
||||
}
|
||||
})
|
||||
|
||||
test('report test', async t => {
|
||||
await fastify.listen({ port: 0 })
|
||||
|
||||
t.after(() => {
|
||||
fastify.close()
|
||||
})
|
||||
|
||||
await t.test('request - report', async (t) => {
|
||||
t.plan(3)
|
||||
const result = await fetch(`http://localhost:${fastify.server.address().port}/`, {
|
||||
method: 'REPORT'
|
||||
})
|
||||
|
||||
t.assert.ok(result.ok)
|
||||
t.assert.strictEqual(result.status, 207)
|
||||
t.assert.strictEqual(result.headers.get('content-length'), '' + (await result.text()).length)
|
||||
})
|
||||
|
||||
await t.test('request with other path - report', async (t) => {
|
||||
t.plan(3)
|
||||
const result = await fetch(`http://localhost:${fastify.server.address().port}/test`, {
|
||||
method: 'REPORT'
|
||||
})
|
||||
|
||||
t.assert.ok(result.ok)
|
||||
t.assert.strictEqual(result.status, 207)
|
||||
t.assert.strictEqual(result.headers.get('content-length'), '' + (await result.text()).length)
|
||||
})
|
||||
|
||||
// the body test uses a text/plain content type instead of application/xml because it requires
|
||||
// a specific content type parser
|
||||
await t.test('request with body - report', async (t) => {
|
||||
t.plan(3)
|
||||
const result = await fetch(`http://localhost:${fastify.server.address().port}/test`, {
|
||||
method: 'REPORT',
|
||||
headers: { 'content-type': 'text/plain' },
|
||||
body: bodySample
|
||||
})
|
||||
|
||||
t.assert.ok(result.ok)
|
||||
t.assert.strictEqual(result.status, 207)
|
||||
t.assert.strictEqual(result.headers.get('content-length'), '' + (await result.text()).length)
|
||||
})
|
||||
|
||||
await t.test('request with body and no content type (415 error) - report', async (t) => {
|
||||
t.plan(3)
|
||||
const result = await fetch(`http://localhost:${fastify.server.address().port}/test`, {
|
||||
method: 'REPORT',
|
||||
body: bodySample,
|
||||
headers: { 'content-type': '' }
|
||||
})
|
||||
|
||||
t.assert.ok(!result.ok)
|
||||
t.assert.strictEqual(result.status, 415)
|
||||
t.assert.strictEqual(result.headers.get('content-length'), '' + (await result.text()).length)
|
||||
})
|
||||
|
||||
await t.test('request without body - report', async (t) => {
|
||||
t.plan(3)
|
||||
const result = await fetch(`http://localhost:${fastify.server.address().port}/test`, {
|
||||
method: 'REPORT'
|
||||
})
|
||||
|
||||
t.assert.ok(result.ok)
|
||||
t.assert.strictEqual(result.status, 207)
|
||||
t.assert.strictEqual(result.headers.get('content-length'), '' + (await result.text()).length)
|
||||
})
|
||||
})
|
||||
233
node_modules/fastify/test/http-methods/search.test.js
generated
vendored
Normal file
233
node_modules/fastify/test/http-methods/search.test.js
generated
vendored
Normal file
@@ -0,0 +1,233 @@
|
||||
'use strict'
|
||||
|
||||
const { test } = require('node:test')
|
||||
const fastify = require('../../fastify')()
|
||||
fastify.addHttpMethod('SEARCH', { hasBody: true })
|
||||
|
||||
const schema = {
|
||||
response: {
|
||||
'2xx': {
|
||||
type: 'object',
|
||||
properties: {
|
||||
hello: {
|
||||
type: 'string'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const querySchema = {
|
||||
querystring: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
hello: {
|
||||
type: 'integer'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const paramsSchema = {
|
||||
params: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
foo: {
|
||||
type: 'string'
|
||||
},
|
||||
test: {
|
||||
type: 'integer'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const bodySchema = {
|
||||
body: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
foo: {
|
||||
type: 'string'
|
||||
},
|
||||
test: {
|
||||
type: 'integer'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
test('search', t => {
|
||||
t.plan(1)
|
||||
try {
|
||||
fastify.route({
|
||||
method: 'SEARCH',
|
||||
url: '/',
|
||||
schema,
|
||||
handler: function (request, reply) {
|
||||
reply.code(200).send({ hello: 'world' })
|
||||
}
|
||||
})
|
||||
t.assert.ok(true)
|
||||
} catch (e) {
|
||||
t.assert.fail()
|
||||
}
|
||||
})
|
||||
|
||||
test('search, params schema', t => {
|
||||
t.plan(1)
|
||||
try {
|
||||
fastify.route({
|
||||
method: 'SEARCH',
|
||||
url: '/params/:foo/:test',
|
||||
schema: paramsSchema,
|
||||
handler: function (request, reply) {
|
||||
reply.code(200).send(request.params)
|
||||
}
|
||||
})
|
||||
t.assert.ok(true)
|
||||
} catch (e) {
|
||||
t.assert.fail()
|
||||
}
|
||||
})
|
||||
|
||||
test('search, querystring schema', t => {
|
||||
t.plan(1)
|
||||
try {
|
||||
fastify.route({
|
||||
method: 'SEARCH',
|
||||
url: '/query',
|
||||
schema: querySchema,
|
||||
handler: function (request, reply) {
|
||||
reply.code(200).send(request.query)
|
||||
}
|
||||
})
|
||||
t.assert.ok(true)
|
||||
} catch (e) {
|
||||
t.assert.fail()
|
||||
}
|
||||
})
|
||||
|
||||
test('search, body schema', t => {
|
||||
t.plan(1)
|
||||
try {
|
||||
fastify.route({
|
||||
method: 'SEARCH',
|
||||
url: '/body',
|
||||
schema: bodySchema,
|
||||
handler: function (request, reply) {
|
||||
reply.code(200).send(request.body)
|
||||
}
|
||||
})
|
||||
t.assert.ok(true)
|
||||
} catch (e) {
|
||||
t.assert.fail()
|
||||
}
|
||||
})
|
||||
|
||||
test('search test', async t => {
|
||||
await fastify.listen({ port: 0 })
|
||||
t.after(() => { fastify.close() })
|
||||
const url = `http://localhost:${fastify.server.address().port}`
|
||||
|
||||
await t.test('request - search', async t => {
|
||||
t.plan(4)
|
||||
const result = await fetch(url, {
|
||||
method: 'SEARCH'
|
||||
})
|
||||
const body = await result.text()
|
||||
t.assert.ok(result.ok)
|
||||
t.assert.strictEqual(result.status, 200)
|
||||
t.assert.strictEqual(result.headers.get('content-length'), '' + body.length)
|
||||
t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
|
||||
})
|
||||
|
||||
await t.test('request search params schema', async t => {
|
||||
t.plan(4)
|
||||
const result = await fetch(`${url}/params/world/123`, {
|
||||
method: 'SEARCH'
|
||||
})
|
||||
const body = await result.text()
|
||||
t.assert.ok(result.ok)
|
||||
t.assert.strictEqual(result.status, 200)
|
||||
t.assert.strictEqual(result.headers.get('content-length'), '' + body.length)
|
||||
t.assert.deepStrictEqual(JSON.parse(body), { foo: 'world', test: 123 })
|
||||
})
|
||||
|
||||
await t.test('request search params schema error', async t => {
|
||||
t.plan(3)
|
||||
const result = await fetch(`${url}/params/world/string`, {
|
||||
method: 'SEARCH'
|
||||
})
|
||||
const body = await result.text()
|
||||
t.assert.strictEqual(result.status, 400)
|
||||
t.assert.strictEqual(result.headers.get('content-length'), '' + body.length)
|
||||
t.assert.deepStrictEqual(JSON.parse(body), {
|
||||
error: 'Bad Request',
|
||||
code: 'FST_ERR_VALIDATION',
|
||||
message: 'params/test must be integer',
|
||||
statusCode: 400
|
||||
})
|
||||
})
|
||||
|
||||
await t.test('request search querystring schema', async t => {
|
||||
t.plan(4)
|
||||
const result = await fetch(`${url}/query?hello=123`, {
|
||||
method: 'SEARCH'
|
||||
})
|
||||
const body = await result.text()
|
||||
t.assert.ok(result.ok)
|
||||
t.assert.strictEqual(result.status, 200)
|
||||
t.assert.strictEqual(result.headers.get('content-length'), '' + body.length)
|
||||
t.assert.deepStrictEqual(JSON.parse(body), { hello: 123 })
|
||||
})
|
||||
|
||||
await t.test('request search querystring schema error', async t => {
|
||||
t.plan(3)
|
||||
const result = await fetch(`${url}/query?hello=world`, {
|
||||
method: 'SEARCH'
|
||||
})
|
||||
const body = await result.text()
|
||||
t.assert.strictEqual(result.status, 400)
|
||||
t.assert.strictEqual(result.headers.get('content-length'), '' + body.length)
|
||||
t.assert.deepStrictEqual(JSON.parse(body), {
|
||||
error: 'Bad Request',
|
||||
code: 'FST_ERR_VALIDATION',
|
||||
message: 'querystring/hello must be integer',
|
||||
statusCode: 400
|
||||
})
|
||||
})
|
||||
|
||||
await t.test('request search body schema', async t => {
|
||||
t.plan(4)
|
||||
const replyBody = { foo: 'bar', test: 5 }
|
||||
const result = await fetch(`${url}/body`, {
|
||||
method: 'SEARCH',
|
||||
body: JSON.stringify(replyBody),
|
||||
headers: { 'content-type': 'application/json' }
|
||||
})
|
||||
const body = await result.text()
|
||||
t.assert.ok(result.ok)
|
||||
t.assert.strictEqual(result.status, 200)
|
||||
t.assert.strictEqual(result.headers.get('content-length'), '' + body.length)
|
||||
t.assert.deepStrictEqual(JSON.parse(body), replyBody)
|
||||
})
|
||||
|
||||
await t.test('request search body schema error', async t => {
|
||||
t.plan(4)
|
||||
const result = await fetch(`${url}/body`, {
|
||||
method: 'SEARCH',
|
||||
body: JSON.stringify({ foo: 'bar', test: 'test' }),
|
||||
headers: { 'content-type': 'application/json' }
|
||||
})
|
||||
const body = await result.text()
|
||||
t.assert.ok(!result.ok)
|
||||
t.assert.strictEqual(result.status, 400)
|
||||
t.assert.strictEqual(result.headers.get('content-length'), '' + body.length)
|
||||
t.assert.deepStrictEqual(JSON.parse(body), {
|
||||
error: 'Bad Request',
|
||||
code: 'FST_ERR_VALIDATION',
|
||||
message: 'body/test must be integer',
|
||||
statusCode: 400
|
||||
})
|
||||
})
|
||||
})
|
||||
21
node_modules/fastify/test/http-methods/trace.test.js
generated
vendored
Normal file
21
node_modules/fastify/test/http-methods/trace.test.js
generated
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
'use strict'
|
||||
|
||||
const { test } = require('node:test')
|
||||
const fastify = require('../../fastify')()
|
||||
fastify.addHttpMethod('TRACE')
|
||||
|
||||
test('shorthand - trace', t => {
|
||||
t.plan(1)
|
||||
try {
|
||||
fastify.route({
|
||||
method: 'TRACE',
|
||||
url: '/',
|
||||
handler: function (request, reply) {
|
||||
reply.code(200).send('TRACE OK')
|
||||
}
|
||||
})
|
||||
t.assert.ok(true)
|
||||
} catch (e) {
|
||||
t.assert.fail()
|
||||
}
|
||||
})
|
||||
38
node_modules/fastify/test/http-methods/unlock.test.js
generated
vendored
Normal file
38
node_modules/fastify/test/http-methods/unlock.test.js
generated
vendored
Normal file
@@ -0,0 +1,38 @@
|
||||
'use strict'
|
||||
|
||||
const { test } = require('node:test')
|
||||
const fastify = require('../../fastify')()
|
||||
fastify.addHttpMethod('UNLOCK')
|
||||
|
||||
test('can be created - unlock', t => {
|
||||
t.plan(1)
|
||||
try {
|
||||
fastify.route({
|
||||
method: 'UNLOCK',
|
||||
url: '*',
|
||||
handler: function (req, reply) {
|
||||
reply.code(204).send()
|
||||
}
|
||||
})
|
||||
t.assert.ok(true)
|
||||
} catch (e) {
|
||||
t.assert.fail()
|
||||
}
|
||||
})
|
||||
|
||||
test('unlock test', async t => {
|
||||
const fastifyServer = await fastify.listen({ port: 0 })
|
||||
|
||||
t.after(() => { fastify.close() })
|
||||
await t.test('request - unlock', async t => {
|
||||
t.plan(2)
|
||||
const result = await fetch(`${fastifyServer}/test/a.txt`, {
|
||||
method: 'UNLOCK',
|
||||
headers: {
|
||||
'Lock-Token': 'urn:uuid:a515cfa4-5da4-22e1-f5b5-00a0451e6bf7'
|
||||
}
|
||||
})
|
||||
t.assert.ok(result.ok)
|
||||
t.assert.strictEqual(result.status, 204)
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user