From f2f4f2e65658b29d7252f4cad67d0aad0d0bd876 Mon Sep 17 00:00:00 2001 From: Olly Smith Date: Mon, 8 Oct 2012 07:42:34 +0100 Subject: [PATCH] Rspec helper. --- lib/billy/rspec.rb | 26 ++++++++++++++++++++++++++ spec/request/proxy_spec.rb | 23 +++++++---------------- spec/spec_helper.rb | 2 ++ 3 files changed, 35 insertions(+), 16 deletions(-) create mode 100644 lib/billy/rspec.rb diff --git a/lib/billy/rspec.rb b/lib/billy/rspec.rb new file mode 100644 index 0000000..56b52b0 --- /dev/null +++ b/lib/billy/rspec.rb @@ -0,0 +1,26 @@ +require 'rspec' +require 'billy' + +$billy_proxy = Billy::Proxy.new +$billy_proxy.start + +module Billy + def self.proxy + $billy_proxy + end + + module RspecHelper + def proxy + Billy.proxy + end + end +end + +RSpec.configure do |config| + config.include(Billy::RspecHelper) + + config.after(:each) do + proxy.reset + end +end + diff --git a/spec/request/proxy_spec.rb b/spec/request/proxy_spec.rb index 713deb9..059d36f 100644 --- a/spec/request/proxy_spec.rb +++ b/spec/request/proxy_spec.rb @@ -25,31 +25,31 @@ shared_examples_for 'a request stub' do it 'should stub GET requests' do - @proxy.stub("#{url}/foo"). + proxy.stub("#{url}/foo"). and_return(200, {}, 'hello, GET!') http.get('/foo').body.should == 'hello, GET!' end it 'should stub POST requests' do - @proxy.stub("#{url}/bar", :method => :post). + proxy.stub("#{url}/bar", :method => :post). and_return(200, {}, 'hello, POST!') http.post('/bar', :foo => :bar).body.should == 'hello, POST!' end it 'should stub PUT requests' do - @proxy.stub("#{url}/baz", :method => :put). + proxy.stub("#{url}/baz", :method => :put). and_return(200, {}, 'hello, PUT!') http.put('/baz', :foo => :bar).body.should == 'hello, PUT!' end it 'should stub HEAD requests' do - @proxy.stub("#{url}/bap", :method => :head). + proxy.stub("#{url}/bap", :method => :head). and_return(200, {'HTTP-X-Hello' => 'hello, HEAD!'}, nil) http.head('/bap').headers['http_x_hello'] == 'hello, HEAD!' end it 'should stub DELETE requests' do - @proxy.stub("#{url}/bam", :method => :delete). + proxy.stub("#{url}/bam", :method => :delete). and_return(200, {}, 'hello, DELETE!') http.delete('/bam').body.should == 'hello, DELETE!' end @@ -57,27 +57,18 @@ describe Billy::Proxy do - before :all do - @proxy = Billy::Proxy.new - @proxy.start - end - before do @http = Faraday.new @http_url, - :proxy => { :uri => @proxy.url }, + :proxy => { :uri => proxy.url }, :keepalive => false, :timeout => 0.5 @https = Faraday.new @https_url, :ssl => { :verify => false }, - :proxy => { :uri => @proxy.url }, + :proxy => { :uri => proxy.url }, :keepalive => false, :timeout => 0.5 end - after do - @proxy.reset - end - context 'proxying' do context 'HTTP' do diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 95307b4..0e87c3d 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,5 +1,7 @@ Dir[File.expand_path("../support/**/*.rb", __FILE__)].each {|f| require f} +require 'billy/rspec' + RSpec.configure do |config| config.treat_symbols_as_metadata_keys_with_true_values = true config.run_all_when_everything_filtered = true