#501·pollyjs

NodeHttpAdapter's use of the non-standard options.href causes different behavior when Polly is enabled

Author: sflankerCreated Sep 11, 2024Updated Sep 11, 2024

Description

The use of the non-standard href property in getUrlFromOptions results in different behavior depending on whether or not Polly is enabled. There is no such property according to the Node.js documentation: https://nodejs.org/docs/latest-v18.x/api/http.html#httprequesturl-options-callback

Unfortunately there are libraries in the wild that use this non-standard property, and assign it an inconsistent/invalid value (I encounters this with the OpenSearch client library: https://github.com/opensearch-project/opensearch-js/blob/main/lib/Connection.js#L207)

Shareable Source

javascript
import https from 'https'
import { Polly } from '@pollyjs/core'
import NodeHttpAdapter from '@pollyjs/adapter-node-http'
import InMemoryPersister from './in-memory-persister.js'

function doRequest() {
  return new Promise(resolve => {
    let data = ''
    const req = https.request(
      {
        host: 'echo.free.beeceptor.com',
        path: '/great-shot-kid',
        method: 'GET',
        // This is non-standard and has no effect on the request
        href: 'https://echo.free.beeceptor.com/these-are-not-the-droids-you-are-looking-for'
      },
      (res) => {
        res.on('data', (chunk) => {
          data += chunk
        })
        res.on('end', () => {
          const body = JSON.parse(data)
          resolve(body.path)
        })
      }
    )

    req.on('error', (e) => {
      console.error(`problem with request: ${e.message}`);
    })

    req.end()
  })
}

console.log('Expected:')
console.log(await doRequest())

Polly.register(NodeHttpAdapter)
Polly.register(InMemoryPersister)

const polly = new Polly('node-http-test', {
  adapters: ['node-http'],
  persister: 'in-memory-persister',
})

console.log('\nActual:')
console.log(await doRequest())
await polly.stop()

Error Message & Stack Trace

N/A in this case. The request succeeds, it's just the wrong request.

Config

Copy the config used to setup the Polly instance:

javascript
Polly.register(NodeHttpAdapter)
Polly.register(InMemoryPersister)

const polly = new Polly('node-http-test', {
  adapters: ['node-http'],
  persister: 'in-memory-persister',
})

Dependencies

Copy the @pollyjs dependencies from package.json:

json
{
    "@pollyjs/adapter-node-http": "^6.0.6",
    "@pollyjs/core": "^6.0.6",
    "@pollyjs/persister": "^6.0.6"
}

Relevant Links

Environment

OS: darwin 23.6.0 Node.js v18.20.4 npm v10.7.0