Spaces:
Runtime error
Runtime error
File size: 4,992 Bytes
23ac194 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
'use strict'
const { test } = require('node:test')
const sget = require('simple-get').concat
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', (t, done) => {
t.plan(3)
sget(
{
url: `http://localhost:${fastify.server.address().port}/`,
method: 'REPORT'
},
(err, response, body) => {
t.assert.ifError(err)
t.assert.strictEqual(response.statusCode, 207)
t.assert.strictEqual(response.headers['content-length'], '' + body.length)
done()
}
)
})
await t.test('request with other path - report', (t, done) => {
t.plan(3)
sget(
{
url: `http://localhost:${fastify.server.address().port}/test`,
method: 'REPORT'
},
(err, response, body) => {
t.assert.ifError(err)
t.assert.strictEqual(response.statusCode, 207)
t.assert.strictEqual(response.headers['content-length'], '' + body.length)
done()
}
)
})
// 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', (t, done) => {
t.plan(3)
sget(
{
url: `http://localhost:${fastify.server.address().port}/test`,
headers: { 'content-type': 'text/plain' },
body: bodySample,
method: 'REPORT'
},
(err, response, body) => {
t.assert.ifError(err)
t.assert.strictEqual(response.statusCode, 207)
t.assert.strictEqual(response.headers['content-length'], '' + body.length)
done()
}
)
})
await t.test('request with body and no content type (415 error) - report', (t, done) => {
t.plan(3)
sget(
{
url: `http://localhost:${fastify.server.address().port}/test`,
body: bodySample,
method: 'REPORT'
},
(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 - report', (t, done) => {
t.plan(3)
sget(
{
url: `http://localhost:${fastify.server.address().port}/test`,
method: 'REPORT'
},
(err, response, body) => {
t.assert.ifError(err)
t.assert.strictEqual(response.statusCode, 207)
t.assert.strictEqual(response.headers['content-length'], '' + body.length)
done()
}
)
})
})
|