Commit 5138cb69 authored by Wandenberg Peixoto's avatar Wandenberg Peixoto

add a matcher to check status and content length of response

parent 70a38059
module CustomHttpMatchers
class BeHttpStatus
def initialize(expected)
@expected = expected
@should_has_content = nil
end
def matches?(target)
@target = target
ret = @target.response_header.status.eql?(@expected)
ret = @should_has_content ? has_content? : !has_content? unless (@should_has_content.nil? || !ret)
ret
end
alias == matches?
def without_body
@should_has_content = false
self
end
def with_body
@should_has_content = true
self
end
def failure_message_for_should
"expected that the #{@target.req.method} to #{@target.req.uri} to #{description}"
end
def failure_message_for_should_not
"expected that the #{@target.req.method} to #{@target.req.uri} not to #{description}"
end
def description
returned_values = " but returned with status #{@target.response_header.status} and content_length equals to #{@target.response_header.content_length}"
about_content = " and #{@should_has_content ? "with body" : "without body"}" unless @should_has_content.nil?
"be returned with status #{@expected}#{about_content}#{returned_values}"
end
private
def has_content?
@target.response_header.content_length > 0
end
end
def be_http_status(expected)
BeHttpStatus.new(expected)
end
end
......@@ -22,7 +22,7 @@ describe "Broadcast Properties" do
pub.callback do
sub_1 = EventMachine::HttpRequest.new(nginx_address + '/sub/' + channel.to_s + '/' + channel_broad_fail).get :head => headers
sub_1.callback do |chunk|
sub_1.response_header.status.should eql(403)
sub_1.should be_http_status(403).without_body
sub_2 = EventMachine::HttpRequest.new(nginx_address + '/sub/' + channel.to_s + '/' + channel_broad).get :head => headers
sub_2.stream do |chunk2|
......@@ -48,7 +48,7 @@ describe "Broadcast Properties" do
pub.callback do
sub_1 = EventMachine::HttpRequest.new(nginx_address + '/sub/' + channel.to_s + '/' + channel_broad1 + '/' + channel_broad2 + '/' + channel_broad3).get :head => headers
sub_1.callback do |chunk|
sub_1.response_header.status.should eql(403)
sub_1.should be_http_status(403).without_body
sub_2 = EventMachine::HttpRequest.new(nginx_address + '/sub/' + channel.to_s + '/' + channel_broad1 + '/' + channel_broad2).get :head => headers
sub_2.stream do
EventMachine.stop
......
This diff is collapsed.
This diff is collapsed.
......@@ -40,8 +40,7 @@ describe "Measure Memory" do
EventMachine.run do
pub_2 = EventMachine::HttpRequest.new(nginx_address + '/channels-stats').get
pub_2.callback do
pub_2.response_header.status.should eql(200)
pub_2.response_header.content_length.should_not eql(0)
pub_2.should be_http_status(200).with_body
resp = JSON.parse(pub_2.response)
expected_message = shared_size / (message_estimate_size + body.size)
......@@ -73,8 +72,7 @@ describe "Measure Memory" do
EventMachine.run do
pub_2 = EventMachine::HttpRequest.new(nginx_address + '/channels-stats').get
pub_2.callback do
pub_2.response_header.status.should eql(200)
pub_2.response_header.content_length.should_not eql(0)
pub_2.should be_http_status(200).with_body
resp = JSON.parse(pub_2.response)
expected_channel = (shared_size - ((body.size + message_estimate_size) * resp["published_messages"].to_i)) / (channel_estimate_size + 4) # 4 channel id size
......@@ -93,8 +91,7 @@ describe "Measure Memory" do
subscriber_in_loop(1000, headers) do
pub_2 = EventMachine::HttpRequest.new(nginx_address + '/channels-stats').get :head => headers
pub_2.callback do
pub_2.response_header.status.should eql(200)
pub_2.response_header.content_length.should_not eql(0)
pub_2.should be_http_status(200).with_body
resp = JSON.parse(pub_2.response)
expected_subscriber = (shared_size - ((channel_estimate_size + 4) * resp["channels"].to_i)) / subscriber_estimate_size # 4 channel id size
......
......@@ -41,8 +41,7 @@ describe "Send Signals" do
# check statistics
pub_1 = EventMachine::HttpRequest.new(nginx_address + '/channels-stats').get :head => headers
pub_1.callback do
pub_1.response_header.status.should eql(200)
pub_1.response_header.content_length.should_not eql(0)
pub_1.should be_http_status(200).with_body
resp_1 = JSON.parse(pub_1.response)
resp_1.has_key?("channels").should be_true
resp_1["channels"].to_i.should eql(1)
......@@ -127,8 +126,7 @@ describe "Send Signals" do
# check statistics
pub_1 = EventMachine::HttpRequest.new(nginx_address + '/channels-stats').get :head => headers
pub_1.callback do
pub_1.response_header.status.should eql(200)
pub_1.response_header.content_length.should_not eql(0)
pub_1.should be_http_status(200).with_body
resp_1 = JSON.parse(pub_1.response)
resp_1.has_key?("channels").should be_true
resp_1["channels"].to_i.should eql(1)
......@@ -144,8 +142,7 @@ describe "Send Signals" do
pub_2 = EventMachine::HttpRequest.new(nginx_address + '/channels-stats').get :head => headers
pub_2.callback do
pub_2.response_header.status.should eql(200)
pub_2.response_header.content_length.should_not eql(0)
pub_2.should be_http_status(200).with_body
resp_2 = JSON.parse(pub_2.response)
resp_2.has_key?("channels").should be_true
resp_2["channels"].to_i.should eql(1)
......
......@@ -10,7 +10,7 @@ describe "Publisher Channel id collision" do
EventMachine.run do
pub = EventMachine::HttpRequest.new(nginx_address + '/pub?id=' + channel).post :body => 'x'
pub.callback do
pub.response_header.status.should eql(200)
pub.should be_http_status(200)
EventMachine.stop
end
end
......@@ -20,7 +20,7 @@ describe "Publisher Channel id collision" do
EventMachine.run do
pub = EventMachine::HttpRequest.new(nginx_address + '/channels-stats?id=' + channel).get :timeout => 30
pub.callback do
pub.response_header.status.should eql(200)
pub.should be_http_status(200)
EventMachine.stop
end
end
......
This diff is collapsed.
......@@ -7,18 +7,20 @@ require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE'])
Bundler.require(:default, :test) if defined?(Bundler)
require 'nginx_configuration'
require 'custom_http_matchers'
RSpec.configure do |config|
config.after(:each) do
NginxTestHelper::Config.delete_config_and_log_files(config_id) if has_passed?
end
config.order = "random"
config.include(CustomHttpMatchers)
end
def publish_message_inline(channel, headers, body, &block)
pub = EventMachine::HttpRequest.new(nginx_address + '/pub?id=' + channel.to_s).post :head => headers, :body => body
pub.callback do
fail("Request was not accepted") if pub.response_header.status != 200
pub.should be_http_status(200)
block.call unless block.nil?
end
pub
......@@ -27,7 +29,7 @@ end
def publish_message(channel, headers, body)
EventMachine.run do
pub = publish_message_inline(channel, headers, body) do
pub.response_header.content_length.should_not eql(0)
pub.should be_http_status(200).with_body
response = JSON.parse(pub.response)
response["channel"].to_s.should eql(channel)
EventMachine.stop
......
......@@ -33,8 +33,7 @@ describe "Comunication Properties" do
EventMachine.run do
sub_1 = EventMachine::HttpRequest.new(nginx_address + '/sub/' + channel.to_s).get :head => headers
sub_1.callback do |chunk|
sub_1.response_header.status.should eql(403)
sub_1.response_header.content_length.should eql(0)
sub_1.should be_http_status(403).without_body
sub_1.response_header['X_NGINX_PUSHSTREAM_EXPLAIN'].should eql("Subscriber could not create channels.")
pub = EventMachine::HttpRequest.new(nginx_address + '/pub?id=' + channel.to_s ).post :head => headers, :body => body
......
......@@ -108,7 +108,7 @@ describe "Subscriber Properties" do
sub_1 = EventMachine::HttpRequest.new(nginx_address + '/sub/' + channel_2.to_s + '/' + channel_1.to_s).get :head => headers
sub_1.callback do
sub_1.response_header.status.should eql(200)
sub_1.should be_http_status(200)
sub_1.response_header['LAST_MODIFIED'].to_s.should_not eql("")
sub_1.response_header['ETAG'].to_s.should_not eql("")
sub_1.response.should eql("#{body}_2\r\n#{body}_1\r\n")
......@@ -116,7 +116,7 @@ describe "Subscriber Properties" do
sent_headers = headers.merge({'If-Modified-Since' => sub_1.response_header['LAST_MODIFIED'], 'If-None-Match' => sub_1.response_header['ETAG']})
sub_2 = EventMachine::HttpRequest.new(nginx_address + '/sub/' + channel_2.to_s + '/' + channel_1.to_s).get :head => sent_headers
sub_2.callback do
sub_2.response_header.status.should eql(200)
sub_2.should be_http_status(200)
sub_2.response_header['LAST_MODIFIED'].to_s.should_not eql(sub_1.response_header['LAST_MODIFIED'])
sub_2.response_header['ETAG'].to_s.should eql("0")
sub_2.response.should eql("#{body}1_1\r\n")
......@@ -124,7 +124,7 @@ describe "Subscriber Properties" do
sent_headers = headers.merge({'If-Modified-Since' => sub_2.response_header['LAST_MODIFIED'], 'If-None-Match' => sub_2.response_header['ETAG']})
sub_3 = EventMachine::HttpRequest.new(nginx_address + '/sub/' + channel_2.to_s + '/' + channel_1.to_s).get :head => sent_headers
sub_3.callback do
sub_3.response_header.status.should eql(200)
sub_3.should be_http_status(200)
sub_3.response_header['LAST_MODIFIED'].to_s.should_not eql(sub_2.response_header['LAST_MODIFIED'])
sub_3.response_header['ETAG'].to_s.should eql("0")
sub_3.response.should eql("#{body}1_2\r\n")
......@@ -153,10 +153,9 @@ describe "Subscriber Properties" do
sub.callback do
stop = Time.now
time_diff_sec(start, stop).should be_in_the_interval(10, 10.5)
sub.response_header.status.should eql(304)
sub.should be_http_status(304).without_body
Time.parse(sub.response_header['LAST_MODIFIED'].to_s).utc.to_i.should be_in_the_interval(Time.now.utc.to_i-1, Time.now.utc.to_i)
sub.response_header['ETAG'].to_s.should eql("0")
sub.response_header.content_length.should eql(0)
EventMachine.stop
end
end
......@@ -173,10 +172,9 @@ describe "Subscriber Properties" do
sub.callback do
stop = Time.now
time_diff_sec(start, stop).should be_in_the_interval(5, 5.5)
sub.response_header.status.should eql(304)
sub.should be_http_status(304).without_body
Time.parse(sub.response_header['LAST_MODIFIED'].to_s).utc.to_i.should be_in_the_interval(Time.now.utc.to_i-1, Time.now.utc.to_i)
sub.response_header['ETAG'].to_s.should eql("0")
sub.response_header.content_length.should eql(0)
EventMachine.stop
end
end
......@@ -193,10 +191,9 @@ describe "Subscriber Properties" do
sub.callback do
stop = Time.now
time_diff_sec(start, stop).should be_in_the_interval(3, 3.5)
sub.response_header.status.should eql(304)
sub.should be_http_status(304).without_body
Time.parse(sub.response_header['LAST_MODIFIED'].to_s).utc.to_i.should be_in_the_interval(Time.now.utc.to_i-1, Time.now.utc.to_i)
sub.response_header['ETAG'].to_s.should eql("0")
sub.response_header.content_length.should eql(0)
EventMachine.stop
end
end
......@@ -213,8 +210,7 @@ describe "Subscriber Properties" do
sub.callback do
stop = Time.now
time_diff_sec(start, stop).should be_in_the_interval(5, 5.5)
sub.response_header.status.should eql(304)
sub.response_header.content_length.should eql(0)
sub.should be_http_status(304).without_body
EventMachine.stop
end
end
......@@ -249,7 +245,7 @@ describe "Subscriber Properties" do
EM.add_timer(2) do
sub = EventMachine::HttpRequest.new(nginx_address + '/sub/' + channel.to_s).get :head => headers.merge({'If-Modified-Since' => 'Thu, 1 Jan 1970 00:00:00 GMT', 'If-None-Match' => 0})
sub.callback do
sub.response_header.status.should eql(200)
sub.should be_http_status(200)
stored_messages.should eql(messagens_to_publish + 1)
messages = sub.response.split("\r\n")
messages.count.should eql(messagens_to_publish + 1)
......@@ -273,13 +269,13 @@ describe "Subscriber Properties" do
EventMachine.run do
sub_1 = EventMachine::HttpRequest.new(nginx_address + '/sub/' + channel_1.to_s + '/' + channel_2.to_s).get :head => headers.merge({'If-Modified-Since' => 'Thu, 1 Jan 1970 00:00:00 GMT', 'If-None-Match' => 0})
sub_1.callback do
sub_1.response_header.status.should eql(200)
sub_1.should be_http_status(200)
response = JSON.parse(sub_1.response)
response["channel"].should eql(channel_1)
sub_2 = EventMachine::HttpRequest.new(nginx_address + '/sub/' + channel_1.to_s + '/' + channel_2.to_s).get :head => headers.merge({'If-Modified-Since' => sub_1.response_header['LAST_MODIFIED'], 'If-None-Match' => sub_1.response_header['ETAG']})
sub_2.callback do
sub_2.response_header.status.should eql(200)
sub_2.should be_http_status(200)
response = JSON.parse(sub_2.response)
response["channel"].should eql(channel_2)
sub_2.response_header['ETAG'].to_i.should eql(sub_1.response_header['ETAG'].to_i + 1)
......@@ -303,7 +299,7 @@ describe "Subscriber Properties" do
EventMachine.run do
sub_1 = EventMachine::HttpRequest.new(nginx_address + '/sub/' + channel.to_s).get :head => headers
sub_1.callback do
sub_1.response_header.status.should eql(200)
sub_1.should be_http_status(200)
response = JSON.parse(sub_1.response)
response["channel"].should eql(channel)
response["id"].to_i.should eql(-2)
......@@ -312,8 +308,7 @@ describe "Subscriber Properties" do
pub = EventMachine::HttpRequest.new(nginx_address + '/pub?id=' + channel.to_s).delete :head => headers
pub.callback do
pub.response_header.status.should eql(200)
pub.response_header.content_length.should eql(0)
pub.should be_http_status(200).without_body
pub.response_header['X_NGINX_PUSHSTREAM_EXPLAIN'].should eql("Channel deleted.")
end
end
......
......@@ -19,17 +19,17 @@ describe "Subscriber Padding by user agent" do
EventMachine.run do
sub_1 = EventMachine::HttpRequest.new(nginx_address + '/sub/' + channel.to_s).get :head => headers.merge("User-Agent" => "Test 1")
sub_1.callback do
sub_1.response_header.status.should eql(200)
sub_1.should be_http_status(200)
sub_1.response.size.should eql(1100 + conf.header_template.size + 4)
sub_2 = EventMachine::HttpRequest.new(nginx_address + '/sub/' + channel.to_s).get :head => headers.merge("User-Agent" => "Test 2")
sub_2.callback do
sub_2.response_header.status.should eql(200)
sub_2.should be_http_status(200)
sub_2.response.size.should eql(4097 + conf.header_template.size + 4)
sub_3 = EventMachine::HttpRequest.new(nginx_address + '/sub/' + channel.to_s).get :head => headers.merge("User-Agent" => "Test 3")
sub_3.callback do
sub_3.response_header.status.should eql(200)
sub_3.should be_http_status(200)
sub_3.response.size.should eql(conf.header_template.size + 2)
EventMachine.stop
......@@ -49,17 +49,17 @@ describe "Subscriber Padding by user agent" do
EventMachine.run do
sub_1 = EventMachine::HttpRequest.new(nginx_address + '/sub/' + channel.to_s).get :head => headers.merge("User-Agent" => "Test 1")
sub_1.callback {
sub_1.response_header.status.should eql(200)
sub_1.should be_http_status(200)
sub_1.response.size.should eql(500 + body.size + 4)
sub_2 = EventMachine::HttpRequest.new(nginx_address + '/sub/' + channel.to_s).get :head => headers.merge("User-Agent" => "Test 2")
sub_2.callback {
sub_2.response_header.status.should eql(200)
sub_2.should be_http_status(200)
sub_2.response.size.should eql(body.size + 2)
sub_3 = EventMachine::HttpRequest.new(nginx_address + '/sub/' + channel.to_s).get :head => headers.merge("User-Agent" => "Test 3")
sub_3.callback {
sub_3.response_header.status.should eql(200)
sub_3.should be_http_status(200)
sub_3.response.size.should eql(body.size + 2)
EventMachine.stop
......@@ -83,7 +83,7 @@ describe "Subscriber Padding by user agent" do
sub_1 = EventMachine::HttpRequest.new(nginx_address + '/sub/' + channel.to_s).get :head => headers.merge("User-Agent" => "Test 1")
sub_1.callback do
sub_1.response_header.status.should eql(200)
sub_1.should be_http_status(200)
sub_1.response.size.should eql(expected_padding + i + 4)
i = 105
......@@ -91,7 +91,7 @@ describe "Subscriber Padding by user agent" do
sub_1 = EventMachine::HttpRequest.new(nginx_address + '/sub/' + channel.to_s).get :head => headers.merge("User-Agent" => "Test 1")
sub_1.callback do
sub_1.response_header.status.should eql(200)
sub_1.should be_http_status(200)
sub_1.response.size.should eql(expected_padding + i + 4)
i = 221
......@@ -99,7 +99,7 @@ describe "Subscriber Padding by user agent" do
sub_1 = EventMachine::HttpRequest.new(nginx_address + '/sub/' + channel.to_s).get :head => headers.merge("User-Agent" => "Test 1")
sub_1.callback do
sub_1.response_header.status.should eql(200)
sub_1.should be_http_status(200)
sub_1.response.size.should eql(expected_padding + i + 4)
i = 331
......@@ -107,7 +107,7 @@ describe "Subscriber Padding by user agent" do
sub_1 = EventMachine::HttpRequest.new(nginx_address + '/sub/' + channel.to_s).get :head => headers.merge("User-Agent" => "Test 1")
sub_1.callback do
sub_1.response_header.status.should eql(200)
sub_1.should be_http_status(200)
sub_1.response.size.should eql(expected_padding + i + 4)
i = 435
......@@ -115,7 +115,7 @@ describe "Subscriber Padding by user agent" do
sub_1 = EventMachine::HttpRequest.new(nginx_address + '/sub/' + channel.to_s).get :head => headers.merge("User-Agent" => "Test 1")
sub_1.callback do
sub_1.response_header.status.should eql(200)
sub_1.should be_http_status(200)
sub_1.response.size.should eql(expected_padding + i + 4)
i = 502
......@@ -123,14 +123,14 @@ describe "Subscriber Padding by user agent" do
sub_1 = EventMachine::HttpRequest.new(nginx_address + '/sub/' + channel.to_s).get :head => headers.merge("User-Agent" => "Test 1")
sub_1.callback do
sub_1.response_header.status.should eql(200)
sub_1.should be_http_status(200)
sub_1.response.size.should eql(expected_padding + i + 4)
i = 550
sub_1 = EventMachine::HttpRequest.new(nginx_address + '/sub/' + channel.to_s).get :head => headers.merge("User-Agent" => "Test 1")
sub_1.callback do
sub_1.response_header.status.should eql(200)
sub_1.should be_http_status(200)
sub_1.response.size.should eql(i + 2)
EventMachine.stop
......@@ -159,12 +159,12 @@ describe "Subscriber Padding by user agent" do
EventMachine.run do
sub_1 = EventMachine::HttpRequest.new(nginx_address + '/sub/' + channel.to_s + '?ua=test 1').get :head => headers
sub_1.callback do
sub_1.response_header.status.should eql(200)
sub_1.should be_http_status(200)
sub_1.response.size.should eql(1024 + conf.header_template.size + 4)
sub_2 = EventMachine::HttpRequest.new(nginx_address + '/sub/' + channel.to_s + '?ua=test 2').get :head => headers
sub_2.callback do
sub_2.response_header.status.should eql(200)
sub_2.should be_http_status(200)
sub_2.response.size.should eql(conf.header_template.size + 2)
EventMachine.stop
......
This diff is collapsed.
This diff is collapsed.
......@@ -33,23 +33,23 @@ describe "Subscriber WebSocket" do
multi.callback do
multi.responses[:callback].length.should eql(5)
multi.responses[:callback][:a].response_header.status.should eql(405)
multi.responses[:callback][:a].should be_http_status(405)
multi.responses[:callback][:a].req.method.should eql("HEAD")
multi.responses[:callback][:a].response_header['ALLOW'].should eql("GET")
multi.responses[:callback][:b].response_header.status.should eql(405)
multi.responses[:callback][:b].should be_http_status(405)
multi.responses[:callback][:b].req.method.should eql("PUT")
multi.responses[:callback][:b].response_header['ALLOW'].should eql("GET")
multi.responses[:callback][:c].response_header.status.should eql(405)
multi.responses[:callback][:c].should be_http_status(405)
multi.responses[:callback][:c].req.method.should eql("POST")
multi.responses[:callback][:c].response_header['ALLOW'].should eql("GET")
multi.responses[:callback][:d].response_header.status.should eql(405)
multi.responses[:callback][:d].should be_http_status(405)
multi.responses[:callback][:d].req.method.should eql("DELETE")
multi.responses[:callback][:d].response_header['ALLOW'].should eql("GET")
multi.responses[:callback][:e].response_header.status.should_not eql(405)
multi.responses[:callback][:e].should_not be_http_status(405)
multi.responses[:callback][:e].req.method.should eql("GET")
EventMachine.stop
......@@ -311,8 +311,7 @@ describe "Subscriber WebSocket" do
EventMachine.run do
pub = EventMachine::HttpRequest.new(nginx_address + '/channels-stats?id=' + channel.to_s).get :timeout => 30
pub.callback do
pub.response_header.status.should eql(200)
pub.response_header.content_length.should_not eql(0)
pub.should be_http_status(200).with_body
response = JSON.parse(pub.response)
response["channel"].to_s.should eql(channel)
response["published_messages"].to_i.should eql(1)
......@@ -350,8 +349,7 @@ describe "Subscriber WebSocket" do
EventMachine.run do
pub = EventMachine::HttpRequest.new(nginx_address + '/channels-stats?id=' + channel.to_s).get :timeout => 30
pub.callback do
pub.response_header.status.should eql(200)
pub.response_header.content_length.should_not eql(0)
pub.should be_http_status(200).with_body
response = JSON.parse(pub.response)
response["channel"].to_s.should eql(channel)
response["published_messages"].to_i.should eql(0)
......@@ -378,8 +376,7 @@ describe "Subscriber WebSocket" do
EventMachine.run do
pub = EventMachine::HttpRequest.new(nginx_address + '/channels-stats?id=' + channel.to_s).get :timeout => 30
pub.callback do
pub.response_header.status.should eql(200)
pub.response_header.content_length.should_not eql(0)
pub.should be_http_status(200).with_body
response = JSON.parse(pub.response)
response["channel"].to_s.should eql(channel)
response["published_messages"].to_i.should eql(0)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment