|  | 
| 9 | 9 | import type { Request } from '../../test' | 
| 10 | 10 | import type { EndpointBuilder } from '../domain/configuration' | 
| 11 | 11 | import { createEndpointBuilder } from '../domain/configuration' | 
|  | 12 | +import { addExperimentalFeatures, resetExperimentalFeatures, ExperimentalFeature } from '../tools/experimentalFeatures' | 
| 12 | 13 | import { noop } from '../tools/utils/functionUtils' | 
| 13 | 14 | import { createHttpRequest, fetchKeepAliveStrategy, fetchStrategy } from './httpRequest' | 
| 14 | 15 | import type { HttpRequest } from './httpRequest' | 
| @@ -263,3 +264,48 @@ describe('httpRequest intake parameters', () => { | 
| 263 | 264 |     expect(requests.length).toEqual(2) | 
| 264 | 265 |   }) | 
| 265 | 266 | }) | 
|  | 267 | + | 
|  | 268 | +describe('httpRequest with AVOID_FETCH_KEEPALIVE feature flag', () => { | 
|  | 269 | +  const BATCH_BYTES_LIMIT = 100 | 
|  | 270 | +  const ENDPOINT_URL = 'http://my.website' | 
|  | 271 | +  let interceptor: ReturnType<typeof interceptRequests> | 
|  | 272 | +  let requests: Request[] | 
|  | 273 | +  let endpointBuilder: EndpointBuilder | 
|  | 274 | +  let request: HttpRequest | 
|  | 275 | + | 
|  | 276 | +  beforeEach(() => { | 
|  | 277 | +    interceptor = interceptRequests() | 
|  | 278 | +    requests = interceptor.requests | 
|  | 279 | +    endpointBuilder = mockEndpointBuilder(ENDPOINT_URL) | 
|  | 280 | +  }) | 
|  | 281 | + | 
|  | 282 | +  afterEach(() => { | 
|  | 283 | +    resetExperimentalFeatures() | 
|  | 284 | +  }) | 
|  | 285 | + | 
|  | 286 | +  it('should use regular fetch (without keepalive) when feature flag is enabled', async () => { | 
|  | 287 | +    addExperimentalFeatures([ExperimentalFeature.AVOID_FETCH_KEEPALIVE]) | 
|  | 288 | +    request = createHttpRequest(endpointBuilder, BATCH_BYTES_LIMIT, noop) | 
|  | 289 | + | 
|  | 290 | +    request.send({ data: '{"foo":"bar"}', bytesCount: 10 }) | 
|  | 291 | +    await interceptor.waitForAllFetchCalls() | 
|  | 292 | + | 
|  | 293 | +    expect(requests.length).toEqual(1) | 
|  | 294 | +    expect(requests[0].type).toBe('fetch') | 
|  | 295 | +    expect(requests[0].url).toContain(ENDPOINT_URL) | 
|  | 296 | +  }) | 
|  | 297 | + | 
|  | 298 | +  it('should use fetch keepalive when feature flag is not enabled', async () => { | 
|  | 299 | +    if (!interceptor.isFetchKeepAliveSupported()) { | 
|  | 300 | +      pending('no fetch keepalive support') | 
|  | 301 | +    } | 
|  | 302 | + | 
|  | 303 | +    request = createHttpRequest(endpointBuilder, BATCH_BYTES_LIMIT, noop) | 
|  | 304 | + | 
|  | 305 | +    request.send({ data: '{"foo":"bar"}', bytesCount: 10 }) | 
|  | 306 | +    await interceptor.waitForAllFetchCalls() | 
|  | 307 | + | 
|  | 308 | +    expect(requests.length).toEqual(1) | 
|  | 309 | +    expect(requests[0].type).toBe('fetch-keepalive') | 
|  | 310 | +  }) | 
|  | 311 | +}) | 
0 commit comments