Skip to content

Commit 46e99ec

Browse files
committed
[API] Escapes spaces with %20 for URL encoded strings
1 parent d3c3d0e commit 46e99ec

File tree

2 files changed

+96
-1
lines changed

2 files changed

+96
-1
lines changed

elasticsearch-api/lib/elasticsearch/api/utils.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
# KIND, either express or implied. See the License for the
1515
# specific language governing permissions and limitations
1616
# under the License.
17+
require 'erb'
1718

1819
module Elasticsearch
1920
module API
@@ -29,7 +30,7 @@ module Utils
2930
# @api private
3031
def __escape(string)
3132
return string if string == '*'
32-
defined?(EscapeUtils) ? EscapeUtils.escape_url(string.to_s) : CGI.escape(string.to_s)
33+
ERB::Util.url_encode(string.to_s)
3334
end
3435

3536
# Create a "list" of values from arguments, ignoring nil values and encoding special characters.
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# Licensed to Elasticsearch B.V. under one or more contributor
2+
# license agreements. See the NOTICE file distributed with
3+
# this work for additional information regarding copyright
4+
# ownership. Elasticsearch B.V. licenses this file to you under
5+
# the Apache License, Version 2.0 (the "License"); you may
6+
# not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
ELASTICSEARCH_URL = ENV['TEST_ES_SERVER'] || "http://localhost:#{(ENV['PORT'] || 9200)}"
18+
raise URI::InvalidURIError unless ELASTICSEARCH_URL =~ /\A#{URI::DEFAULT_PARSER.make_regexp}\z/
19+
20+
require 'spec_helper'
21+
22+
context 'Elasticsearch client' do
23+
let(:client) do
24+
Elasticsearch::Client.new(host: ELASTICSEARCH_URL, user: 'elastic', password: 'changeme')
25+
end
26+
let(:index) { 'tvs' }
27+
28+
after do
29+
client.indices.delete(index: index)
30+
end
31+
32+
context 'escaping spaces in ids' do
33+
it 'escapes spaces for id when using index' do
34+
response = client.index(index: index, id: 'a test 1', body: { name: 'A test 1' }, refresh: true)
35+
expect(response['_id']).to eq 'a test 1'
36+
37+
response = client.search(index: index)
38+
expect(response['hits']['hits'].first['_id']).to eq 'a test 1'
39+
40+
# Raises exception, _id is unrecognized
41+
expect do
42+
client.index(index: index, _id: 'a test 2', body: { name: 'A test 2' })
43+
end.to raise_exception ArgumentError
44+
45+
# Raises exception, id is a query parameter
46+
expect do
47+
client.index(index: index, body: { name: 'A test 3', _id: 'a test 3' })
48+
end.to raise_exception Elasticsearch::Transport::Transport::Errors::BadRequest
49+
end
50+
51+
it 'escapes spaces for id when using create' do
52+
# Works with create
53+
response = client.create(index: index, id: 'a test 4', body: { name: 'A test 4' })
54+
expect(response['_id']).to eq 'a test 4'
55+
end
56+
57+
it 'escapes spaces for id when using bulk' do
58+
body = [
59+
{ create: { _index: index, _id: 'a test 5', data: { name: 'A test 5' } } }
60+
]
61+
expect(client.bulk(body: body, refresh: true))
62+
63+
response = client.search(index: index)
64+
expect(
65+
response['hits']['hits'].select { |a| a['_id'] == 'a test 5' }.size
66+
).to eq 1
67+
end
68+
end
69+
70+
context 'it doesnae escape plus signs in id' do
71+
it 'escapes spaces for id when using index' do
72+
response = client.index(index: index, id: 'a+test+1', body: { name: 'A test 1' })
73+
expect(response['_id']).to eq 'a+test+1'
74+
end
75+
76+
it 'escapes spaces for id when using create' do
77+
# Works with create
78+
response = client.create(index: index, id: 'a+test+2', body: { name: 'A test 2' })
79+
expect(response['_id']).to eq 'a+test+2'
80+
end
81+
82+
it 'escapes spaces for id when using bulk' do
83+
body = [
84+
{ create: { _index: index, _id: 'a+test+3', data: { name: 'A test 3' } } }
85+
]
86+
expect(client.bulk(body: body, refresh: true))
87+
88+
response = client.search(index: index)
89+
expect(
90+
response['hits']['hits'].select { |a| a['_id'] == 'a+test+3' }.size
91+
).to eq 1
92+
end
93+
end
94+
end

0 commit comments

Comments
 (0)