Skip to content

Commit

Permalink
Fix error in instantiating bad response code exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewvc committed Aug 16, 2017
1 parent 76a0f9f commit 5b1e53d
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 8 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,6 @@
## 8.0.2
- Fix bug where logging errors for bad response codes would raise an unhandled exception

## 8.0.1
- Fix some documentation issues

Expand Down
Expand Up @@ -66,7 +66,7 @@ def perform_request(url, method, path, params={}, body=nil)

request_uri = format_url(url, path)

resp = @manticore.send(method.downcase, request_uri, params)
resp = @manticore.send(method.downcase, request_uri.to_s, params)

# Manticore returns lazy responses by default
# We want to block for our usage, this will wait for the repsonse
Expand Down Expand Up @@ -106,7 +106,7 @@ def format_url(url, path_and_query=nil)

request_uri.path = "#{request_uri.path}/#{parsed_path_and_query.path}".gsub(/\/{2,}/, "/")

request_uri.to_s
request_uri
end

def close
Expand Down
2 changes: 1 addition & 1 deletion logstash-output-elasticsearch.gemspec
@@ -1,6 +1,6 @@
Gem::Specification.new do |s|
s.name = 'logstash-output-elasticsearch'
s.version = '8.0.1'
s.version = '8.0.2'
s.licenses = ['apache-2.0']
s.summary = "Logstash Output to Elasticsearch"
s.description = "This gem is a Logstash plugin required to be installed on top of the Logstash core pipeline using $LS_HOME/bin/logstash-plugin install gemname. This gem is not a stand-alone program"
Expand Down
Expand Up @@ -44,19 +44,44 @@
end
end

describe "bad response codes" do
let(:uri) { ::LogStash::Util::SafeURI.new("http://localhost:9200") }

it "should raise a bad response code error" do
resp = double("response")
allow(resp).to receive(:call)
allow(resp).to receive(:code).and_return(500)
allow(resp).to receive(:body).and_return("a body")

expect(subject.manticore).to receive(:get).
with(uri.to_s + "/", anything).
and_return(resp)

uri_with_path = uri.clone
uri_with_path.path = "/"

expect(::LogStash::Outputs::ElasticSearch::HttpClient::Pool::BadResponseCodeError).to receive(:new).
with(resp.code, uri_with_path, nil, resp.body).and_call_original

expect do
subject.perform_request(uri, :get, "/")
end.to raise_error(::LogStash::Outputs::ElasticSearch::HttpClient::Pool::BadResponseCodeError)
end
end

describe "format_url" do
let(:url) { ::LogStash::Util::SafeURI.new("http://localhost:9200/path/") }
let(:path) { "_bulk" }
subject { described_class.new(double("logger"), {}) }

it "should add the path argument to the uri's path" do
expect(java.net.URI.new(subject.format_url(url, path)).path).to eq("/path/_bulk")
expect(subject.format_url(url, path).path).to eq("/path/_bulk")
end

context "when uri contains query parameters" do
let(:query_params) { "query=value&key=value2" }
let(:url) { ::LogStash::Util::SafeURI.new("http://localhost:9200/path/?#{query_params}") }
let(:formatted) { java.net.URI.new(subject.format_url(url, path))}
let(:formatted) { subject.format_url(url, path)}

it "should retain query_params after format" do
expect(formatted.query).to eq(query_params)
Expand All @@ -73,7 +98,7 @@

context "when the path contains query parameters" do
let(:path) { "/special_bulk?pathParam=1"}
let(:formatted) { java.net.URI.new(subject.format_url(url, path)) }
let(:formatted) { subject.format_url(url, path) }

it "should add the path correctly" do
expect(formatted.path).to eq("#{url.path}special_bulk")
Expand All @@ -86,10 +111,10 @@

context "when uri contains credentials" do
let(:url) { ::LogStash::Util::SafeURI.new("http://myuser:mypass@localhost:9200") }
let(:formatted) { java.net.URI.new(subject.format_url(url, path)) }
let(:formatted) { subject.format_url(url, path) }

it "should remove credentials after format" do
expect(formatted.user_info).to be_nil
expect(formatted.userinfo).to be_nil
end
end
end
Expand Down

0 comments on commit 5b1e53d

Please sign in to comment.