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

fix: cookies correlation #83

Merged
merged 5 commits into from
Aug 5, 2024
Merged

fix: cookies correlation #83

merged 5 commits into from
Aug 5, 2024

Conversation

Llandy3d
Copy link
Member

@Llandy3d Llandy3d commented Aug 3, 2024

This fixes the issue where cookies and headers were not correctly correlated as the variable was not in a backticks string.

Other major changes are:

  • headers are now excluding the Cookie field in favor of the k6 cookies parameter
  • cookies are being defined explicitly

Regarding the cookies point, it means that now the script is really verbose due to cookies needing an object specifying replace for making it work with k6.
This is because k6 has a cookiejar that automatically saves cookies and reuse them in further request, this was interfering with our own cookies making them duplicated, hence the replace: true.

I think this is good enough for now since it makes the application work but I would like to have a better solutions for cookies when we have time, for example we could rely always on the cookiejar and only when a specific cookie is specified/modified we selectively target that one explicitly. It will need more brainstorming and thoughts.

@Llandy3d Llandy3d requested a review from going-confetti August 3, 2024 02:08
Copy link
Collaborator

@going-confetti going-confetti left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I found a bug

@@ -152,10 +152,10 @@ function generateRequestParams(request: ProxyData['request']): string {
return `
{
headers: {
${request.headers.map(([name, value]) => `'${name}': '${value}'`).join(',\n')}
${request.headers.map(([name, value]) => (name !== 'Cookie' ? `'${name}': \`${value}\`` : '')).join(',\n')}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this can generate invalid code, because a Cookie header will produce an extra comma:

headers {
  headerA: '...',
  ,
  headerB: '...',
}

Here's what you can do:

request.headers
  .filter(([name]) => name !== 'Cookie')
  .map(([name, value]) => `'${name}': \`${value}\``)
  .join(',')

Copy link
Collaborator

@e-fisher e-fisher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a good candidate for a unit test 🧪

}
}
`
}

// @ts-expect-error we have commonjs set as module option
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of scope for this PR, but according to the typescript docs,

You very likely want "nodenext" for modern Node.js projects and preserve or esnext for code that will be bundled.

Should we try that out?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indeed! I wanted to discuss the commonjs setting and what would be the appropriate one to use as I feel you both know more than me on the topic.

I've marked in this PR and others the error with the commonjs keyword so that when we make a decision it should be easy to remove these extra lines

}
}
`
}

// @ts-expect-error we have commonjs set as module option
if (import.meta.vitest) {
Copy link
Collaborator

@going-confetti going-confetti Aug 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't mind in source testing, but we already have tests for codegen.ts in codegen.test.ts - isn't it a better place for these tests as well? At least for now.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless I missunderstood something I would say nope.

codegen.test.ts makes sense to test the exported functionality or have bigger components tests.
In source makes sense in here because of the locality of the function and it being private (limited to testing the unit).

I guess we can export it and move the test there, but I don't see in-source & filename.test.ts replacing one another any time soon. They are complementary.

With that said, no strong opinions, do you think it's better here to export the function and test it in codegen.test.ts anyways ? 🤔

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have a strong opinion either. Your explanation sounds good 👍

Copy link
Collaborator

@going-confetti going-confetti left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Collaborator

@e-fisher e-fisher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@Llandy3d Llandy3d merged commit 87102cc into main Aug 5, 2024
1 check passed
@Llandy3d Llandy3d deleted the fix_correlation branch August 5, 2024 12:29
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

Successfully merging this pull request may close these issues.

3 participants