Netflix / pollyjs

Record, Replay, and Stub HTTP Interactions.
https://netflix.github.io/pollyjs
Apache License 2.0
10.2k stars 350 forks source link

[Question] Record and replay #400

Open 40x opened 3 years ago

40x commented 3 years ago

Description

Is it possible to record and replay at the same time? I have a node proxy server for a web app in which I am trying to request the resources only once and then server them from the recorded cache.

Config

This is the config that I have so far, it seems to be recording (I can see it on the logs) but not replaying (keeps recording instead). Should I be using a different kind of persister instead?

new Polly('Recording Name', {
        adapters: ['node-http'],
        persister: 'fs',
        persisterOptions: {
            fs: {
                recordingsDir
            }
        },
        logging: false,
        mode: 'replay',
        recordIfMissing: true,
        recordFailedRequests: true,
        flushRequestsOnStop: true,
        matchRequestsBy: {
            headers: false,
            order: false
        }
    });

Dependencies

{
    "@pollyjs/adapter-fetch": "5.1.1",
    "@pollyjs/adapter-node-http": "5.1.0",
    "@pollyjs/adapter-xhr": "5.1.1",
    "@pollyjs/core": "5.1.0",
    "@pollyjs/persister-fs": "5.0.0",
}

Environment

Node.js v14.16.0 darwin 19.6.0 npm 6.14.11

offirgolan commented 3 years ago

Your config looks right to me. Do you have a header or something in the url that's constantly changing (e.g. a date, time stamp, or uuid) on every recording? If so, you'll need to ignore it by providing additional config options via matchRequestsBy.

40x commented 3 years ago

No, I don't, I'll try to break down my use case and I'd love to know if it's even possible to do it or not.

  1. User visits home page. We make API calls. They are recorded via our proxy server
  2. User navigates to another page
  3. User goes back to home page.

Expected the API calls to be replayed but they didn't. When I stop our proxy server (SIGINT) I see a few har file with those API calls. I understand what it is doing and why (I'm assuming it's cause I have an fs persister which reads/writes to HAR).

Not sure where to go from here.

samuelko123 commented 10 months ago

I have a similar requirement.

Looked at the source code. Apparently we can call await polly.persister?.persist(); to persist before stopping the server. https://github.com/Netflix/pollyjs/blob/9b6bede12b7ee998472b8883c9dd01e2159e00a8/packages/%40pollyjs/core/src/polly.js#L252

Hope it helps.