summaryrefslogtreecommitdiff
path: root/test/parallel/test-http2-client-upload-reject.js
blob: 678114130e3dba8980a0c92dab65e655e5793525 (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
'use strict';

// Verifies that uploading data from a client works

const common = require('../common');
if (!common.hasCrypto)
  common.skip('missing crypto');
const assert = require('assert');
const http2 = require('http2');
const fs = require('fs');
const fixtures = require('../common/fixtures');

const loc = fixtures.path('person-large.jpg');

assert(fs.existsSync(loc));

fs.readFile(loc, common.mustCall((err, data) => {
  assert.ifError(err);

  const server = http2.createServer();

  server.on('stream', common.mustCall((stream) => {
    // Wait for some data to come through.
    setImmediate(() => {
      stream.on('close', common.mustCall(() => {
        assert.strictEqual(stream.rstCode, 0);
      }));

      stream.respond({ ':status': 400 });
      stream.end();
    });
  }));

  server.listen(0, common.mustCall(() => {
    const client = http2.connect(`http://localhost:${server.address().port}`);

    const req = client.request({ ':method': 'POST' });
    req.on('response', common.mustCall((headers) => {
      assert.strictEqual(headers[':status'], 400);
    }));

    req.resume();
    req.on('end', common.mustCall(() => {
      server.close();
      client.close();
    }));

    const str = fs.createReadStream(loc);
    str.pipe(req);
  }));
}));