|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const { |
| 4 | + mustCall, |
| 5 | + mustNotCall, |
| 6 | + expectsError, |
| 7 | + hasCrypto, |
| 8 | + skip |
| 9 | +} = require('../common'); |
| 10 | +if (!hasCrypto) |
| 11 | + skip('missing crypto'); |
| 12 | +const { createServer, connect } = require('http2'); |
| 13 | +const assert = require('assert'); |
| 14 | + |
| 15 | +const server = createServer(); |
| 16 | +server.listen(0, mustCall(() => { |
| 17 | + const port = server.address().port; |
| 18 | + const url = `http://localhost:${port}`; |
| 19 | + const client = connect(url, mustCall(() => { |
| 20 | + const request = client.request(); |
| 21 | + request.resume(); |
| 22 | + request.on('end', mustCall()); |
| 23 | + request.on('close', mustCall(() => { |
| 24 | + client.close(); |
| 25 | + })); |
| 26 | + })); |
| 27 | + |
| 28 | + server.once('request', mustCall((request, response) => { |
| 29 | + // response.write() returns true |
| 30 | + assert(response.write('muahaha', 'utf8', mustCall())); |
| 31 | + |
| 32 | + response.stream.close(0, mustCall(() => { |
| 33 | + response.on('error', mustNotCall()); |
| 34 | + |
| 35 | + // response.write() without cb returns error |
| 36 | + expectsError( |
| 37 | + () => { response.write('muahaha'); }, |
| 38 | + { |
| 39 | + type: Error, |
| 40 | + code: 'ERR_HTTP2_INVALID_STREAM', |
| 41 | + message: 'The stream has been destroyed' |
| 42 | + } |
| 43 | + ); |
| 44 | + |
| 45 | + // response.write() with cb returns falsy value |
| 46 | + assert(!response.write('muahaha', mustCall())); |
| 47 | + |
| 48 | + client.destroy(); |
| 49 | + server.close(); |
| 50 | + })); |
| 51 | + })); |
| 52 | +})); |
0 commit comments