Skip to content

Commit

Permalink
[Console] Fix bug in which selecting requests with characters ending …
Browse files Browse the repository at this point in the history
…with '{}' was not possible

Co-authored-by: Muhammad Ibragimov <[email protected]>
  • Loading branch information
mibragimov and Muhammad Ibragimov authored Sep 7, 2022
1 parent 6f599e5 commit cda1e3d
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 2 deletions.
91 changes: 91 additions & 0 deletions src/plugins/console/public/lib/row_parser.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* 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 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 or the Server
* Side Public License, v 1.
*/

import '../application/models/legacy_core_editor/legacy_core_editor.test.mocks';

import RowParser from './row_parser';
import { create, MODE } from '../application/models';
import type { SenseEditor } from '../application/models';
import type { CoreEditor } from '../types';

describe('RowParser', () => {
let editor: SenseEditor | null;
let parser: RowParser | null;

beforeEach(function () {
// Set up our document body
document.body.innerHTML = `<div>
<div id="ConAppEditor" />
<div id="ConAppEditorActions" />
<div id="ConCopyAsCurl" />
</div>`;
editor = create(document.getElementById('ConAppEditor')!);
parser = new RowParser(editor.getCoreEditor() as CoreEditor);
});

afterEach(function () {
editor?.getCoreEditor().destroy();
editor = null;
parser = null;
});

describe('getRowParseMode', () => {
const forceRetokenize = false;

it('should return MODE.BETWEEN_REQUESTS if line is empty', () => {
editor?.getCoreEditor().setValue('', forceRetokenize);
expect(parser?.getRowParseMode()).toBe(MODE.BETWEEN_REQUESTS);
});

it('should return MODE.BETWEEN_REQUESTS if line is a comment', () => {
editor?.getCoreEditor().setValue('// comment', forceRetokenize);
expect(parser?.getRowParseMode()).toBe(MODE.BETWEEN_REQUESTS);
});

it('should return MODE.REQUEST_START | MODE.REQUEST_END if line is a single line request', () => {
editor?.getCoreEditor().setValue('GET _search', forceRetokenize);
// eslint-disable-next-line no-bitwise
expect(parser?.getRowParseMode()).toBe(MODE.REQUEST_START | MODE.REQUEST_END);
});

it('should return MODE.IN_REQUEST if line is a request with an opening curly brace', () => {
editor?.getCoreEditor().setValue('{', forceRetokenize);
expect(parser?.getRowParseMode()).toBe(MODE.IN_REQUEST);
});

it('should return MODE.MULTI_DOC_CUR_DOC_END | MODE.IN_REQUEST if line is a multi doc request with an opening curly brace', () => {
editor?.getCoreEditor().setValue('GET _msearch\n{}\n{', forceRetokenize);
const lineNumber = editor?.getCoreEditor().getLineCount()! - 1;
expect(parser?.getRowParseMode(lineNumber)).toBe(
// eslint-disable-next-line no-bitwise
MODE.MULTI_DOC_CUR_DOC_END | MODE.IN_REQUEST
);
});

it('should return MODE.MULTI_DOC_CUR_DOC_END | MODE.REQUEST_END if line is a multi doc request with a closing curly brace', () => {
editor?.getCoreEditor().setValue('GET _msearch\n{}\n{"foo": 1}\n', forceRetokenize);
const lineNumber = editor?.getCoreEditor().getLineCount()! - 1;
expect(parser?.getRowParseMode(lineNumber)).toBe(
// eslint-disable-next-line no-bitwise
MODE.MULTI_DOC_CUR_DOC_END | MODE.REQUEST_END
);
});

it('should return MODE.REQUEST_START | MODE.REQUEST_END if line is a request with variables', () => {
editor?.getCoreEditor().setValue('GET /${exampleVariable}', forceRetokenize);
// eslint-disable-next-line no-bitwise
expect(parser?.getRowParseMode()).toBe(MODE.REQUEST_START | MODE.REQUEST_END);
});

it('should return MODE.REQUEST_END | MODE.MULTI_DOC_CUR_DOC_END for a request that ends with a curly closing brace', () => {
editor?.getCoreEditor().setValue('DELETE <foo>/_bar/_baz%{test}', forceRetokenize);
// eslint-disable-next-line no-bitwise
expect(parser?.getRowParseMode()).toBe(MODE.REQUEST_END | MODE.MULTI_DOC_CUR_DOC_END);
});
});
});
8 changes: 6 additions & 2 deletions src/plugins/console/public/lib/row_parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ export default class RowParser {
return MODE.BETWEEN_REQUESTS;
}
const mode = this.editor.getLineState(lineNumber);
const pos = this.editor.getCurrentPosition();
const token = this.editor.getTokenAt(pos);

if (!mode) {
return MODE.BETWEEN_REQUESTS;
Expand Down Expand Up @@ -57,8 +59,10 @@ export default class RowParser {
return MODE.BETWEEN_REQUESTS;
} // empty line or a comment waiting for a new req to start

if (line.indexOf('}', line.length - 1) >= 0) {
// check for a multi doc request (must start a new json doc immediately after this one end.
// If the line ends with a closing curly brace, it's the end of a request,
// and we should also check if the current token is not an url token
if (line.indexOf('}', line.length - 1) >= 0 && token?.type !== 'url.part') {
// check for a multi doc request must start a new json doc immediately after this one end.
lineNumber++;
if (lineNumber < linesCount + 1) {
line = (this.editor.getLineValue(lineNumber) || '').trim();
Expand Down

0 comments on commit cda1e3d

Please sign in to comment.