Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,21 @@ const extractStatusCodeAndText = (response: Response | undefined, path: string)
// For ES requests, we need to extract the status code and text from the response
// headers, due to the way the proxy set up to avoid mirroring the status code which could be 401
// and trigger a login prompt. See for more details: https://github.com/elastic/kibana/issues/140536
const statusCode = parseInt(response?.headers.get('x-console-proxy-status-code') ?? '500', 10);
const statusText = response?.headers.get('x-console-proxy-status-text') ?? 'error';
const proxyStatusCode = response?.headers.get('x-console-proxy-status-code');
const proxyStatusText = response?.headers.get('x-console-proxy-status-text');

return { statusCode, statusText };
// If proxy headers are missing (e.g., validation errors), use the actual response status
if (!proxyStatusCode) {
return {
statusCode: parseInt(String(response?.status ?? 500), 10),
statusText: response?.statusText ?? 'error',
};
}

return {
statusCode: parseInt(proxyStatusCode, 10),
statusText: proxyStatusText ?? 'error',
};
};

let CURRENT_REQ_ID = 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import { HttpSetup } from '@kbn/core/public';

jest.unmock('./send_request');

describe('Status Code Extraction in sendRequest', () => {
let mockHttp: jest.Mocked<HttpSetup>;

beforeEach(() => {
mockHttp = {
post: jest.fn(),
} as any;
});

afterEach(() => {
jest.clearAllMocks();
});

describe('extractStatusCodeAndText function behavior', () => {
it('should use proxy headers when available for ES requests', async () => {
const { sendRequest } = await import('./send_request');

const mockResponse = {
response: {
status: 200, // Actual HTTP status
statusText: 'OK',
headers: new Map([
['x-console-proxy-status-code', '404'], // ES status from proxy
['x-console-proxy-status-text', 'Not Found'],
['Content-Type', 'application/json'],
]),
},
body: JSON.stringify({ error: 'index_not_found_exception' }),
};

mockHttp.post.mockResolvedValue(mockResponse);

const result = await sendRequest({
http: mockHttp,
requests: [{ url: '/_search', method: 'GET', data: ['{}'] }],
});

expect(result[0].response.statusCode).toBe(404); // Should use proxy header
expect(result[0].response.statusText).toBe('Not Found');
});

it('should fall back to actual response status when proxy headers are missing', async () => {
const { sendRequest } = await import('./send_request');

const mockResponse = {
response: {
status: 400,
statusText: 'Bad Request',
headers: new Map([['Content-Type', 'application/json']]),
},
body: JSON.stringify({
statusCode: 400,
error: 'Bad Request',
message:
"Method must be one of, case insensitive ['HEAD', 'GET', 'POST', 'PUT', 'DELETE', 'PATCH']. Received 'INVALIDMETHOD'.",
}),
};

mockHttp.post.mockResolvedValue(mockResponse);

const result = await sendRequest({
http: mockHttp,
requests: [{ url: '/_search', method: 'INVALIDMETHOD', data: ['{}'] }],
});

expect(result[0].response.statusCode).toBe(400); // Should use actual response status, not 500
expect(result[0].response.statusText).toBe('Bad Request');
});

it('should handle empty proxy header as missing header', async () => {
const { sendRequest } = await import('./send_request');

const mockResponse = {
response: {
status: 400,
statusText: 'Bad Request',
headers: new Map([
['x-console-proxy-status-code', ''], // Empty header value
['Content-Type', 'application/json'],
]),
},
body: JSON.stringify({ error: 'validation error' }),
};

mockHttp.post.mockResolvedValue(mockResponse);

const result = await sendRequest({
http: mockHttp,
requests: [{ url: '/_search', method: 'INVALID', data: ['{}'] }],
});

expect(result[0].response.statusCode).toBe(400); // Should fall back to actual status
expect(result[0].response.statusText).toBe('Bad Request');
});
});
});