xref: /third_party/node/test/parallel/test-http.js (revision 1cb0ef41)
11cb0ef41Sopenharmony_ci// Copyright Joyent, Inc. and other Node contributors.
21cb0ef41Sopenharmony_ci//
31cb0ef41Sopenharmony_ci// Permission is hereby granted, free of charge, to any person obtaining a
41cb0ef41Sopenharmony_ci// copy of this software and associated documentation files (the
51cb0ef41Sopenharmony_ci// "Software"), to deal in the Software without restriction, including
61cb0ef41Sopenharmony_ci// without limitation the rights to use, copy, modify, merge, publish,
71cb0ef41Sopenharmony_ci// distribute, sublicense, and/or sell copies of the Software, and to permit
81cb0ef41Sopenharmony_ci// persons to whom the Software is furnished to do so, subject to the
91cb0ef41Sopenharmony_ci// following conditions:
101cb0ef41Sopenharmony_ci//
111cb0ef41Sopenharmony_ci// The above copyright notice and this permission notice shall be included
121cb0ef41Sopenharmony_ci// in all copies or substantial portions of the Software.
131cb0ef41Sopenharmony_ci//
141cb0ef41Sopenharmony_ci// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
151cb0ef41Sopenharmony_ci// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
161cb0ef41Sopenharmony_ci// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
171cb0ef41Sopenharmony_ci// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
181cb0ef41Sopenharmony_ci// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
191cb0ef41Sopenharmony_ci// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
201cb0ef41Sopenharmony_ci// USE OR OTHER DEALINGS IN THE SOFTWARE.
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci'use strict';
231cb0ef41Sopenharmony_ciconst common = require('../common');
241cb0ef41Sopenharmony_ciconst assert = require('assert');
251cb0ef41Sopenharmony_ciconst http = require('http');
261cb0ef41Sopenharmony_ciconst url = require('url');
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ciconst expectedRequests = ['/hello', '/there', '/world'];
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ciconst server = http.Server(common.mustCall((req, res) => {
311cb0ef41Sopenharmony_ci  assert.strictEqual(expectedRequests.shift(), req.url);
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci  switch (req.url) {
341cb0ef41Sopenharmony_ci    case '/hello':
351cb0ef41Sopenharmony_ci      assert.strictEqual(req.method, 'GET');
361cb0ef41Sopenharmony_ci      assert.strictEqual(req.headers.accept, '*/*');
371cb0ef41Sopenharmony_ci      assert.strictEqual(req.headers.foo, 'bar');
381cb0ef41Sopenharmony_ci      assert.strictEqual(req.headers.cookie, 'foo=bar; bar=baz; baz=quux');
391cb0ef41Sopenharmony_ci      break;
401cb0ef41Sopenharmony_ci    case '/there':
411cb0ef41Sopenharmony_ci      assert.strictEqual(req.method, 'PUT');
421cb0ef41Sopenharmony_ci      assert.strictEqual(req.headers.cookie, 'node=awesome; ta=da');
431cb0ef41Sopenharmony_ci      break;
441cb0ef41Sopenharmony_ci    case '/world':
451cb0ef41Sopenharmony_ci      assert.strictEqual(req.method, 'POST');
461cb0ef41Sopenharmony_ci      assert.strictEqual(req.headers.cookie, 'abc=123; def=456; ghi=789');
471cb0ef41Sopenharmony_ci      break;
481cb0ef41Sopenharmony_ci    default:
491cb0ef41Sopenharmony_ci      assert(false, `Unexpected request for ${req.url}`);
501cb0ef41Sopenharmony_ci  }
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ci  if (expectedRequests.length === 0)
531cb0ef41Sopenharmony_ci    server.close();
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ci  req.on('end', () => {
561cb0ef41Sopenharmony_ci    res.writeHead(200, { 'Content-Type': 'text/plain' });
571cb0ef41Sopenharmony_ci    res.write(`The path was ${url.parse(req.url).pathname}`);
581cb0ef41Sopenharmony_ci    res.end();
591cb0ef41Sopenharmony_ci  });
601cb0ef41Sopenharmony_ci  req.resume();
611cb0ef41Sopenharmony_ci}, 3));
621cb0ef41Sopenharmony_ciserver.listen(0);
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_ciserver.on('listening', () => {
651cb0ef41Sopenharmony_ci  const agent = new http.Agent({ port: server.address().port, maxSockets: 1 });
661cb0ef41Sopenharmony_ci  const req = http.get({
671cb0ef41Sopenharmony_ci    port: server.address().port,
681cb0ef41Sopenharmony_ci    path: '/hello',
691cb0ef41Sopenharmony_ci    headers: {
701cb0ef41Sopenharmony_ci      Accept: '*/*',
711cb0ef41Sopenharmony_ci      Foo: 'bar',
721cb0ef41Sopenharmony_ci      Cookie: [ 'foo=bar', 'bar=baz', 'baz=quux' ]
731cb0ef41Sopenharmony_ci    },
741cb0ef41Sopenharmony_ci    agent: agent
751cb0ef41Sopenharmony_ci  }, common.mustCall((res) => {
761cb0ef41Sopenharmony_ci    const cookieHeaders = req._header.match(/^Cookie: .+$/img);
771cb0ef41Sopenharmony_ci    assert.deepStrictEqual(cookieHeaders,
781cb0ef41Sopenharmony_ci                           ['Cookie: foo=bar; bar=baz; baz=quux']);
791cb0ef41Sopenharmony_ci    assert.strictEqual(res.statusCode, 200);
801cb0ef41Sopenharmony_ci    let body = '';
811cb0ef41Sopenharmony_ci    res.setEncoding('utf8');
821cb0ef41Sopenharmony_ci    res.on('data', (chunk) => { body += chunk; });
831cb0ef41Sopenharmony_ci    res.on('end', common.mustCall(() => {
841cb0ef41Sopenharmony_ci      assert.strictEqual(body, 'The path was /hello');
851cb0ef41Sopenharmony_ci    }));
861cb0ef41Sopenharmony_ci  }));
871cb0ef41Sopenharmony_ci
881cb0ef41Sopenharmony_ci  setTimeout(common.mustCall(() => {
891cb0ef41Sopenharmony_ci    const req = http.request({
901cb0ef41Sopenharmony_ci      port: server.address().port,
911cb0ef41Sopenharmony_ci      method: 'PUT',
921cb0ef41Sopenharmony_ci      path: '/there',
931cb0ef41Sopenharmony_ci      agent: agent
941cb0ef41Sopenharmony_ci    }, common.mustCall((res) => {
951cb0ef41Sopenharmony_ci      const cookieHeaders = req._header.match(/^Cookie: .+$/img);
961cb0ef41Sopenharmony_ci      assert.deepStrictEqual(cookieHeaders, ['Cookie: node=awesome; ta=da']);
971cb0ef41Sopenharmony_ci      assert.strictEqual(res.statusCode, 200);
981cb0ef41Sopenharmony_ci      let body = '';
991cb0ef41Sopenharmony_ci      res.setEncoding('utf8');
1001cb0ef41Sopenharmony_ci      res.on('data', (chunk) => { body += chunk; });
1011cb0ef41Sopenharmony_ci      res.on('end', common.mustCall(() => {
1021cb0ef41Sopenharmony_ci        assert.strictEqual(body, 'The path was /there');
1031cb0ef41Sopenharmony_ci      }));
1041cb0ef41Sopenharmony_ci    }));
1051cb0ef41Sopenharmony_ci    req.setHeader('Cookie', ['node=awesome', 'ta=da']);
1061cb0ef41Sopenharmony_ci    req.end();
1071cb0ef41Sopenharmony_ci  }), 1);
1081cb0ef41Sopenharmony_ci
1091cb0ef41Sopenharmony_ci  setTimeout(common.mustCall(() => {
1101cb0ef41Sopenharmony_ci    const req = http.request({
1111cb0ef41Sopenharmony_ci      port: server.address().port,
1121cb0ef41Sopenharmony_ci      method: 'POST',
1131cb0ef41Sopenharmony_ci      path: '/world',
1141cb0ef41Sopenharmony_ci      headers: [ ['Cookie', 'abc=123'],
1151cb0ef41Sopenharmony_ci                 ['Cookie', 'def=456'],
1161cb0ef41Sopenharmony_ci                 ['Cookie', 'ghi=789'] ],
1171cb0ef41Sopenharmony_ci      agent: agent
1181cb0ef41Sopenharmony_ci    }, common.mustCall((res) => {
1191cb0ef41Sopenharmony_ci      const cookieHeaders = req._header.match(/^Cookie: .+$/img);
1201cb0ef41Sopenharmony_ci      assert.deepStrictEqual(cookieHeaders,
1211cb0ef41Sopenharmony_ci                             ['Cookie: abc=123',
1221cb0ef41Sopenharmony_ci                              'Cookie: def=456',
1231cb0ef41Sopenharmony_ci                              'Cookie: ghi=789']);
1241cb0ef41Sopenharmony_ci      assert.strictEqual(res.statusCode, 200);
1251cb0ef41Sopenharmony_ci      let body = '';
1261cb0ef41Sopenharmony_ci      res.setEncoding('utf8');
1271cb0ef41Sopenharmony_ci      res.on('data', (chunk) => { body += chunk; });
1281cb0ef41Sopenharmony_ci      res.on('end', common.mustCall(() => {
1291cb0ef41Sopenharmony_ci        assert.strictEqual(body, 'The path was /world');
1301cb0ef41Sopenharmony_ci      }));
1311cb0ef41Sopenharmony_ci    }));
1321cb0ef41Sopenharmony_ci    req.end();
1331cb0ef41Sopenharmony_ci  }), 2);
1341cb0ef41Sopenharmony_ci});
135