|
20 | 20 | // USE OR OTHER DEALINGS IN THE SOFTWARE. |
21 | 21 |
|
22 | 22 | 'use strict'; |
23 | | -require('../common'); |
| 23 | +const common = require('../common'); |
24 | 24 | const assert = require('assert'); |
25 | 25 | const http = require('http'); |
26 | 26 |
|
27 | | -let test = 1; |
| 27 | +const server = http.createServer(); |
28 | 28 |
|
29 | | -const server = http.createServer(function(req, res) { |
30 | | - res.writeHead(200, { 'Content-Type': 'text/plain' }); |
31 | | - if (test === 1) { |
32 | | - // write should accept string |
33 | | - res.write('string'); |
34 | | - // write should accept buffer |
35 | | - res.write(Buffer.from('asdf')); |
| 29 | +server.once('request', common.mustCall((req, res) => { |
| 30 | + server.on('request', common.mustCall((req, res) => { |
| 31 | + res.end(Buffer.from('asdf')); |
| 32 | + })); |
| 33 | + // write should accept string |
| 34 | + res.write('string'); |
| 35 | + // write should accept buffer |
| 36 | + res.write(Buffer.from('asdf')); |
36 | 37 |
|
37 | | - // write should not accept an Array |
38 | | - assert.throws(function() { |
39 | | - res.write(['array']); |
40 | | - }, TypeError, 'first argument must be a string or Buffer'); |
| 38 | + // write should not accept an Array |
| 39 | + assert.throws(function() { |
| 40 | + res.write(['array']); |
| 41 | + }, TypeError, 'first argument must be a string or Buffer'); |
41 | 42 |
|
42 | | - // end should not accept an Array |
43 | | - assert.throws(function() { |
44 | | - res.end(['moo']); |
45 | | - }, TypeError, 'first argument must be a string or Buffer'); |
| 43 | + // end should not accept an Array |
| 44 | + assert.throws(function() { |
| 45 | + res.end(['moo']); |
| 46 | + }, TypeError, 'first argument must be a string or Buffer'); |
46 | 47 |
|
47 | | - // end should accept string |
48 | | - res.end('string'); |
49 | | - } else if (test === 2) { |
50 | | - // end should accept Buffer |
51 | | - res.end(Buffer.from('asdf')); |
52 | | - } |
53 | | -}); |
| 48 | + // end should accept string |
| 49 | + res.end('string'); |
| 50 | +})); |
54 | 51 |
|
55 | 52 | server.listen(0, function() { |
56 | 53 | // just make a request, other tests handle responses |
57 | 54 | http.get({ port: this.address().port }, function(res) { |
58 | 55 | res.resume(); |
59 | | - // lazy serial test, because we can only call end once per request |
60 | | - test += 1; |
61 | 56 | // do it again to test .end(Buffer); |
62 | 57 | http.get({ port: server.address().port }, function(res) { |
63 | 58 | res.resume(); |
|
0 commit comments