From 3100360d6a0409285d4b5d3f860615bc0a4f3d40 Mon Sep 17 00:00:00 2001 From: Yulia Cech Date: Thu, 19 Sep 2024 08:54:07 +0200 Subject: [PATCH 1/2] [Console] Add mixed case methods support --- packages/kbn-monaco/src/console/parser.js | 101 ++++++------------ .../kbn-monaco/src/console/parser.test.ts | 45 ++++++++ test/functional/apps/console/_console.ts | 13 ++- test/functional/page_objects/console_page.ts | 4 +- 4 files changed, 93 insertions(+), 70 deletions(-) diff --git a/packages/kbn-monaco/src/console/parser.js b/packages/kbn-monaco/src/console/parser.js index be0d5cbd27f68..014e579ac3477 100644 --- a/packages/kbn-monaco/src/console/parser.js +++ b/packages/kbn-monaco/src/console/parser.js @@ -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); @@ -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 + '\''); diff --git a/packages/kbn-monaco/src/console/parser.test.ts b/packages/kbn-monaco/src/console/parser.test.ts index 2c4417bdcd8a6..f9e9f3516c542 100644 --- a/packages/kbn-monaco/src/console/parser.test.ts +++ b/packages/kbn-monaco/src/console/parser.test.ts @@ -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); + }); + }); }); diff --git a/test/functional/apps/console/_console.ts b/test/functional/apps/console/_console.ts index 6b6b192373ea6..8c8f4cc653cb8 100644 --- a/test/functional/apps/console/_console.ts +++ b/test/functional/apps/console/_console.ts @@ -143,6 +143,16 @@ export default function ({ getService, getPageObjects }: FtrProviderContext) { }); }); + it('should send request with mixed case methods', async () => { + await PageObjects.console.monaco.clearEditorText(); + await PageObjects.console.monaco.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(); @@ -206,8 +216,7 @@ export default function ({ getService, getPageObjects }: FtrProviderContext) { }); }); - // not implemented for monaco yet https://github.com/elastic/kibana/issues/184010 - it.skip('should display status badges', async () => { + it('should display status badges', async () => { await sendMultipleRequests(['\n GET _search/test', '\n GET _search']); await PageObjects.header.waitUntilLoadingHasFinished(); expect(await PageObjects.console.hasWarningBadge()).to.be(true); diff --git a/test/functional/page_objects/console_page.ts b/test/functional/page_objects/console_page.ts index 080167c995ccb..c0e6edcb85722 100644 --- a/test/functional/page_objects/console_page.ts +++ b/test/functional/page_objects/console_page.ts @@ -379,11 +379,11 @@ export class ConsolePageObject extends FtrService { } public async hasSuccessBadge() { - return await this.find.existsByCssSelector('.ace_badge--success'); + return await this.find.existsByCssSelector('.monaco__status_badge--success'); } public async hasWarningBadge() { - return await this.find.existsByCssSelector('.ace_badge--warning'); + return await this.find.existsByCssSelector('.monaco__status_badge--warning'); } public async getResponseStatus() { From 2d18bfe8e9f18df9fd4dfe191484473b6ee28d9a Mon Sep 17 00:00:00 2001 From: Yulia Cech Date: Fri, 20 Sep 2024 11:26:00 +0200 Subject: [PATCH 2/2] Revert test changes --- test/functional/apps/console/_console.ts | 7 ++++--- test/functional/page_objects/console_page.ts | 4 ++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/test/functional/apps/console/_console.ts b/test/functional/apps/console/_console.ts index 8c8f4cc653cb8..af49ff0d1fa59 100644 --- a/test/functional/apps/console/_console.ts +++ b/test/functional/apps/console/_console.ts @@ -144,8 +144,8 @@ export default function ({ getService, getPageObjects }: FtrProviderContext) { }); it('should send request with mixed case methods', async () => { - await PageObjects.console.monaco.clearEditorText(); - await PageObjects.console.monaco.enterText('Get /'); + await PageObjects.console.clearEditorText(); + await PageObjects.console.enterText('Get /'); await PageObjects.console.clickPlay(); await retry.try(async () => { const status = await PageObjects.console.getResponseStatus(); @@ -216,7 +216,8 @@ export default function ({ getService, getPageObjects }: FtrProviderContext) { }); }); - it('should display status badges', async () => { + // not implemented for monaco yet https://github.com/elastic/kibana/issues/184010 + it.skip('should display status badges', async () => { await sendMultipleRequests(['\n GET _search/test', '\n GET _search']); await PageObjects.header.waitUntilLoadingHasFinished(); expect(await PageObjects.console.hasWarningBadge()).to.be(true); diff --git a/test/functional/page_objects/console_page.ts b/test/functional/page_objects/console_page.ts index c0e6edcb85722..080167c995ccb 100644 --- a/test/functional/page_objects/console_page.ts +++ b/test/functional/page_objects/console_page.ts @@ -379,11 +379,11 @@ export class ConsolePageObject extends FtrService { } public async hasSuccessBadge() { - return await this.find.existsByCssSelector('.monaco__status_badge--success'); + return await this.find.existsByCssSelector('.ace_badge--success'); } public async hasWarningBadge() { - return await this.find.existsByCssSelector('.monaco__status_badge--warning'); + return await this.find.existsByCssSelector('.ace_badge--warning'); } public async getResponseStatus() {