Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
sflanker opened this issue Sep 11, 2024 · 0 comments

Comments

@sflanker
Copy link

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

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:

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:

{
    "@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant