'use strict' const { test } = require('node:test') const sget = require('simple-get').concat const fastify = require('../../fastify')() fastify.addHttpMethod('LOCK', { hasBody: true }) const bodySample = ` http://example.org/~ejw/contact.html ` test('can be created - lock', t => { t.plan(1) try { fastify.route({ method: 'LOCK', url: '*', handler: function (req, reply) { reply .code(200) .send(` infinity http://example.org/~ejw/contact.html Second-604800 urn:uuid:e71d4fae-5dec-22d6-fea5-00a0c91e6be4 http://example.com/workspace/webdav/proposal.oc ` ) } }) t.assert.ok(true) } catch (e) { t.assert.fail() } }) test('lock test', async t => { 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', (t, done) => { t.plan(3) sget({ url: `http://localhost:${fastify.server.address().port}/test/a.txt`, headers: { 'content-type': 'text/plain' }, body: bodySample, method: 'LOCK' }, (err, response, body) => { t.assert.ifError(err) t.assert.strictEqual(response.statusCode, 200) t.assert.strictEqual(response.headers['content-length'], '' + body.length) done() }) }) await t.test('request with body and no content type (415 error) - lock', (t, done) => { t.plan(3) sget({ url: `http://localhost:${fastify.server.address().port}/test/a.txt`, body: bodySample, method: 'LOCK' }, (err, response, body) => { t.assert.ifError(err) t.assert.strictEqual(response.statusCode, 415) t.assert.strictEqual(response.headers['content-length'], '' + body.length) done() }) }) await t.test('request without body - lock', (t, done) => { t.plan(3) sget({ url: `http://localhost:${fastify.server.address().port}/test/a.txt`, headers: { 'content-type': 'text/plain' }, method: 'LOCK' }, (err, response, body) => { t.assert.ifError(err) t.assert.strictEqual(response.statusCode, 200) t.assert.strictEqual(response.headers['content-length'], '' + body.length) done() }) }) })