@@ -21,6 +21,11 @@ describe('tokens_utils', () => {
2121 const result = removeTrailingWhitespaces ( url ) ;
2222 expect ( result ) . toBe ( url ) ;
2323 } ) ;
24+ it ( `doesn't strip if the first character is whitespace` , ( ) => {
25+ const url = ' _search' ;
26+ const result = removeTrailingWhitespaces ( url ) ;
27+ expect ( result ) . toBe ( url ) ;
28+ } ) ;
2429 it ( `removes any text after the first whitespace` , ( ) => {
2530 const url = '_search some_text' ;
2631 const result = removeTrailingWhitespaces ( url ) ;
@@ -31,6 +36,49 @@ describe('tokens_utils', () => {
3136 const result = removeTrailingWhitespaces ( url ) ;
3237 expect ( result ) . toBe ( url ) ;
3338 } ) ;
39+ it ( `doesn't treat a question mark inside quotes as query string start` , ( ) => {
40+ const url = '_search/"?literal" trailing_text' ;
41+ const result = removeTrailingWhitespaces ( url ) ;
42+ expect ( result ) . toBe ( '_search/"?literal"' ) ;
43+ } ) ;
44+ it ( `does not strip unquoted spaces inside query values` , ( ) => {
45+ const url = 'myindex/_search?q=type:organisation AND elastic' ;
46+ const result = removeTrailingWhitespaces ( url ) ;
47+ expect ( result ) . toBe ( url ) ;
48+ } ) ;
49+ it . each ( [
50+ [
51+ 'keeps slashes inside query values' ,
52+ 'myindex/_search?q=http://example.com/path AND elastic' ,
53+ ] ,
54+ [ 'keeps hashes inside query values' , 'myindex/_search?q=tag#1 AND elastic' ] ,
55+ [
56+ 'keeps comment markers inside quoted query values' ,
57+ 'myindex/_search?q="organisation // elastic" AND kibana' ,
58+ ] ,
59+ [
60+ 'uses the first question mark outside quotes as query string start' ,
61+ 'myindex/"?literal"/_search?q=type:organisation AND elastic' ,
62+ ] ,
63+ ] ) ( '%s' , ( _ , url ) => {
64+ const result = removeTrailingWhitespaces ( url ) ;
65+ expect ( result ) . toBe ( url ) ;
66+ } ) ;
67+ it ( `still detects and strips inline comment after unquoted query spaces` , ( ) => {
68+ const url = 'myindex/_search?q=type:organisation AND elastic // filter orgs' ;
69+ const result = removeTrailingWhitespaces ( url ) ;
70+ expect ( result ) . toBe ( 'myindex/_search?q=type:organisation AND elastic' ) ;
71+ } ) ;
72+ it ( `still detects inline comment after mixed whitespace in query values` , ( ) => {
73+ const url = 'myindex/_search?q=type:organisation AND elastic \t// filter orgs' ;
74+ const result = removeTrailingWhitespaces ( url ) ;
75+ expect ( result ) . toBe ( 'myindex/_search?q=type:organisation AND elastic' ) ;
76+ } ) ;
77+ it ( `still detects and strips hash comment after unquoted query spaces` , ( ) => {
78+ const url = 'myindex/_search?q=type:organisation AND elastic # filter orgs' ;
79+ const result = removeTrailingWhitespaces ( url ) ;
80+ expect ( result ) . toBe ( 'myindex/_search?q=type:organisation AND elastic' ) ;
81+ } ) ;
3482 } ) ;
3583
3684 describe ( 'parseLine' , ( ) => {
@@ -48,6 +96,24 @@ describe('tokens_utils', () => {
4896 expect ( urlPathTokens ) . toEqual ( [ '_search' ] ) ;
4997 expect ( urlParamsTokens [ 0 ] ) . toEqual ( [ 'query' , '"test1 test2 test3"' ] ) ;
5098 } ) ;
99+ it ( 'preserves unquoted spaces inside query values' , ( ) => {
100+ const { method, url, urlPathTokens, urlParamsTokens } = parseLine (
101+ 'GET myindex/_search?q=type:organisation AND elastic'
102+ ) ;
103+ expect ( method ) . toBe ( 'GET' ) ;
104+ expect ( url ) . toBe ( 'myindex/_search?q=type:organisation AND elastic' ) ;
105+ expect ( urlPathTokens ) . toEqual ( [ 'myindex' , '_search' ] ) ;
106+ expect ( urlParamsTokens [ 0 ] ) . toEqual ( [ 'q' , 'type:organisation AND elastic' ] ) ;
107+ } ) ;
108+ it ( 'uses the first question mark outside quotes to parse url params' , ( ) => {
109+ const { method, url, urlPathTokens, urlParamsTokens } = parseLine (
110+ 'GET myindex/"?literal"/_search?q=type:organisation AND elastic'
111+ ) ;
112+ expect ( method ) . toBe ( 'GET' ) ;
113+ expect ( url ) . toBe ( 'myindex/"?literal"/_search?q=type:organisation AND elastic' ) ;
114+ expect ( urlPathTokens ) . toEqual ( [ 'myindex' , '"?literal"' , '_search' ] ) ;
115+ expect ( urlParamsTokens [ 0 ] ) . toEqual ( [ 'q' , 'type:organisation AND elastic' ] ) ;
116+ } ) ;
51117 it ( 'works with multiple whitespaces' , ( ) => {
52118 const { method, url, urlPathTokens, urlParamsTokens } = parseLine (
53119 ' GET _search?query="test1 test2 test3" // comment'
@@ -197,5 +263,12 @@ describe('tokens_utils', () => {
197263 const result = parseUrl ( url ) ;
198264 expect ( result . urlPathTokens ) . toEqual ( [ '_search' , 'test' ] ) ;
199265 } ) ;
266+
267+ it ( 'uses the first question mark outside quotes for url params' , ( ) => {
268+ const url = 'myindex/"?literal"/_search?q=type:organisation AND elastic' ;
269+ const result = parseUrl ( url ) ;
270+ expect ( result . urlPathTokens ) . toEqual ( [ 'myindex' , '"?literal"' , '_search' ] ) ;
271+ expect ( result . urlParamsTokens [ 0 ] ) . toEqual ( [ 'q' , 'type:organisation AND elastic' ] ) ;
272+ } ) ;
200273 } ) ;
201274} ) ;
0 commit comments