|
1 | 1 | 'use strict'
|
2 | 2 |
|
3 | 3 | const { test } = require('node:test')
|
4 |
| -const sget = require('simple-get').concat |
5 | 4 | const fastify = require('../../fastify')()
|
6 | 5 | fastify.addHttpMethod('LOCK', { hasBody: true })
|
7 | 6 |
|
@@ -57,52 +56,53 @@ test('can be created - lock', t => {
|
57 | 56 | })
|
58 | 57 |
|
59 | 58 | test('lock test', async t => {
|
60 |
| - await fastify.listen({ port: 0 }) |
| 59 | + const fastifyServer = await fastify.listen({ port: 0 }) |
61 | 60 | t.after(() => {
|
62 | 61 | fastify.close()
|
63 | 62 | })
|
64 | 63 | // the body test uses a text/plain content type instead of application/xml because it requires
|
65 | 64 | // a specific content type parser
|
66 |
| - await t.test('request with body - lock', (t, done) => { |
| 65 | + await t.test('request with body - lock', async (t) => { |
67 | 66 | t.plan(3)
|
68 |
| - sget({ |
69 |
| - url: `http://localhost:${fastify.server.address().port}/test/a.txt`, |
| 67 | + |
| 68 | + const result = await fetch(`${fastifyServer}/test/a.txt`, { |
| 69 | + method: 'LOCK', |
70 | 70 | headers: { 'content-type': 'text/plain' },
|
71 |
| - body: bodySample, |
72 |
| - method: 'LOCK' |
73 |
| - }, (err, response, body) => { |
74 |
| - t.assert.ifError(err) |
75 |
| - t.assert.strictEqual(response.statusCode, 200) |
76 |
| - t.assert.strictEqual(response.headers['content-length'], '' + body.length) |
77 |
| - done() |
| 71 | + body: bodySample |
78 | 72 | })
|
| 73 | + |
| 74 | + t.assert.ok(result.ok) |
| 75 | + t.assert.strictEqual(result.status, 200) |
| 76 | + const body = await result.text() |
| 77 | + t.assert.strictEqual(result.headers.get('content-length'), '' + body.length) |
79 | 78 | })
|
80 | 79 |
|
81 |
| - await t.test('request with body and no content type (415 error) - lock', (t, done) => { |
| 80 | + await t.test('request with body and no content type (415 error) - lock', async (t) => { |
82 | 81 | t.plan(3)
|
83 |
| - sget({ |
84 |
| - url: `http://localhost:${fastify.server.address().port}/test/a.txt`, |
| 82 | + |
| 83 | + const result = await fetch(`${fastifyServer}/test/a.txt`, { |
| 84 | + method: 'LOCK', |
85 | 85 | body: bodySample,
|
86 |
| - method: 'LOCK' |
87 |
| - }, (err, response, body) => { |
88 |
| - t.assert.ifError(err) |
89 |
| - t.assert.strictEqual(response.statusCode, 415) |
90 |
| - t.assert.strictEqual(response.headers['content-length'], '' + body.length) |
91 |
| - done() |
| 86 | + headers: { 'content-type': undefined } |
92 | 87 | })
|
| 88 | + |
| 89 | + t.assert.ok(!result.ok) |
| 90 | + t.assert.strictEqual(result.status, 415) |
| 91 | + const body = await result.text() |
| 92 | + t.assert.strictEqual(result.headers.get('content-length'), '' + body.length) |
93 | 93 | })
|
94 | 94 |
|
95 |
| - await t.test('request without body - lock', (t, done) => { |
| 95 | + await t.test('request without body - lock', async (t) => { |
96 | 96 | t.plan(3)
|
97 |
| - sget({ |
98 |
| - url: `http://localhost:${fastify.server.address().port}/test/a.txt`, |
99 |
| - headers: { 'content-type': 'text/plain' }, |
100 |
| - method: 'LOCK' |
101 |
| - }, (err, response, body) => { |
102 |
| - t.assert.ifError(err) |
103 |
| - t.assert.strictEqual(response.statusCode, 200) |
104 |
| - t.assert.strictEqual(response.headers['content-length'], '' + body.length) |
105 |
| - done() |
| 97 | + |
| 98 | + const result = await fetch(`${fastifyServer}/test/a.txt`, { |
| 99 | + method: 'LOCK', |
| 100 | + headers: { 'content-type': 'text/plain' } |
106 | 101 | })
|
| 102 | + |
| 103 | + t.assert.ok(result.ok) |
| 104 | + t.assert.strictEqual(result.status, 200) |
| 105 | + const body = await result.text() |
| 106 | + t.assert.strictEqual(result.headers.get('content-length'), '' + body.length) |
107 | 107 | })
|
108 | 108 | })
|
0 commit comments