mirror of
https://github.com/n8n-io/n8n.git
synced 2025-03-05 20:50:17 -08:00
fix(Elasticsearch Node): Fix issue stopping search queries being sent (#11464)
This commit is contained in:
parent
54b02acdd7
commit
388a83dfbd
|
@ -56,7 +56,7 @@ export class ElasticsearchApi implements ICredentialType {
|
||||||
|
|
||||||
test: ICredentialTestRequest = {
|
test: ICredentialTestRequest = {
|
||||||
request: {
|
request: {
|
||||||
baseURL: '={{$credentials.baseUrl}}',
|
baseURL: '={{$credentials.baseUrl}}'.replace(/\/$/, ''),
|
||||||
url: '/_xpack?human=false',
|
url: '/_xpack?human=false',
|
||||||
skipSslCertificateValidation: '={{$credentials.ignoreSSLIssues}}',
|
skipSslCertificateValidation: '={{$credentials.ignoreSSLIssues}}',
|
||||||
},
|
},
|
||||||
|
|
|
@ -172,7 +172,7 @@ export class Elasticsearch implements INodeType {
|
||||||
} else {
|
} else {
|
||||||
responseData = await elasticsearchApiRequest.call(
|
responseData = await elasticsearchApiRequest.call(
|
||||||
this,
|
this,
|
||||||
'GET',
|
'POST',
|
||||||
`/${indexId}/_search`,
|
`/${indexId}/_search`,
|
||||||
body,
|
body,
|
||||||
qs,
|
qs,
|
||||||
|
@ -184,7 +184,7 @@ export class Elasticsearch implements INodeType {
|
||||||
|
|
||||||
responseData = await elasticsearchApiRequest.call(
|
responseData = await elasticsearchApiRequest.call(
|
||||||
this,
|
this,
|
||||||
'GET',
|
'POST',
|
||||||
`/${indexId}/_search`,
|
`/${indexId}/_search`,
|
||||||
body,
|
body,
|
||||||
qs,
|
qs,
|
||||||
|
|
|
@ -20,7 +20,7 @@ export async function elasticsearchBulkApiRequest(this: IExecuteFunctions, body:
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: { 'Content-Type': 'application/x-ndjson' },
|
headers: { 'Content-Type': 'application/x-ndjson' },
|
||||||
body: bulkBody,
|
body: bulkBody,
|
||||||
url: `${baseUrl}/_bulk`,
|
url: `${baseUrl.replace(/\/$/, '')}/_bulk`,
|
||||||
skipSslCertificateValidation: ignoreSSLIssues,
|
skipSslCertificateValidation: ignoreSSLIssues,
|
||||||
returnFullResponse: true,
|
returnFullResponse: true,
|
||||||
ignoreHttpStatusErrors: true,
|
ignoreHttpStatusErrors: true,
|
||||||
|
@ -66,7 +66,7 @@ export async function elasticsearchApiRequest(
|
||||||
method,
|
method,
|
||||||
body,
|
body,
|
||||||
qs,
|
qs,
|
||||||
url: `${baseUrl}${endpoint}`,
|
url: `${baseUrl.replace(/\/$/, '')}${endpoint}`,
|
||||||
json: true,
|
json: true,
|
||||||
skipSslCertificateValidation: ignoreSSLIssues,
|
skipSslCertificateValidation: ignoreSSLIssues,
|
||||||
};
|
};
|
||||||
|
|
|
@ -0,0 +1,112 @@
|
||||||
|
import { type IExecuteFunctions, NodeApiError } from 'n8n-workflow';
|
||||||
|
|
||||||
|
import { elasticsearchApiRequest } from '../GenericFunctions';
|
||||||
|
|
||||||
|
describe('Elasticsearch -> elasticsearchApiRequest', () => {
|
||||||
|
let mockExecuteFunctions: IExecuteFunctions;
|
||||||
|
|
||||||
|
const mockHttpRequestWithAuthentication = jest.fn();
|
||||||
|
|
||||||
|
const setupMockFunctions = () => {
|
||||||
|
mockExecuteFunctions = {
|
||||||
|
getCredentials: jest.fn().mockResolvedValue({
|
||||||
|
baseUrl: 'https://example.com',
|
||||||
|
ignoreSSLIssues: false,
|
||||||
|
}),
|
||||||
|
helpers: {
|
||||||
|
httpRequestWithAuthentication: mockHttpRequestWithAuthentication,
|
||||||
|
},
|
||||||
|
getNode: jest.fn().mockReturnValue({}),
|
||||||
|
} as unknown as IExecuteFunctions;
|
||||||
|
jest.clearAllMocks();
|
||||||
|
};
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
setupMockFunctions();
|
||||||
|
mockHttpRequestWithAuthentication.mockClear();
|
||||||
|
});
|
||||||
|
|
||||||
|
const response = { success: true };
|
||||||
|
|
||||||
|
it('should make a successful GET API request', async () => {
|
||||||
|
mockHttpRequestWithAuthentication.mockResolvedValue(response);
|
||||||
|
|
||||||
|
const result = await elasticsearchApiRequest.call(
|
||||||
|
mockExecuteFunctions,
|
||||||
|
'GET',
|
||||||
|
'/test-endpoint',
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(result).toEqual(response);
|
||||||
|
expect(mockExecuteFunctions.helpers.httpRequestWithAuthentication).toHaveBeenCalledWith(
|
||||||
|
'elasticsearchApi',
|
||||||
|
expect.objectContaining({
|
||||||
|
method: 'GET',
|
||||||
|
url: 'https://example.com/test-endpoint',
|
||||||
|
json: true,
|
||||||
|
skipSslCertificateValidation: false,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should make a successful POST API request', async () => {
|
||||||
|
const body = { key: 'value' };
|
||||||
|
|
||||||
|
mockHttpRequestWithAuthentication.mockResolvedValue(response);
|
||||||
|
|
||||||
|
const result = await elasticsearchApiRequest.call(
|
||||||
|
mockExecuteFunctions,
|
||||||
|
'POST',
|
||||||
|
'/test-endpoint',
|
||||||
|
body,
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(result).toEqual(response);
|
||||||
|
expect(mockExecuteFunctions.helpers.httpRequestWithAuthentication).toHaveBeenCalledWith(
|
||||||
|
'elasticsearchApi',
|
||||||
|
expect.objectContaining({
|
||||||
|
body,
|
||||||
|
method: 'POST',
|
||||||
|
url: 'https://example.com/test-endpoint',
|
||||||
|
json: true,
|
||||||
|
skipSslCertificateValidation: false,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should handle API request errors', async () => {
|
||||||
|
const errorResponse = { message: 'Error occurred' };
|
||||||
|
mockHttpRequestWithAuthentication.mockRejectedValue(errorResponse);
|
||||||
|
|
||||||
|
await expect(
|
||||||
|
elasticsearchApiRequest.call(mockExecuteFunctions, 'GET', '/test-endpoint'),
|
||||||
|
).rejects.toThrow(NodeApiError);
|
||||||
|
|
||||||
|
expect(mockExecuteFunctions.helpers.httpRequestWithAuthentication).toHaveBeenCalledWith(
|
||||||
|
'elasticsearchApi',
|
||||||
|
expect.objectContaining({
|
||||||
|
method: 'GET',
|
||||||
|
url: 'https://example.com/test-endpoint',
|
||||||
|
json: true,
|
||||||
|
skipSslCertificateValidation: false,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should ignore trailing slashes in the base URL', async () => {
|
||||||
|
mockHttpRequestWithAuthentication.mockResolvedValue(response);
|
||||||
|
|
||||||
|
mockExecuteFunctions.getCredentials = jest.fn().mockResolvedValue({
|
||||||
|
baseUrl: 'https://elastic.domain.com/',
|
||||||
|
ignoreSSLIssues: false,
|
||||||
|
});
|
||||||
|
await elasticsearchApiRequest.call(mockExecuteFunctions, 'GET', '/test-endpoint');
|
||||||
|
|
||||||
|
expect(mockExecuteFunctions.helpers.httpRequestWithAuthentication).toHaveBeenCalledWith(
|
||||||
|
'elasticsearchApi',
|
||||||
|
expect.objectContaining({
|
||||||
|
url: 'https://elastic.domain.com/test-endpoint',
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
Loading…
Reference in a new issue