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';
23const common = require('../common');
24const assert = require('assert');
25const http = require('http');
26
27const test_req_body = 'some stuff...\n';
28const test_res_body = 'other stuff!\n';
29let sent_continue = false;
30let got_continue = false;
31
32const handler = common.mustCall((req, res) => {
33  assert.ok(sent_continue, 'Full response sent before 100 Continue');
34  console.error('Server sending full response...');
35  res.writeHead(200, {
36    'Content-Type': 'text/plain',
37    'ABCD': '1'
38  });
39  res.end(test_res_body);
40});
41
42const server = http.createServer(common.mustNotCall());
43server.on('checkContinue', common.mustCall((req, res) => {
44  console.error('Server got Expect: 100-continue...');
45  res.writeContinue();
46  sent_continue = true;
47  setTimeout(function() {
48    handler(req, res);
49  }, 100);
50}));
51server.listen(0);
52
53
54server.on('listening', common.mustCall(() => {
55  const req = http.request({
56    port: server.address().port,
57    method: 'POST',
58    path: '/world',
59    headers: { 'Expect': '100-continue' }
60  });
61  console.error('Client sending request...');
62  let body = '';
63  req.on('continue', common.mustCall(() => {
64    console.error('Client got 100 Continue...');
65    got_continue = true;
66    req.end(test_req_body);
67  }));
68  req.on('response', common.mustCall((res) => {
69    assert.ok(got_continue, 'Full response received before 100 Continue');
70    assert.strictEqual(res.statusCode, 200,
71                       `Final status code was ${res.statusCode}, not 200.`);
72    res.setEncoding('utf8');
73    res.on('data', function(chunk) { body += chunk; });
74    res.on('end', common.mustCall(() => {
75      console.error('Got full response.');
76      assert.strictEqual(body, test_res_body);
77      assert.ok('abcd' in res.headers, 'Response headers missing.');
78      server.close();
79    }));
80  }));
81}));
82