|
| 1 | +// Copyright Joyent, Inc. and other Node contributors. |
| 2 | +// |
| 3 | +// Permission is hereby granted, free of charge, to any person obtaining a |
| 4 | +// copy of this software and associated documentation files (the |
| 5 | +// "Software"), to deal in the Software without restriction, including |
| 6 | +// without limitation the rights to use, copy, modify, merge, publish, |
| 7 | +// distribute, sublicense, and/or sell copies of the Software, and to permit |
| 8 | +// persons to whom the Software is furnished to do so, subject to the |
| 9 | +// following conditions: |
| 10 | +// |
| 11 | +// The above copyright notice and this permission notice shall be included |
| 12 | +// in all copies or substantial portions of the Software. |
| 13 | +// |
| 14 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 15 | +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 16 | +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN |
| 17 | +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| 18 | +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 19 | +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE |
| 20 | +// USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 21 | + |
| 22 | +'use strict'; |
| 23 | +const common = require('../common'); |
| 24 | +const assert = require('assert'); |
| 25 | +const http = require('http'); |
| 26 | + |
| 27 | +{ |
| 28 | + const server = http.createServer((req, res) => { |
| 29 | + res.writeHead(200); |
| 30 | + res.end('this is content'); |
| 31 | + }); |
| 32 | + server.listen(0); |
| 33 | + |
| 34 | + server.on('listening', common.mustCall(function() { |
| 35 | + const req = http.request({ |
| 36 | + port: this.address().port, |
| 37 | + method: 'HEAD', |
| 38 | + path: '/' |
| 39 | + }, common.mustCall((res) => { |
| 40 | + res.resume(); |
| 41 | + res.on('end', common.mustCall(function() { |
| 42 | + server.close(); |
| 43 | + })); |
| 44 | + })); |
| 45 | + req.end(); |
| 46 | + })); |
| 47 | +} |
| 48 | + |
| 49 | +{ |
| 50 | + const server = http.createServer({ |
| 51 | + rejectNonStandardBodyWrites: true, |
| 52 | + }, (req, res) => { |
| 53 | + res.writeHead(204); |
| 54 | + assert.throws(() => { |
| 55 | + res.write('this is content'); |
| 56 | + }, { |
| 57 | + code: 'ERR_HTTP_BODY_NOT_ALLOWED', |
| 58 | + name: 'Error', |
| 59 | + message: 'Adding content for this request method or response status is not allowed.' |
| 60 | + }); |
| 61 | + res.end(); |
| 62 | + }); |
| 63 | + server.listen(0); |
| 64 | + |
| 65 | + server.on('listening', common.mustCall(function() { |
| 66 | + const req = http.request({ |
| 67 | + port: this.address().port, |
| 68 | + method: 'GET', |
| 69 | + path: '/' |
| 70 | + }, common.mustCall((res) => { |
| 71 | + res.resume(); |
| 72 | + res.on('end', common.mustCall(function() { |
| 73 | + server.close(); |
| 74 | + })); |
| 75 | + })); |
| 76 | + req.end(); |
| 77 | + })); |
| 78 | +} |
| 79 | + |
| 80 | +{ |
| 81 | + const server = http.createServer({ |
| 82 | + rejectNonStandardBodyWrites: false, |
| 83 | + }, (req, res) => { |
| 84 | + res.writeHead(200); |
| 85 | + res.end('this is content'); |
| 86 | + }); |
| 87 | + server.listen(0); |
| 88 | + |
| 89 | + server.on('listening', common.mustCall(function() { |
| 90 | + const req = http.request({ |
| 91 | + port: this.address().port, |
| 92 | + method: 'HEAD', |
| 93 | + path: '/' |
| 94 | + }, common.mustCall((res) => { |
| 95 | + res.resume(); |
| 96 | + res.on('end', common.mustCall(function() { |
| 97 | + server.close(); |
| 98 | + })); |
| 99 | + })); |
| 100 | + req.end(); |
| 101 | + })); |
| 102 | +} |
0 commit comments