|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch B.V. under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch B.V. licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +import Http from 'http'; |
| 20 | +import Url from 'url'; |
| 21 | +import { FtrProviderContext } from '../../services/types'; |
| 22 | + |
| 23 | +// @ts-ignore |
| 24 | +import getUrl from '../../../../src/test_utils/get_url'; |
| 25 | + |
| 26 | +const delay = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)); |
| 27 | +const oneSec = 1_000; |
| 28 | + |
| 29 | +// eslint-disable-next-line import/no-default-export |
| 30 | +export default function ({ getService }: FtrProviderContext) { |
| 31 | + const config = getService('config'); |
| 32 | + |
| 33 | + describe('headers timeout ', () => { |
| 34 | + it('issue-73849', async () => { |
| 35 | + const agent = new Http.Agent({ |
| 36 | + keepAlive: true, |
| 37 | + }); |
| 38 | + const { protocol, hostname, port } = Url.parse(getUrl.baseUrl(config.get('servers.kibana'))); |
| 39 | + |
| 40 | + function performRequest() { |
| 41 | + return new Promise((resolve, reject) => { |
| 42 | + const req = Http.request( |
| 43 | + { |
| 44 | + protocol, |
| 45 | + hostname, |
| 46 | + port, |
| 47 | + path: '/', |
| 48 | + method: 'GET', |
| 49 | + agent, |
| 50 | + }, |
| 51 | + function (res) { |
| 52 | + let data = ''; |
| 53 | + res.on('data', (chunk) => { |
| 54 | + data += chunk; |
| 55 | + }); |
| 56 | + res.on('end', () => resolve(data)); |
| 57 | + } |
| 58 | + ); |
| 59 | + |
| 60 | + req.on('socket', (socket) => { |
| 61 | + socket.write('GET / HTTP/1.1\r\n'); |
| 62 | + setTimeout(() => { |
| 63 | + socket.write('Host: localhost\r\n'); |
| 64 | + }, oneSec); |
| 65 | + setTimeout(() => { |
| 66 | + // headersTimeout doesn't seem to fire if request headers are sent in one packet. |
| 67 | + socket.write('\r\n'); |
| 68 | + req.end(); |
| 69 | + }, 2 * oneSec); |
| 70 | + }); |
| 71 | + |
| 72 | + req.on('error', reject); |
| 73 | + }); |
| 74 | + } |
| 75 | + |
| 76 | + await performRequest(); |
| 77 | + const defaultHeadersTimeout = 40 * oneSec; |
| 78 | + await delay(defaultHeadersTimeout + oneSec); |
| 79 | + await performRequest(); |
| 80 | + }); |
| 81 | + }); |
| 82 | +} |
0 commit comments