-
-
Notifications
You must be signed in to change notification settings - Fork 846
Query unaware interceptors cache fix #4240
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
Merged
metcoder95
merged 3 commits into
nodejs:main
from
FelixVaughan:query-unaware-interceptors-cache-fix
Jun 2, 2025
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,229 @@ | ||
| 'use strict' | ||
|
|
||
| const { test, after } = require('node:test') | ||
| const { equal, notEqual } = require('node:assert') | ||
| const { createServer } = require('node:http') | ||
| const { once } = require('node:events') | ||
| const { Client, request, interceptors } = require('../../') | ||
|
|
||
| test('query parameters create separate cache entries', async () => { | ||
| let requestCount = 0 | ||
| const server = createServer((req, res) => { | ||
| requestCount++ | ||
| const url = new URL(req.url, 'http://localhost') | ||
| const param = url.searchParams.get('param') || 'default' | ||
|
|
||
| res.writeHead(200, { | ||
| 'Content-Type': 'application/json', | ||
| 'Cache-Control': 'public, max-age=100' | ||
| }) | ||
| res.end(JSON.stringify({ | ||
| message: `Response for param=${param}`, | ||
| requestNumber: requestCount | ||
| })) | ||
| }) | ||
|
|
||
| server.listen(0) | ||
| await once(server, 'listening') | ||
|
|
||
| const client = new Client(`http://localhost:${server.address().port}`) | ||
| .compose(interceptors.cache()) | ||
|
|
||
| after(async () => { | ||
| server.close() | ||
| await client.close() | ||
| }) | ||
|
|
||
| const origin = `http://localhost:${server.address().port}` | ||
|
|
||
| // First request with param=value1 | ||
| const response1 = await request(origin, { | ||
| dispatcher: client, | ||
| query: { param: 'value1' } | ||
| }) | ||
| const body1 = await response1.body.text() | ||
| equal(requestCount, 1, 'First request should hit the server') | ||
|
|
||
| // Second request with same param - should be cached | ||
| const response2 = await request(origin, { | ||
| dispatcher: client, | ||
| query: { param: 'value1' } | ||
| }) | ||
| const body2 = await response2.body.text() | ||
| equal(requestCount, 1, 'Second request with same query should be cached') | ||
| equal(body1, body2, 'Cached response should match original') | ||
|
|
||
| // Third request with different param - should NOT be cached | ||
| const response3 = await request(origin, { | ||
| dispatcher: client, | ||
| query: { param: 'value2' } | ||
| }) | ||
| const body3 = await response3.body.text() | ||
| equal(requestCount, 2, 'Request with different query should hit the server') | ||
| notEqual(body1, body3, 'Different query parameters should create separate cache entries') | ||
| }) | ||
|
|
||
| test('complex query parameters are handled correctly', async () => { | ||
| let requestCount = 0 | ||
| const server = createServer((req, res) => { | ||
| requestCount++ | ||
|
|
||
| res.writeHead(200, { | ||
| 'Content-Type': 'application/json', | ||
| 'Cache-Control': 'public, max-age=100' | ||
| }) | ||
| res.end(JSON.stringify({ | ||
| url: req.url, | ||
| requestNumber: requestCount | ||
| })) | ||
| }) | ||
|
|
||
| server.listen(0) | ||
| await once(server, 'listening') | ||
|
|
||
| const client = new Client(`http://localhost:${server.address().port}`) | ||
| .compose(interceptors.cache()) | ||
|
|
||
| after(async () => { | ||
| server.close() | ||
| await client.close() | ||
| }) | ||
|
|
||
| const origin = `http://localhost:${server.address().port}` | ||
|
|
||
| // Complex query with arrays and multiple parameters | ||
| const complexQuery = { | ||
| search: 'hello world', | ||
| tags: ['javascript', 'nodejs'], | ||
| limit: 10, | ||
| active: true | ||
| } | ||
|
|
||
| // First request | ||
| const response1 = await request(origin, { | ||
| dispatcher: client, | ||
| query: complexQuery | ||
| }) | ||
| const body1 = await response1.body.text() | ||
| equal(requestCount, 1, 'First complex query should hit the server') | ||
|
|
||
| // Same complex query - should be cached | ||
| const response2 = await request(origin, { | ||
| dispatcher: client, | ||
| query: complexQuery | ||
| }) | ||
| const body2 = await response2.body.text() | ||
| equal(requestCount, 1, 'Same complex query should be cached') | ||
| equal(body1, body2, 'Complex query parameters should be cached correctly') | ||
|
|
||
| // Slightly different query - should NOT be cached | ||
| const response3 = await request(origin, { | ||
| dispatcher: client, | ||
| query: { | ||
| search: 'hello world', | ||
| tags: ['javascript', 'nodejs'], | ||
| limit: 20, // Different limit | ||
| active: true | ||
| } | ||
| }) | ||
| const body3 = await response3.body.text() | ||
| equal(requestCount, 2, 'Different complex query should hit the server') | ||
| notEqual(body1, body3, 'Different query parameters should create separate cache entries') | ||
| }) | ||
|
|
||
| test('query parameters vs path equivalence', async () => { | ||
| let requestCount = 0 | ||
| const server = createServer((req, res) => { | ||
| requestCount++ | ||
|
|
||
| res.writeHead(200, { | ||
| 'Content-Type': 'application/json', | ||
| 'Cache-Control': 'public, max-age=100' | ||
| }) | ||
| res.end(JSON.stringify({ | ||
| url: req.url, | ||
| requestNumber: requestCount | ||
| })) | ||
| }) | ||
|
|
||
| server.listen(0) | ||
| await once(server, 'listening') | ||
|
|
||
| const client = new Client(`http://localhost:${server.address().port}`) | ||
| .compose(interceptors.cache()) | ||
|
|
||
| after(async () => { | ||
| server.close() | ||
| await client.close() | ||
| }) | ||
|
|
||
| const origin = `http://localhost:${server.address().port}` | ||
|
|
||
| // Request using query object | ||
| const response1 = await request(origin, { | ||
| dispatcher: client, | ||
| query: { foo: 'bar', baz: 'qux' } | ||
| }) | ||
| const body1 = await response1.body.text() | ||
| equal(requestCount, 1, 'Query object request should hit the server') | ||
|
|
||
| // Request using path with query string - should be cached if URLs match | ||
| const response2 = await request(`${origin}/?foo=bar&baz=qux`, { | ||
| dispatcher: client | ||
| }) | ||
| const body2 = await response2.body.text() | ||
| equal(requestCount, 1, 'Equivalent path query should be cached') | ||
| equal(body1, body2, 'Query object and path query should be equivalent') | ||
| }) | ||
|
|
||
| test('empty and undefined query parameters', async () => { | ||
| let requestCount = 0 | ||
| const server = createServer((req, res) => { | ||
| requestCount++ | ||
|
|
||
| res.writeHead(200, { | ||
| 'Content-Type': 'application/json', | ||
| 'Cache-Control': 'public, max-age=100' | ||
| }) | ||
| res.end(JSON.stringify({ | ||
| url: req.url, | ||
| requestNumber: requestCount | ||
| })) | ||
| }) | ||
|
|
||
| server.listen(0) | ||
| await once(server, 'listening') | ||
|
|
||
| const client = new Client(`http://localhost:${server.address().port}`) | ||
| .compose(interceptors.cache()) | ||
|
|
||
| after(async () => { | ||
| server.close() | ||
| await client.close() | ||
| }) | ||
|
|
||
| const origin = `http://localhost:${server.address().port}` | ||
|
|
||
| // Request with no query | ||
| const response1 = await request(origin, { dispatcher: client }) | ||
| const body1 = await response1.body.text() | ||
| equal(requestCount, 1, 'No query request should hit the server') | ||
|
|
||
| // Request with empty query object - should be cached | ||
| const response2 = await request(origin, { | ||
| dispatcher: client, | ||
| query: {} | ||
| }) | ||
| const body2 = await response2.body.text() | ||
| equal(requestCount, 1, 'Empty query object should be cached') | ||
| equal(body1, body2, 'No query and empty query should be equivalent') | ||
|
|
||
| // Request with undefined query - should be cached | ||
| const response3 = await request(origin, { | ||
| dispatcher: client, | ||
| query: undefined | ||
| }) | ||
| const body3 = await response3.body.text() | ||
| equal(requestCount, 1, 'Undefined query should be cached') | ||
| equal(body1, body3, 'No query and undefined query should be equivalent') | ||
| }) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you sure we don't have this already serialized somewhere? We might be wasting some resources. Also, I would prefer if we used the previous logic so we are sure the actual result is identical.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea! Looks like we already have a util function to handle serialization.