Permalink
Browse files

Added test coverage

  • Loading branch information...
1 parent 5b0e772 commit aa1120c3522258d3bf051251d98faefed0d5b7f4 @ayanko ayanko committed Apr 25, 2012
View
@@ -1,3 +1,16 @@
source 'https://rubygems.org'
gemspec
+
+gem 'activeresource', '3.2.3'
+gem 'net-http-persistent', '2.6'
+
+group :test do
+ gem 'rspec', '2.8.0'
+ gem 'sinatra', '1.3.2'
+ gem 'posix-spawn', '0.3.6'
+end
+
+group :bundle do
+ gem 'passenger', '3.0.12'
+end
View
@@ -0,0 +1,2 @@
+Dir["spec/support/**/*.rb"].each { |f| load f }
+run TestApplication.new
@@ -5,8 +5,11 @@ class Connection
protected
- def new_http
+ def new_http_with_persistent
ActiveResource::Persistent::HTTP.new(site, proxy)
end
+
+ alias_method :new_http_without_persistent, :new_http
+ alias_method :new_http, :new_http_with_persistent
end
end
@@ -8,6 +8,8 @@ def initialize(site, proxy)
@site = site
end
+ attr_reader :site
+
def get(path, headers)
req = Net::HTTP::Get.new(path, headers)
request(@site, req)
View
No changes.
@@ -0,0 +1,74 @@
+require 'spec_helper'
+
+describe ActiveResource::Connection do
+ let(:site) { TestServer.uri }
+
+ let(:instance) { described_class.new(site) }
+
+ describe "#http" do
+ subject { instance.send(:http) }
+ it "should be persistent" do
+ subject.should be_instance_of(ActiveResource::Persistent::HTTP)
+ subject.site.should == site
+ end
+ end
+
+ context "request methods" do
+ let(:path) { '/inspect' }
+ let(:body) { 'HELLO WORLD' }
+ let(:headers) { { 'X_Header' => 'x_value' } }
+ let(:http_mock) { mock('HTTP MOCK') }
+ let(:response_mock) { mock('RESPONSE MOCK', :code => '200') }
+
+ describe "#head" do
+ it "should use ActiveResource::Persistent::HTTP#head" do
+ expected_headers = headers.update('Accept' => 'application/json')
+ instance.should_receive(:http).and_return(http_mock)
+ http_mock.should_receive(:head).with(path, expected_headers).and_return(response_mock)
+
+ instance.head(path, headers)
+ end
+ end
+
+ describe "#get" do
+ it "should use ActiveResource::Persistent::HTTP#get" do
+ expected_headers = headers.update('Accept' => 'application/json')
+ instance.should_receive(:http).and_return(http_mock)
+ http_mock.should_receive(:get).with(path, expected_headers).and_return(response_mock)
+
+ instance.get(path, headers)
+ end
+ end
+
+ describe "#delete" do
+ it "should use ActiveResource::Persistent::HTTP#delete" do
+ expected_headers = headers.update('Accept' => 'application/json')
+ instance.should_receive(:http).and_return(http_mock)
+ http_mock.should_receive(:delete).with(path, expected_headers).and_return(response_mock)
+
+ instance.delete(path, headers)
+ end
+ end
+
+ describe "#post" do
+ it "should use ActiveResource::Persistent::HTTP#post" do
+ expected_headers = headers.update('Content-Type' => 'application/json')
+ instance.should_receive(:http).and_return(http_mock)
+ http_mock.should_receive(:post).with(path, body, expected_headers).and_return(response_mock)
+
+ instance.post(path, body, headers)
+ end
+ end
+
+ describe "#put" do
+ it "should use ActiveResource::Persistent::HTTP#put" do
+ expected_headers = headers.update('Content-Type' => 'application/json')
+ instance.should_receive(:http).and_return(http_mock)
+ http_mock.should_receive(:put).with(path, body, expected_headers).and_return(response_mock)
+
+ instance.put(path, body, headers)
+ end
+ end
+ end
+
+end
@@ -0,0 +1,62 @@
+require 'spec_helper'
+
+describe %q(
+ As Developer
+ I order to speed up my active resources usage
+ I want to use HTTP 1.1 keepalive feature
+) do
+
+ it "Retrieving one resource multiple times" do
+ ids = %w(101 202 303)
+ users = ids.map { |id| TestUser.find(id) }
+
+ users[0].id.should == ids[0]
+ users[1].id.should == ids[1]
+ users[2].id.should == ids[2]
+
+ TestHelper.logged_requests.size.should == 3
+ TestHelper.persistent_requests_logged?.should be_true
+ end
+
+ it "Retrieving two resources from the same site" do
+ user = TestUser.find("101")
+ post = TestPost.find("202")
+
+ user.id.should == "101"
+ post.id.should == "202"
+
+ TestHelper.logged_requests.size.should == 2
+ TestHelper.persistent_requests_logged?.should be_true
+ end
+
+ it "Delete different resources from the same site" do
+ TestUser.find("101").destroy
+ TestPost.find("202").destroy
+
+ TestHelper.logged_requests.size.should == 4
+ TestHelper.persistent_requests_logged?.should be_true
+ end
+
+ it "Updating different resources from the same site" do
+ user = TestUser.find("101").update_attribute(:username, "jack")
+ post = TestPost.find("202").update_attribute(:title, "hi")
+
+ TestHelper.logged_requests.size.should == 4
+ TestHelper.persistent_requests_logged?.should be_true
+ end
+
+
+ it "Creating different resources on the same site" do
+ user = TestUser.create(:username => "Bob")
+ post = TestPost.create(:title => "Hello Bob")
+
+ user.id.should_not be_blank
+ post.id.should_not be_blank
+
+ user.username.should == "Bob"
+ post.title.should == "Hello Bob"
+
+ TestHelper.logged_requests.size.should == 2
+ TestHelper.persistent_requests_logged?.should be_true
+ end
+end
@@ -0,0 +1,77 @@
+require 'spec_helper'
+
+describe ActiveResource::Persistent::HTTP do
+ describe "class" do
+ subject { described_class }
+ its(:superclass) { should == Net::HTTP::Persistent }
+ end
+
+ describe "instance" do
+ let(:site) { URI.parse('http://site.example.com:3000') }
+ let(:proxy) { URI.parse('http://proxy.example.com:8080') }
+
+ subject { described_class.new(site, proxy) }
+
+ it "should have site" do
+ subject.site.should == site
+ end
+
+ it "should have proxy_uri" do
+ subject.proxy_uri.should == proxy
+ end
+ end
+
+ context "request methods" do
+ let(:site) { TestServer.uri }
+ let(:instance) { described_class.new(site, nil) }
+
+ let(:path) { '/inspect/all?key=value' }
+ let(:body) { 'HELLO WORLD' }
+ let(:headers) { { 'X-AUTHORIZATION' => 'qwerty' } }
+
+ it "should HEAD data" do
+ @response = instance.head(path, headers)
+
+ @response.should be_instance_of(Net::HTTPOK)
+ @response.body.should be_nil
+ end
+
+ it "should GET data" do
+ @response = instance.get(path, headers)
+
+ expect_response('GET', nil)
+ end
+
+ it "should DELETE data" do
+ @response = instance.delete(path, headers)
+
+ expect_response('DELETE', nil)
+ end
+
+ it "should POST data" do
+ @response = instance.post(path, body, headers)
+
+ expect_response('POST', body)
+ end
+
+ it "should PUT data" do
+ @response = instance.put(path, body, headers)
+
+ expect_response('PUT', body)
+ end
+
+ protected
+
+ def expect_response(request_method, payload)
+ @response.should be_instance_of(Net::HTTPOK)
+
+ body = YAML.load(@response.body)
+
+ body['REQUEST_METHOD'].should == request_method
+ body['REQUEST_URI'].should == path
+ body['HTTP_X_AUTHORIZATION'].should == 'qwerty'
+ body['rack.input'].should == payload
+ end
+
+ end
+end
View
@@ -0,0 +1,22 @@
+Bundler.setup
+
+require 'active_resource/persistent'
+
+Dir["spec/support/**/*.rb"].each { |f| load f }
+
+RSpec.configure do |config|
+ config.before(:suite) do
+ TestServer.start
+
+ TestUser.site = TestServer.uri
+ TestPost.site = TestServer.uri
+ end
+
+ config.before(:each) do
+ TestIPC.clear
+ end
+
+ config.after(:suite) do
+ TestServer.stop
+ end
+end
Oops, something went wrong.

0 comments on commit aa1120c

Please sign in to comment.