Skip to content

Commit

Permalink
[Console] Allow mixed case methods (elastic#193445)
Browse files Browse the repository at this point in the history
## Summary

Fixes https://github.com//elastic/kibana/issues/186774
This PR is re-opened from elastic#190527
after too many merge conflicts.

This PR adds a new function to the parser to allow mixed case methods
like GeT, posT etc





### Checklist

Delete any items that are not applicable to this PR.

- [ ] Any text added follows [EUI's writing
guidelines](https://elastic.github.io/eui/#/guidelines/writing), uses
sentence case text and includes [i18n
support](https://github.com/elastic/kibana/blob/main/packages/kbn-i18n/README.md)
- [ ]
[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)
was added for features that require explanation or tutorials
- [ ] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios
- [ ] [Flaky Test
Runner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was
used on any tests changed
- [ ] Any UI touched in this PR is usable by keyboard only (learn more
about [keyboard accessibility](https://webaim.org/techniques/keyboard/))
- [ ] Any UI touched in this PR does not create any new axe failures
(run axe in browser:
[FF](https://addons.mozilla.org/en-US/firefox/addon/axe-devtools/),
[Chrome](https://chrome.google.com/webstore/detail/axe-web-accessibility-tes/lhdoppojpmngadmnindnejefpokejbdd?hl=en-US))
- [ ] If a plugin configuration key changed, check if it needs to be
allowlisted in the cloud and added to the [docker
list](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)
- [ ] This renders correctly on smaller devices using a responsive
layout. (You can test this [in your
browser](https://www.browserstack.com/guide/responsive-testing-on-local-server))
- [ ] This was checked for [cross-browser
compatibility](https://www.elastic.co/support/matrix#matrix_browsers)


### Risk Matrix

Delete this section if it is not applicable to this PR.

Before closing this PR, invite QA, stakeholders, and other developers to
identify risks that should be tested prior to the change/feature
release.

When forming the risk matrix, consider some of the following examples
and how they may potentially impact the change:

| Risk | Probability | Severity | Mitigation/Notes |

|---------------------------|-------------|----------|-------------------------|
| Multiple Spaces—unexpected behavior in non-default Kibana Space.
| Low | High | Integration tests will verify that all features are still
supported in non-default Kibana Space and when user switches between
spaces. |
| Multiple nodes—Elasticsearch polling might have race conditions
when multiple Kibana nodes are polling for the same tasks. | High | Low
| Tasks are idempotent, so executing them multiple times will not result
in logical error, but will degrade performance. To test for this case we
add plenty of unit tests around this logic and document manual testing
procedure. |
| Code should gracefully handle cases when feature X or plugin Y are
disabled. | Medium | High | Unit tests will verify that any feature flag
or plugin combination still results in our service operational. |
| [See more potential risk
examples](https://github.com/elastic/kibana/blob/main/RISK_MATRIX.mdx) |


### For maintainers

- [ ] This was checked for breaking API changes and was [labeled
appropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)
  • Loading branch information
yuliacech authored Sep 20, 2024
1 parent 682afb7 commit 52aa453
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 66 deletions.
101 changes: 35 additions & 66 deletions packages/kbn-monaco/src/console/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ export const createParser = () => {
at += 1;
return ch;
},
nextOneOf = function (chars) {
if (chars && !chars.includes(ch)) {
error('Expected one of ' + chars + ' instead of \'' + ch + '\'');
}
ch = text.charAt(at);
at += 1;
return ch;
},
nextUpTo = function (upTo, errorMessage) {
let currentAt = at,
i = text.indexOf(upTo, currentAt);
Expand Down Expand Up @@ -221,84 +229,45 @@ export const createParser = () => {
},
// parses and returns the method
method = function () {
switch (ch) {
case 'g':
next('g');
next('e');
next('t');
return 'get';
const upperCaseChar = ch.toUpperCase();
switch (upperCaseChar) {
case 'G':
next('G');
next('E');
next('T');
nextOneOf(['G', 'g']);
nextOneOf(['E', 'e']);
nextOneOf(['T', 't']);
return 'GET';
case 'h':
next('h');
next('e');
next('a');
next('d');
return 'head';
case 'H':
next('H');
next('E');
next('A');
next('D');
nextOneOf(['H', 'h']);
nextOneOf(['E', 'e']);
nextOneOf(['A', 'a']);
nextOneOf(['D', 'd']);
return 'HEAD';
case 'd':
next('d');
next('e');
next('l');
next('e');
next('t');
next('e');
return 'delete';
case 'D':
next('D');
next('E');
next('L');
next('E');
next('T');
next('E');
nextOneOf(['D', 'd']);
nextOneOf(['E', 'e']);
nextOneOf(['L', 'l']);
nextOneOf(['E', 'e']);
nextOneOf(['T', 't']);
nextOneOf(['E', 'e']);
return 'DELETE';
case 'p':
next('p');
switch (ch) {
case 'a':
next('a');
next('t');
next('c');
next('h');
return 'patch';
case 'u':
next('u');
next('t');
return 'put';
case 'o':
next('o');
next('s');
next('t');
return 'post';
default:
error('Unexpected \'' + ch + '\'');
}
break;
case 'P':
next('P');
switch (ch) {
nextOneOf(['P', 'p']);
const nextUpperCaseChar = ch.toUpperCase();
switch (nextUpperCaseChar) {
case 'A':
next('A');
next('T');
next('C');
next('H');
nextOneOf(['A', 'a']);
nextOneOf(['T', 't']);
nextOneOf(['C', 'c']);
nextOneOf(['H', 'h']);
return 'PATCH';
case 'U':
next('U');
next('T');
nextOneOf(['U', 'u']);
nextOneOf(['T', 't']);
return 'PUT';
case 'O':
next('O');
next('S');
next('T');
nextOneOf(['O', 'o']);
nextOneOf(['S', 's']);
nextOneOf(['T', 't']);
return 'POST';
default:
error('Unexpected \'' + ch + '\'');
Expand Down
45 changes: 45 additions & 0 deletions packages/kbn-monaco/src/console/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,49 @@ describe('console parser', () => {
expect(startOffset).toBe(0);
expect(endOffset).toBe(52);
});

describe('case insensitive methods', () => {
const expectedRequests = [
{
startOffset: 0,
endOffset: 11,
},
{
startOffset: 12,
endOffset: 24,
},
{
startOffset: 25,
endOffset: 38,
},
{
startOffset: 39,
endOffset: 50,
},
{
startOffset: 51,
endOffset: 63,
},
];
it('allows upper case methods', () => {
const input = 'GET _search\nPOST _search\nPATCH _search\nPUT _search\nHEAD _search';
const { requests, errors } = parser(input) as ConsoleParserResult;
expect(errors.length).toBe(0);
expect(requests).toEqual(expectedRequests);
});

it('allows lower case methods', () => {
const input = 'get _search\npost _search\npatch _search\nput _search\nhead _search';
const { requests, errors } = parser(input) as ConsoleParserResult;
expect(errors.length).toBe(0);
expect(requests).toEqual(expectedRequests);
});

it('allows mixed case methods', () => {
const input = 'GeT _search\npOSt _search\nPaTch _search\nPut _search\nheAD _search';
const { requests, errors } = parser(input) as ConsoleParserResult;
expect(errors.length).toBe(0);
expect(requests).toEqual(expectedRequests);
});
});
});
10 changes: 10 additions & 0 deletions test/functional/apps/console/_console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,16 @@ export default function ({ getService, getPageObjects }: FtrProviderContext) {
});
});

it('should send request with mixed case methods', async () => {
await PageObjects.console.clearEditorText();
await PageObjects.console.enterText('Get /');
await PageObjects.console.clickPlay();
await retry.try(async () => {
const status = await PageObjects.console.getResponseStatus();
expect(status).to.eql(200);
});
});

describe('with kbn: prefix in request', () => {
before(async () => {
await PageObjects.console.clearEditorText();
Expand Down

0 comments on commit 52aa453

Please sign in to comment.