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

[Vitest] AWS SDK mocking request fail when ignoring headers: TypeError: Cannot delete property 'authorization' of #<Object> #495

Open
andresgutgon opened this issue Apr 26, 2024 · 2 comments

Comments

@andresgutgon
Copy link

andresgutgon commented Apr 26, 2024

Description

Hi, thanks for this tool looks super helpful. So far is going well I manage to setup Polly.js and Vitest when testing a AWS Endpoint that is accessed using AWS JS SDK.

The problem is when I try to obfuscate headers for security reasons.

Shareable Source

This is the relevant part

// ./src/tests/polly.js
import { Polly } from '@pollyjs/core'
import NodeHttpAdapter from '@pollyjs/adapter-node-http'
import FSPersister from '@pollyjs/persister-fs'
import { beforeAll, afterAll } from 'vitest'

Polly.register(NodeHttpAdapter)
Polly.register(FSPersister)
let polly: Polly

export default function useCassettes({
  cassetteName,
  excludedHeaders = [],
}: {
  cassetteName: string
  excludedHeaders?: string[]
}) {
  beforeAll(() => {
    polly = new Polly(cassetteName, {
      adapters: ['node-http'],
      persister: 'fs',
      persisterOptions: {
        fs: {
          recordingsDir: './tests/recordings',
        },
      },
      matchRequestsBy: {
        headers: false,
        order: false,
      },
    })

    polly.server.any().on('beforePersist', (req) => {
      if (excludedHeaders.length > 0) {
        // IT FAILS HERE 💥💥💥
        req.removeHeaders(excludedHeaders)
      }
    })
  })

  afterAll(async () => {
    await polly.stop()
  })
}

Then I use this useCassettes in my tests like this:

import { describe, it, expect } from 'vitest'
import createSecret from './createSecret'
import useCassettes from 'tests/polly'

describe('createSecret', () => {
  useCassettes({
    cassetteName: 'aws_secrets',
    excludedHeaders: ['authorization']
  })

  it.only('creates the secret in aws', async (ctx) => {
    const { workspace }  = await ctx.factories.createWorkspace({
      name: 'patata1'
    })

    const result = await createSecret({
      app: 'myApp',
      workspace,
    })

    expect(result?.successful).toBeTruthy()
  })
})

Error Message & Stack Trace

I'm getting the error when trying to modify the headers beforePersits

TypeError: Cannot delete property 'authorization' of #<Object>
 ❯ Object.set ../../node_modules/.pnpm/@[email protected]/node_modules/@pollyjs/core/src/utils/http-headers.js:17:23
 ❯ Proxy.setHeader ../../node_modules/.pnpm/@[email protected]/node_modules/@pollyjs/core/src/-private/http-base.js:18:5
 ❯ Proxy.removeHeader ../../node_modules/.pnpm/@[email protected]/node_modules/@pollyjs/core/src/-private/http-base.js:32:10
 ❯ Proxy.removeHeaders ../../node_modules/.pnpm/@[email protected]/node_modules/@pollyjs/core/src/-private/http-base.js:39:12
 ❯ listener tests/polly.ts:34:13
     32|     polly.server.any().on('beforePersist', (req) => {
     33|       if (excludedHeaders.length > 0) {
     34|         req.removeHeaders(excludedHeaders)
       |             ^
     35|       }
     36|     })
 ❯ EventEmitter.emit ../../node_modules/.pnpm/@[email protected]/node_modules/@pollyjs/core/src/-private/event-emitter.js:213:13
 ❯ Route.emit ../../node_modules/.pnpm/@[email protected]/node_modules/@pollyjs/core/src/server/route.js:104:58
 ❯ FSPersister.persist ../../node_modules/.pnpm/@[email protected]/node_modules/@pollyjs/persister/dist/cjs/pollyjs-persister.js:3540:9
 ❯ Polly.stop ../../node_modules/.pnpm/@[email protected]/node_modules/@pollyjs/core/dist/cjs/pollyjs-core.js:6454:7


Dependencies

{
  "@pollyjs/adapter-node-http": "^6.0.6",
  "@pollyjs/core": "^6.0.6",
  "@pollyjs/persister-fs": "^6.0.6",
  "vite-tsconfig-paths": "^4.3.2",
  "vitest": "^1.5.0"
}

Environment

Node.js v18.7.0
darwin 22.4.0
PNPM 8.6.3
@andresgutgon andresgutgon changed the title AWS SDK mocking request fail when ignoring headers: TypeError: Cannot delete property 'authorization' of #<Object> [Vitest] AWS SDK mocking request fail when ignoring headers: TypeError: Cannot delete property 'authorization' of #<Object> Apr 26, 2024
@andresgutgon
Copy link
Author

I think the issue could be here no?
https://github.com/Netflix/pollyjs/blob/master/packages/%40pollyjs/core/src/-private/http-base.js#L104

req.headers are freeze so I can't modify them.

Is there any other way of obfuscate headers values?

@monarchwadia
Copy link

I ended up using beforeResponse instead of beforePersist and it worked well. No freezing of objects here.

Out of curiosity, did ChatGPT tell you to use beforePersist ?

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

2 participants