mirror of
https://github.com/n8n-io/n8n.git
synced 2026-08-28 17:22:01 +08:00
fix(Wordpress Node): Send no-cache header on write requests (#20473)
Signed-off-by: Muhammed Hussain Karimi <info@karimi.dev> Co-authored-by: Elias Meire <elias@meire.dev>
This commit is contained in:
committed by
GitHub
parent
62d5de3ec7
commit
077370ff56
@@ -42,11 +42,18 @@ export async function wordpressApiRequest(
|
||||
rejectUnauthorized = !(credentials.allowUnauthorizedCerts as boolean);
|
||||
}
|
||||
|
||||
const headers: IDataObject = {
|
||||
Accept: 'application/json',
|
||||
'Content-Type': 'application/json',
|
||||
};
|
||||
// Some WordPress caching plugins ignore the request method and serve a cached
|
||||
// GET response to writes, which silently returns existing posts instead of creating one.
|
||||
if (!['GET', 'HEAD'].includes(method)) {
|
||||
headers['Cache-Control'] = 'no-cache';
|
||||
}
|
||||
|
||||
let options: IRequestOptions = {
|
||||
headers: {
|
||||
Accept: 'application/json',
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
headers,
|
||||
method,
|
||||
qs,
|
||||
body,
|
||||
|
||||
@@ -46,6 +46,26 @@ describe('Wordpress > GenericFunctions', () => {
|
||||
);
|
||||
});
|
||||
|
||||
it('should set Cache-Control to no-cache for write requests', async () => {
|
||||
mockFunctions.helpers.requestWithAuthentication.mockResolvedValue({ data: 'testData' });
|
||||
await wordpressApiRequest.call(mockFunctions, 'POST', '/posts', {}, {});
|
||||
|
||||
expect(mockFunctions.helpers.requestWithAuthentication).toHaveBeenCalledWith(
|
||||
'wordpressApi',
|
||||
expect.objectContaining({
|
||||
headers: expect.objectContaining({ 'Cache-Control': 'no-cache' }),
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it('should not set Cache-Control for GET requests', async () => {
|
||||
mockFunctions.helpers.requestWithAuthentication.mockResolvedValue({ data: 'testData' });
|
||||
await wordpressApiRequest.call(mockFunctions, 'GET', '/posts', {}, {});
|
||||
|
||||
const callArgs = mockFunctions.helpers.requestWithAuthentication.mock.calls[0][1];
|
||||
expect(callArgs.headers).not.toHaveProperty('Cache-Control');
|
||||
});
|
||||
|
||||
it('should throw NodeApiError on failure', async () => {
|
||||
mockFunctions.helpers.requestWithAuthentication.mockRejectedValue({ message: 'fail' });
|
||||
await expect(
|
||||
|
||||
Reference in New Issue
Block a user