|
| 1 | +'use strict'; |
| 2 | +constcommon=require('../common'); |
| 3 | +constassert=require('node:assert'); |
| 4 | +consthttp=require('node:http'); |
| 5 | + |
| 6 | +// Happy flow: arbitrary 1xx status with custom headers, observed by the |
| 7 | +// client's 'information' event. |
| 8 | +{ |
| 9 | +constserver=http.createServer(common.mustCall((req,res)=>{ |
| 10 | +res.writeInformation(110,{'X-Progress': '50%','X-Stage': 'reading'}); |
| 11 | +res.writeInformation(199,[['X-Custom','one'],['X-Custom-2','two']]); |
| 12 | +res.end('done'); |
| 13 | +})); |
| 14 | + |
| 15 | +server.listen(0,common.mustCall(()=>{ |
| 16 | +constreq=http.request({port: server.address().port}); |
| 17 | + |
| 18 | +constseen=[]; |
| 19 | +req.on('information',(res)=>{ |
| 20 | +seen.push({ |
| 21 | +statusCode: res.statusCode, |
| 22 | +headers: res.headers, |
| 23 | +}); |
| 24 | +}); |
| 25 | + |
| 26 | +req.on('response',common.mustCall((res)=>{ |
| 27 | +assert.strictEqual(res.statusCode,200); |
| 28 | + |
| 29 | +assert.strictEqual(seen.length,2); |
| 30 | +assert.strictEqual(seen[0].statusCode,110); |
| 31 | +assert.strictEqual(seen[0].headers['x-progress'],'50%'); |
| 32 | +assert.strictEqual(seen[0].headers['x-stage'],'reading'); |
| 33 | +assert.strictEqual(seen[1].statusCode,199); |
| 34 | +assert.strictEqual(seen[1].headers['x-custom'],'one'); |
| 35 | +assert.strictEqual(seen[1].headers['x-custom-2'],'two'); |
| 36 | + |
| 37 | +res.resume(); |
| 38 | +res.on('end',common.mustCall(()=>server.close())); |
| 39 | +})); |
| 40 | + |
| 41 | +req.end(); |
| 42 | +})); |
| 43 | +} |
| 44 | + |
| 45 | +// Headers argument is optional / nullable. |
| 46 | +{ |
| 47 | +constserver=http.createServer(common.mustCall((req,res)=>{ |
| 48 | +res.writeInformation(150); |
| 49 | +res.writeInformation(151,null); |
| 50 | +res.end(); |
| 51 | +})); |
| 52 | + |
| 53 | +server.listen(0,common.mustCall(()=>{ |
| 54 | +constreq=http.request({port: server.address().port}); |
| 55 | +letcount=0; |
| 56 | +req.on('information',()=>count++); |
| 57 | +req.on('response',common.mustCall((res)=>{ |
| 58 | +assert.strictEqual(count,2); |
| 59 | +res.resume(); |
| 60 | +res.on('end',common.mustCall(()=>server.close())); |
| 61 | +})); |
| 62 | +req.end(); |
| 63 | +})); |
| 64 | +} |
| 65 | + |
| 66 | +// Error cases. |
| 67 | +{ |
| 68 | +constserver=http.createServer(common.mustCall((req,res)=>{ |
| 69 | +assert.throws(()=>res.writeInformation(101), |
| 70 | +{code: 'ERR_HTTP_INVALID_STATUS_CODE'}); |
| 71 | +assert.throws(()=>res.writeInformation(99), |
| 72 | +{code: 'ERR_OUT_OF_RANGE'}); |
| 73 | +assert.throws(()=>res.writeInformation(200), |
| 74 | +{code: 'ERR_OUT_OF_RANGE'}); |
| 75 | +assert.throws(()=>res.writeInformation('100'), |
| 76 | +{code: 'ERR_INVALID_ARG_TYPE'}); |
| 77 | +assert.throws(()=>res.writeInformation(150,{'X-Bad\n': 'v'}), |
| 78 | +{code: 'ERR_INVALID_HTTP_TOKEN'}); |
| 79 | + |
| 80 | +res.writeHead(200); |
| 81 | +assert.throws(()=>res.writeInformation(150), |
| 82 | +{code: 'ERR_HTTP_HEADERS_SENT'}); |
| 83 | +res.end(); |
| 84 | +})); |
| 85 | + |
| 86 | +server.listen(0,common.mustCall(()=>{ |
| 87 | +constreq=http.request({port: server.address().port}); |
| 88 | +req.on('response',common.mustCall((res)=>{ |
| 89 | +res.resume(); |
| 90 | +res.on('end',common.mustCall(()=>server.close())); |
| 91 | +})); |
| 92 | +req.end(); |
| 93 | +})); |
| 94 | +} |
0 commit comments