-
Notifications
You must be signed in to change notification settings - Fork 28.4k
fix(HTTP Request Node): Add support for Bearer Auth in HttpRequest node #15043
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
ShireenMissi
merged 2 commits into
master
from
node-2909-community-issue-401-unauthorized-no-authorization-token-provided-error
May 2, 2025
Merged
Changes from all commits
Commits
Show all changes
2 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
1 change: 0 additions & 1 deletion
1
packages/nodes-base/nodes/EmailReadImap/test/v2/EmailReadImapV2.node.test.ts
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
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
163 changes: 163 additions & 0 deletions
163
packages/nodes-base/nodes/HttpRequest/test/node/HttpRequestV2.test.ts
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,163 @@ | ||
import type { IExecuteFunctions, INodeTypeBaseDescription } from 'n8n-workflow'; | ||
|
||
import { HttpRequestV2 } from '../../V2/HttpRequestV2.node'; | ||
|
||
describe('HttpRequestV2', () => { | ||
let node: HttpRequestV2; | ||
let executeFunctions: IExecuteFunctions; | ||
|
||
const baseUrl = 'http://example.com'; | ||
const options = { | ||
redirect: '', | ||
batching: { batch: { batchSize: 1, batchInterval: 1 } }, | ||
proxy: '', | ||
timeout: '', | ||
allowUnauthorizedCerts: '', | ||
queryParameterArrays: '', | ||
response: '', | ||
lowercaseHeaders: '', | ||
}; | ||
|
||
beforeEach(() => { | ||
const baseDescription: INodeTypeBaseDescription = { | ||
displayName: 'HTTP Request', | ||
name: 'httpRequest', | ||
description: 'Makes an HTTP request and returns the response data', | ||
group: [], | ||
}; | ||
node = new HttpRequestV2(baseDescription); | ||
executeFunctions = { | ||
getInputData: jest.fn(), | ||
getNodeParameter: jest.fn(), | ||
getNode: jest.fn(() => { | ||
return { | ||
type: 'n8n-nodes-base.httpRequest', | ||
typeVersion: 2, | ||
}; | ||
}), | ||
getCredentials: jest.fn(), | ||
helpers: { | ||
request: jest.fn(), | ||
requestOAuth1: jest.fn( | ||
async () => | ||
await Promise.resolve({ | ||
success: true, | ||
}), | ||
), | ||
requestOAuth2: jest.fn( | ||
async () => | ||
await Promise.resolve({ | ||
success: true, | ||
}), | ||
), | ||
requestWithAuthentication: jest.fn(), | ||
requestWithAuthenticationPaginated: jest.fn(), | ||
assertBinaryData: jest.fn(), | ||
getBinaryStream: jest.fn(), | ||
getBinaryMetadata: jest.fn(), | ||
binaryToString: jest.fn((buffer: Buffer) => { | ||
return buffer.toString(); | ||
}), | ||
prepareBinaryData: jest.fn(), | ||
}, | ||
getContext: jest.fn(), | ||
sendMessageToUI: jest.fn(), | ||
continueOnFail: jest.fn(), | ||
getMode: jest.fn(), | ||
} as unknown as IExecuteFunctions; | ||
}); | ||
|
||
describe('Authentication Handling', () => { | ||
ShireenMissi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const authenticationTypes = [ | ||
{ | ||
genericCredentialType: 'httpBasicAuth', | ||
credentials: { user: 'username', password: 'password' }, | ||
authField: 'auth', | ||
authValue: { user: 'username', pass: 'password' }, | ||
}, | ||
{ | ||
genericCredentialType: 'httpBearerAuth', | ||
credentials: { token: 'bearerToken123' }, | ||
authField: 'headers', | ||
authValue: { Authorization: 'Bearer bearerToken123' }, | ||
}, | ||
{ | ||
genericCredentialType: 'httpDigestAuth', | ||
credentials: { user: 'username', password: 'password' }, | ||
authField: 'auth', | ||
authValue: { user: 'username', pass: 'password', sendImmediately: false }, | ||
}, | ||
{ | ||
genericCredentialType: 'httpHeaderAuth', | ||
credentials: { name: 'Authorization', value: 'Bearer token' }, | ||
authField: 'headers', | ||
authValue: { Authorization: 'Bearer token' }, | ||
}, | ||
{ | ||
genericCredentialType: 'httpQueryAuth', | ||
credentials: { name: 'Token', value: 'secretToken' }, | ||
authField: 'qs', | ||
authValue: { Token: 'secretToken' }, | ||
}, | ||
{ | ||
genericCredentialType: 'oAuth1Api', | ||
credentials: { oauth_token: 'token', oauth_token_secret: 'secret' }, | ||
authField: 'oauth', | ||
authValue: { oauth_token: 'token', oauth_token_secret: 'secret' }, | ||
}, | ||
{ | ||
genericCredentialType: 'oAuth2Api', | ||
credentials: { access_token: 'accessToken' }, | ||
authField: 'auth', | ||
authValue: { bearer: 'accessToken' }, | ||
}, | ||
]; | ||
|
||
it.each(authenticationTypes)( | ||
'should handle $genericCredentialType authentication', | ||
async ({ genericCredentialType, credentials, authField, authValue }) => { | ||
(executeFunctions.getInputData as jest.Mock).mockReturnValue([{ json: {} }]); | ||
(executeFunctions.getNodeParameter as jest.Mock).mockImplementation((paramName: string) => { | ||
switch (paramName) { | ||
case 'method': | ||
return 'GET'; | ||
case 'url': | ||
return baseUrl; | ||
case 'authentication': | ||
return 'genericCredentialType'; | ||
case 'genericAuthType': | ||
return genericCredentialType; | ||
case 'options': | ||
return options; | ||
case 'bodyParametersUi': | ||
case 'headerParametersUi': | ||
case 'queryParametersUi': | ||
return { parameter: [] }; | ||
default: | ||
return undefined; | ||
} | ||
}); | ||
|
||
(executeFunctions.getCredentials as jest.Mock).mockResolvedValue(credentials); | ||
const response = { | ||
success: true, | ||
}; | ||
(executeFunctions.helpers.request as jest.Mock).mockResolvedValue(response); | ||
|
||
const result = await node.execute.call(executeFunctions); | ||
expect(result).toEqual([[{ json: { success: true }, pairedItem: { item: 0 } }]]); | ||
if (genericCredentialType === 'oAuth1Api') { | ||
expect(executeFunctions.helpers.requestOAuth1).toHaveBeenCalled(); | ||
} else if (genericCredentialType === 'oAuth2Api') { | ||
expect(executeFunctions.helpers.requestOAuth2).toHaveBeenCalled(); | ||
} else { | ||
expect(executeFunctions.helpers.request).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
[authField]: expect.objectContaining(authValue), | ||
}), | ||
); | ||
} | ||
}, | ||
); | ||
}); | ||
}); |
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
1 change: 0 additions & 1 deletion
1
packages/nodes-base/nodes/Transform/RemoveDuplicates/v2/test/RemoveDuplicates.test.ts
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
Oops, something went wrong.
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.