-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Carsten König
committed
Sep 13, 2024
1 parent
2fae910
commit 0e81ff8
Showing
10 changed files
with
175 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
const kibanaBodyRegex = /^(GET|POST|PUT|PATCH|HEAD|OPTIONS|DELETE)(?:\s(.*)?)?/ | ||
|
||
type KibanaRequest = { | ||
method: string | null, | ||
path: string | null, | ||
body: string | null | ||
} | ||
|
||
export const parseKibana = (input: string): KibanaRequest => { | ||
const lines = input.split('\n') | ||
const possibleRequestLine = lines[0] | ||
const [, ...rest] = lines | ||
|
||
const matches = possibleRequestLine.match(kibanaBodyRegex) | ||
|
||
const matchedMethod = matches?.[1]?.trim() | ||
if (matchedMethod && matchedMethod.length > 0) { | ||
const matchedpath = matches?.[2]?.trim() | ||
const path = matchedpath || '' | ||
const body = rest.join('\n') | ||
|
||
return { method: matchedMethod, path, body } | ||
} else { | ||
return { method: null, path: null, body: input } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import { describe, it, expect } from 'vitest' | ||
import { parseKibana } from '../../../src/helpers/parseKibana' | ||
|
||
describe.concurrent('parseKibana.ts', () => { | ||
describe.concurrent('plain requests', () => { | ||
it('accepts ""', () => { | ||
const input = '' | ||
const request = parseKibana(input) | ||
expect(request.method).toEqual(null) | ||
expect(request.path).toEqual(null) | ||
expect(request.body).toEqual(input) | ||
}) | ||
|
||
it('accepts "{"query": "*"}"', () => { | ||
const input = '{"query": "*"}' | ||
const request = parseKibana(input) | ||
expect(request.method).toEqual(null) | ||
expect(request.path).toEqual(null) | ||
expect(request.body).toEqual(input) | ||
}) | ||
|
||
it('accepts "{\n\t"query": "*"\n}\n"', () => { | ||
const input = '{\n\t"query": "*"\n}\n' | ||
const request = parseKibana(input) | ||
expect(request.method).toEqual(null) | ||
expect(request.path).toEqual(null) | ||
expect(request.body).toEqual(input) | ||
}) | ||
}) | ||
|
||
describe.concurrent('kibana single line request', () => { | ||
it('accepts "GET"', () => { | ||
const input = 'GET' | ||
const request = parseKibana(input) | ||
expect(request.method).toEqual('GET') | ||
expect(request.path).toEqual('') | ||
expect(request.body).toEqual('') | ||
}) | ||
|
||
it('accepts "GET "', () => { | ||
const input = 'GET ' | ||
const request = parseKibana(input) | ||
expect(request.method).toEqual('GET') | ||
expect(request.path).toEqual('') | ||
expect(request.body).toEqual('') | ||
}) | ||
|
||
it('accepts "GET "', () => { | ||
const input = 'GET ' | ||
const request = parseKibana(input) | ||
expect(request.method).toEqual('GET') | ||
expect(request.path).toEqual('') | ||
expect(request.body).toEqual('') | ||
}) | ||
|
||
it('accepts "GET /"', () => { | ||
const input = 'GET /' | ||
const request = parseKibana(input) | ||
expect(request.method).toEqual('GET') | ||
expect(request.path).toEqual('/') | ||
expect(request.body).toEqual('') | ||
}) | ||
|
||
it('accepts "GET _cat/foo"', () => { | ||
const input = 'GET _cat/foo' | ||
const request = parseKibana(input) | ||
expect(request.method).toEqual('GET') | ||
expect(request.path).toEqual('_cat/foo') | ||
expect(request.body).toEqual('') | ||
}) | ||
|
||
it('accepts "POST _search?query=foobar"', () => { | ||
const input = 'POST _search?query=foobar' | ||
const request = parseKibana(input) | ||
expect(request.method).toEqual('POST') | ||
expect(request.path).toEqual('_search?query=foobar') | ||
expect(request.body).toEqual('') | ||
}) | ||
}) | ||
|
||
describe.concurrent('kibana multiline request', () => { | ||
it('accepts "POST /_search\n{\n\t"query": {\n\t\t"query_string": {\n\t\t\t"query": "*"\n\t\t}\n\t}\n}"', () => { | ||
const input = 'POST /_search\n{\n\t"query": {\n\t\t"query_string": {\n\t\t\t"query": "*"\n\t\t}\n\t}\n}' | ||
const request = parseKibana(input) | ||
expect(request.method).toEqual('POST') | ||
expect(request.path).toEqual('/_search') | ||
expect(request.body).toEqual('{\n\t"query": {\n\t\t"query_string": {\n\t\t\t"query": "*"\n\t\t}\n\t}\n}') | ||
}) | ||
}) | ||
}) |