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 64d71ddbfc82d..27339c408de85 100644 --- a/test/functional/apps/console/_console.ts +++ b/test/functional/apps/console/_console.ts @@ -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();