summaryrefslogtreecommitdiff
path: root/test/parallel/test-http2-origin.js
blob: 6f90a043d8a7cccb404da00d041f6ab42d2e056f (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
'use strict';

const {
  hasCrypto,
  mustCall,
  mustNotCall,
  skip
} = require('../common');
if (!hasCrypto)
  skip('missing crypto');

const {
  deepStrictEqual,
  strictEqual,
  throws
} = require('assert');
const {
  createSecureServer,
  createServer,
  connect
} = require('http2');
const Countdown = require('../common/countdown');

const { readKey } = require('../common/fixtures');

const key = readKey('agent8-key.pem', 'binary');
const cert = readKey('agent8-cert.pem', 'binary');
const ca = readKey('fake-startcom-root-cert.pem', 'binary');

{
  const server = createSecureServer({ key, cert });
  server.on('stream', mustCall((stream) => {
    stream.session.origin('https://example.org/a/b/c',
                          new URL('https://example.com'));
    stream.respond();
    stream.end('ok');
  }));
  server.on('session', mustCall((session) => {
    session.origin('https://foo.org/a/b/c', new URL('https://bar.org'));

    // Won't error, but won't send anything
    session.origin();

    [0, true, {}, []].forEach((input) => {
      throws(
        () => session.origin(input),
        {
          code: 'ERR_INVALID_ARG_TYPE',
          name: 'TypeError'
        }
      );
    });

    [new URL('foo://bar'), 'foo://bar'].forEach((input) => {
      throws(
        () => session.origin(input),
        {
          code: 'ERR_HTTP2_INVALID_ORIGIN',
          name: 'TypeError'
        }
      );
    });

    ['not a valid url'].forEach((input) => {
      throws(
        () => session.origin(input),
        {
          code: 'ERR_INVALID_URL',
          name: 'TypeError'
        }
      );
    });
    const longInput = 'http://foo.bar' + 'a'.repeat(16383);
    throws(
      () => session.origin(longInput),
      {
        code: 'ERR_HTTP2_ORIGIN_LENGTH',
        name: 'TypeError'
      }
    );
  }));

  server.listen(0, mustCall(() => {
    const originSet = [`https://localhost:${server.address().port}`];
    const client = connect(originSet[0], { ca });
    const checks = [
      ['https://foo.org', 'https://bar.org'],
      ['https://example.org', 'https://example.com']
    ];

    const countdown = new Countdown(3, () => {
      client.close();
      server.close();
    });

    client.on('origin', mustCall((origins) => {
      const check = checks.shift();
      originSet.push(...check);
      deepStrictEqual(client.originSet, originSet);
      deepStrictEqual(origins, check);
      countdown.dec();
    }, 2));

    client.request().on('close', mustCall(() => countdown.dec())).resume();
  }));
}

// Test automatically sending origin on connection start
{
  const origins = [ 'https://foo.org/a/b/c', 'https://bar.org' ];
  const server = createSecureServer({ key, cert, origins });
  server.on('stream', mustCall((stream) => {
    stream.respond();
    stream.end('ok');
  }));

  server.listen(0, mustCall(() => {
    const check = ['https://foo.org', 'https://bar.org'];
    const originSet = [`https://localhost:${server.address().port}`];
    const client = connect(originSet[0], { ca });

    const countdown = new Countdown(2, () => {
      client.close();
      server.close();
    });

    client.on('origin', mustCall((origins) => {
      originSet.push(...check);
      deepStrictEqual(client.originSet, originSet);
      deepStrictEqual(origins, check);
      countdown.dec();
    }));

    client.request().on('close', mustCall(() => countdown.dec())).resume();
  }));
}

// If return status is 421, the request origin must be removed from the
// originSet
{
  const server = createSecureServer({ key, cert });
  server.on('stream', mustCall((stream) => {
    stream.respond({ ':status': 421 });
    stream.end();
  }));
  server.on('session', mustCall((session) => {
    session.origin('https://foo.org');
  }));

  server.listen(0, mustCall(() => {
    const origin = `https://localhost:${server.address().port}`;
    const client = connect(origin, { ca });

    client.on('origin', mustCall((origins) => {
      deepStrictEqual(client.originSet, [origin, 'https://foo.org']);
      const req = client.request({ ':authority': 'foo.org' });
      req.on('response', mustCall((headers) => {
        strictEqual(headers[':status'], 421);
        deepStrictEqual(client.originSet, [origin]);
      }));
      req.resume();
      req.on('close', mustCall(() => {
        client.close();
        server.close();
      }));
    }, 1));
  }));
}

// Origin is ignored on plain text HTTP/2 connections... server will still
// send them, but client will ignore them.
{
  const server = createServer();
  server.on('stream', mustCall((stream) => {
    stream.session.origin('https://example.org',
                          new URL('https://example.com'));
    stream.respond();
    stream.end('ok');
  }));
  server.listen(0, mustCall(() => {
    const client = connect(`http://localhost:${server.address().port}`);
    client.on('origin', mustNotCall());
    strictEqual(client.originSet, undefined);
    const req = client.request();
    req.resume();
    req.on('close', mustCall(() => {
      client.close();
      server.close();
    }));
  }));
}