summaryrefslogtreecommitdiff
path: root/test/parallel/test-http-expect-handling.js
blob: 0a39d49d923474444dd60367f8e35d91e2653f37 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Spec documentation http://httpwg.github.io/specs/rfc7231.html#header.expect
'use strict';
const common = require('../common');
const assert = require('assert');
const http = require('http');

const tests = [417, 417];

let testsComplete = 0;
let testIdx = 0;

const s = http.createServer((req, res) => {
  throw new Error('this should never be executed');
});

s.listen(0, nextTest);

function nextTest() {
  const options = {
    port: s.address().port,
    headers: { 'Expect': 'meoww' }
  };

  if (testIdx === tests.length) {
    return s.close();
  }

  const test = tests[testIdx];

  if (testIdx > 0) {
    s.on('checkExpectation', common.mustCall((req, res) => {
      res.statusCode = 417;
      res.end();
    }));
  }

  http.get(options, (response) => {
    console.log(`client: expected status: ${test}`);
    console.log(`client: statusCode: ${response.statusCode}`);
    assert.strictEqual(response.statusCode, test);
    assert.strictEqual(response.statusMessage, 'Expectation Failed');

    response.on('end', () => {
      testsComplete++;
      testIdx++;
      nextTest();
    });
    response.resume();
  });
}


process.on('exit', () => {
  assert.strictEqual(testsComplete, 2);
});