Skip to content

Commit

Permalink
[8.x] [Console] Fix incorrect output message when status code is 200 …
Browse files Browse the repository at this point in the history
…and body is empty (elastic#199975) (elastic#200122)

# Backport

This will backport the following commits from `main` to `8.x`:
- [[Console] Fix incorrect output message when status code is 200 and
body is empty (elastic#199975)](elastic#199975)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Ignacio
Rivas","email":"[email protected]"},"sourceCommit":{"committedDate":"2024-11-14T08:27:31Z","message":"[Console]
Fix incorrect output message when status code is 200 and body is empty
(elastic#199975)","sha":"35c2a9e31e39f068c62767dc455b26e7ce7efb0c","branchLabelMapping":{"^v9.0.0$":"main","^v8.17.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["Feature:Console","Team:Kibana
Management","release_note:skip","v9.0.0","backport:prev-minor"],"title":"[Console]
Fix incorrect output message when status code is 200 and body is
empty","number":199975,"url":"https://github.com/elastic/kibana/pull/199975","mergeCommit":{"message":"[Console]
Fix incorrect output message when status code is 200 and body is empty
(elastic#199975)","sha":"35c2a9e31e39f068c62767dc455b26e7ce7efb0c"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/199975","number":199975,"mergeCommit":{"message":"[Console]
Fix incorrect output message when status code is 200 and body is empty
(elastic#199975)","sha":"35c2a9e31e39f068c62767dc455b26e7ce7efb0c"}}]}]
BACKPORT-->

Co-authored-by: Ignacio Rivas <[email protected]>
  • Loading branch information
kibanamachine and sabarasaba authored Nov 14, 2024
1 parent 5ac9227 commit d9d93fa
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,18 @@ export function sendRequest(args: RequestArgs): Promise<RequestResult[]> {

const { statusCode, statusText } = extractStatusCodeAndText(response, path);

if (body) {
value = JSON.stringify(body, null, 2);
// When the request is sent, the HTTP library tries to parse the response body as JSON.
// However, if the response body is empty or not in valid JSON format, it throws an error.
// To handle this, if the request resolves with a 200 status code but has an empty or invalid body,
// we should still display a success message to the user.
if (statusCode === 200 && body === null) {
value = 'OK';
} else {
value = 'Request failed to get to the server (status code: ' + statusCode + ')';
if (body) {
value = JSON.stringify(body, null, 2);
} else {
value = 'Request failed to get to the server (status code: ' + statusCode + ')';
}
}

if (isMultiRequest) {
Expand Down
16 changes: 16 additions & 0 deletions test/functional/apps/console/_console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,5 +215,21 @@ export default function ({ getService, getPageObjects }: FtrProviderContext) {
expect(await PageObjects.console.hasSuccessBadge()).to.be(true);
});
});

it('Shows OK when status code is 200 but body is empty', async () => {
await PageObjects.console.clearEditorText();

// This request will return 200 but with an empty body
await PageObjects.console.enterText(
'POST /_cluster/voting_config_exclusions?node_names=node'
);
await PageObjects.console.clickPlay();

await retry.try(async () => {
const actualResponse = await PageObjects.console.getOutputText();
log.debug(actualResponse);
expect(actualResponse).to.contain('OK');
});
});
});
}

0 comments on commit d9d93fa

Please sign in to comment.