From fa9640d60fa8af8914d6f388d548925a0cbff6dd Mon Sep 17 00:00:00 2001 From: Joao Duarte Date: Mon, 30 May 2016 15:34:48 +0100 Subject: [PATCH 1/2] refactored tests to ensure webserver is up --- spec/inputs/http_spec.rb | 309 ++++++++++++++++++--------------------- 1 file changed, 145 insertions(+), 164 deletions(-) diff --git a/spec/inputs/http_spec.rb b/spec/inputs/http_spec.rb index 7f80e9c8..b2f79d02 100644 --- a/spec/inputs/http_spec.rb +++ b/spec/inputs/http_spec.rb @@ -16,149 +16,179 @@ let(:queue) { Queue.new } let(:port) { rand(5000) + 1025 } - after :each do - subject.stop - end - it_behaves_like "an interruptible input plugin" do let(:config) { { "port" => port } } end - describe "#run" do + after :each do + subject.stop + end + + describe "request handling" do subject { LogStash::Inputs::Http.new } before :each do subject.register - Thread.new { subject.run(queue) } + t = Thread.new { subject.run(queue) } + sleep 0.01 until subject.instance_variable_get(:@server).running == 0 end + it "should include remote host in \"host\" property" do agent.post!("http://localhost:8080/meh.json", - :headers => { "content-type" => "text/plain" }, - :body => "hello") + :headers => { "content-type" => "text/plain" }, + :body => "hello") event = queue.pop expect(event["host"]).to eq("127.0.0.1") end - end - context "with default codec" do - subject { LogStash::Inputs::Http.new("port" => port) } - context "when receiving a text/plain request" do - it "should process the request normally" do - subject.register - Thread.new { subject.run(queue) } - agent.post!("http://localhost/#{port}/meh.json", - :headers => { "content-type" => "text/plain" }, - :body => "hello") - event = queue.pop - expect(event["message"]).to eq("hello") - end - end - context "when receiving a deflate compressed text/plain request" do - it "should process the request normally" do - subject.register - Thread.new { subject.run(queue) } - agent.post!("http://localhost/#{port}/meh.json", - :headers => { "content-type" => "text/plain", "content-encoding" => "deflate" }, - :body => Zlib::Deflate.deflate("hello")) - event = queue.pop - expect(event["message"]).to eq("hello") - end - end - context "when receiving a deflate text/plain request that cannot be decompressed" do - let!(:response) do - subject.register - Thread.new { subject.run(queue) } - agent.post!("http://localhost/#{port}/meh.json", - :headers => { "content-type" => "text/plain", "content-encoding" => "deflate" }, - :body => "hello") - end - it "should respond with 400" do - expect(response.status).to eq(400) - end - it "should respond with a decompression error" do - expect(response.read_body).to eq("Failed to decompress body") + context "with default codec" do + subject { LogStash::Inputs::Http.new("port" => port) } + context "when receiving a text/plain request" do + it "should process the request normally" do + agent.post!("http://localhost/#{port}/meh.json", + :headers => { "content-type" => "text/plain" }, + :body => "hello") + event = queue.pop + expect(event["message"]).to eq("hello") + end + end + context "when receiving a deflate compressed text/plain request" do + it "should process the request normally" do + agent.post!("http://localhost/#{port}/meh.json", + :headers => { "content-type" => "text/plain", "content-encoding" => "deflate" }, + :body => Zlib::Deflate.deflate("hello")) + event = queue.pop + expect(event["message"]).to eq("hello") + end + end + context "when receiving a deflate text/plain request that cannot be decompressed" do + it "should respond with 400" do + response = agent.post!("http://localhost/#{port}/meh.json", + :headers => { "content-type" => "text/plain", "content-encoding" => "deflate" }, + :body => "hello") + expect(response.status).to eq(400) + end + it "should respond with a decompression error" do + response = agent.post!("http://localhost/#{port}/meh.json", + :headers => { "content-type" => "text/plain", "content-encoding" => "deflate" }, + :body => "hello") + expect(response.read_body).to eq("Failed to decompress body") + end + end + context "when receiving a gzip compressed text/plain request" do + it "should process the request normally" do + z = StringIO.new "" + w = Zlib::GzipWriter.new z + w.write("hello") + w.finish + agent.post!("http://localhost/#{port}/meh.json", + :headers => { "content-type" => "text/plain", "content-encoding" => "gzip" }, + :body => z.string) + event = queue.pop + expect(event["message"]).to eq("hello") + end + end + context "when receiving a gzip text/plain request that cannot be decompressed" do + let(:response) do + agent.post!("http://localhost/#{port}/meh.json", + :headers => { "content-type" => "text/plain", "content-encoding" => "gzip" }, + :body => "hello") + end + it "should respond with 400" do + expect(response.status).to eq(400) + end + it "should respond with a decompression error" do + expect(response.read_body).to eq("Failed to decompress body") + end + end + context "when receiving an application/json request" do + it "should parse the json body" do + agent.post!("http://localhost/#{port}/meh.json", + :headers => { "content-type" => "application/json" }, + :body => { "message_body" => "Hello" }.to_json) + event = queue.pop + expect(event["message_body"]).to eq("Hello") + end end end - context "when receiving a gzip compressed text/plain request" do - it "should process the request normally" do - subject.register - Thread.new { subject.run(queue) } - z = StringIO.new "" - w = Zlib::GzipWriter.new z - w.write("hello") - w.finish - agent.post!("http://localhost/#{port}/meh.json", - :headers => { "content-type" => "text/plain", "content-encoding" => "gzip" }, - :body => z.string) + context "with json codec" do + subject { LogStash::Inputs::Http.new("port" => port, "codec" => "json") } + it "should parse the json body" do + agent.post!("http://localhost/#{port}/meh.json", :body => { "message" => "Hello" }.to_json) event = queue.pop - expect(event["message"]).to eq("hello") - end - end - context "when receiving a gzip text/plain request that cannot be decompressed" do - let!(:response) do - subject.register - Thread.new { subject.run(queue) } - agent.post!("http://localhost/#{port}/meh.json", - :headers => { "content-type" => "text/plain", "content-encoding" => "gzip" }, - :body => "hello") - end - it "should respond with 400" do - expect(response.status).to eq(400) - end - it "should respond with a decompression error" do - expect(response.read_body).to eq("Failed to decompress body") + expect(event["message"]).to eq("Hello") end end - context "when receiving an application/json request" do - it "should parse the json body" do - subject.register - Thread.new { subject.run(queue) } + + context "when using a custom codec mapping" do + subject { LogStash::Inputs::Http.new("port" => port, + "additional_codecs" => { "application/json" => "plain" }) } + it "should decode the message accordingly" do + body = { "message" => "Hello" }.to_json agent.post!("http://localhost/#{port}/meh.json", :headers => { "content-type" => "application/json" }, - :body => { "message_body" => "Hello" }.to_json) + :body => body) event = queue.pop - expect(event["message_body"]).to eq("Hello") + expect(event["message"]).to eq(body) end end - end - - context "with json codec" do - subject { LogStash::Inputs::Http.new("port" => port, "codec" => "json") } - it "should parse the json body" do - subject.register - Thread.new { subject.run(queue) } - agent.post!("http://localhost/#{port}/meh.json", :body => { "message" => "Hello" }.to_json) - event = queue.pop - expect(event["message"]).to eq("Hello") - end - end - - context "when using a custom codec mapping" do - subject { LogStash::Inputs::Http.new("port" => port, - "additional_codecs" => { "application/json" => "plain" }) } - it "should decode the message accordingly" do - body = { "message" => "Hello" }.to_json - subject.register - Thread.new { subject.run(queue) } - agent.post!("http://localhost/#{port}/meh.json", - :headers => { "content-type" => "application/json" }, - :body => body) - event = queue.pop - expect(event["message"]).to eq(body) - end - end - context "when using custom headers" do - let(:custom_headers) { { 'access-control-allow-origin' => '*' } } - subject { LogStash::Inputs::Http.new("port" => port, "response_headers" => custom_headers) } + context "when using custom headers" do + let(:custom_headers) { { 'access-control-allow-origin' => '*' } } + subject { LogStash::Inputs::Http.new("port" => port, "response_headers" => custom_headers) } - describe "the response" do - it "should include the custom headers" do - subject.register - Thread.new { subject.run(queue) } - response = agent.post!("http://localhost/#{port}/meh", :body => "hello") - expect(response.headers.to_hash).to include(custom_headers) + describe "the response" do + it "should include the custom headers" do + response = agent.post!("http://localhost/#{port}/meh", :body => "hello") + expect(response.headers.to_hash).to include(custom_headers) + end + end + end + describe "basic auth" do + user = "test"; password = "pwd" + subject { LogStash::Inputs::Http.new("port" => port, "user" => user, "password" => password) } + let(:auth_token) { Base64.strict_encode64("#{user}:#{password}") } + context "when client doesn't present auth token" do + let!(:response) { agent.post!("http://localhost/#{port}/meh", :body => "hi") } + it "should respond with 401" do + expect(response.status).to eq(401) + end + it "should not generate an event" do + expect(queue).to be_empty + end + end + context "when client presents incorrect auth token" do + let!(:response) do + agent.post!("http://localhost/#{port}/meh", + :headers => { + "content-type" => "text/plain", + "authorization" => "Basic meh" + }, + :body => "hi") + end + it "should respond with 401" do + expect(response.status).to eq(401) + end + it "should not generate an event" do + expect(queue).to be_empty + end + end + context "when client presents correct auth token" do + let!(:response) do + agent.post!("http://localhost/#{port}/meh", + :headers => { + "content-type" => "text/plain", + "authorization" => "Basic #{auth_token}" + }, :body => "hi") + end + it "should respond with 200" do + expect(response.status).to eq(200) + end + it "should generate an event" do + expect(queue).to_not be_empty + end end end + end context "with :ssl => false" do @@ -184,53 +214,4 @@ end end end - describe "basic auth" do - user = "test"; password = "pwd" - subject { LogStash::Inputs::Http.new("port" => port, "user" => user, "password" => password) } - let(:auth_token) { Base64.strict_encode64("#{user}:#{password}") } - before :each do - subject.register - Thread.new { subject.run(queue) } - end - context "when client doesn't present auth token" do - let!(:response) { agent.post!("http://localhost/#{port}/meh", :body => "hi") } - it "should respond with 401" do - expect(response.status).to eq(401) - end - it "should not generate an event" do - expect(queue).to be_empty - end - end - context "when client presents incorrect auth token" do - let!(:response) do - agent.post!("http://localhost/#{port}/meh", - :headers => { - "content-type" => "text/plain", - "authorization" => "Basic meh" - }, - :body => "hi") - end - it "should respond with 401" do - expect(response.status).to eq(401) - end - it "should not generate an event" do - expect(queue).to be_empty - end - end - context "when client presents correct auth token" do - let!(:response) do - agent.post!("http://localhost/#{port}/meh", - :headers => { - "content-type" => "text/plain", - "authorization" => "Basic #{auth_token}" - }, :body => "hi") - end - it "should respond with 200" do - expect(response.status).to eq(200) - end - it "should generate an event" do - expect(queue).to_not be_empty - end - end - end end From 97cc71af01c05ef9c9ad8b4ea4ef9da0ad6ac6d9 Mon Sep 17 00:00:00 2001 From: Joao Duarte Date: Thu, 2 Jun 2016 10:59:36 +0100 Subject: [PATCH 2/2] bump to 2.2.3 --- CHANGELOG.md | 3 +++ logstash-input-http.gemspec | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a6c1f492..edd620a5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +# 2.2.3 + - fix hanging tests + - added some meta documents to the repository like contributing guide and github templates # 2.2.2 - Depend on logstash-core-plugin-api instead of logstash-core, removing the need to mass update plugins on major releases of logstash # 2.2.1 diff --git a/logstash-input-http.gemspec b/logstash-input-http.gemspec index d77010cb..6e223cfb 100644 --- a/logstash-input-http.gemspec +++ b/logstash-input-http.gemspec @@ -1,6 +1,6 @@ Gem::Specification.new do |s| s.name = 'logstash-input-http' - s.version = '2.2.2' + s.version = '2.2.3' s.licenses = ['Apache License (2.0)'] s.summary = "Logstash Input plugin that receives HTTP requests" 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"