diff --git a/README.md b/README.md index 611abf6..971ff03 100644 --- a/README.md +++ b/README.md @@ -98,6 +98,16 @@ proxy.stub('http://example.com/api', :method => 'post').and_return( :headers => { 'Access-Control-Allow-Origin' => '*' }, :code => 201 ) + +# Stub out an OPTIONS request. Set the headers to the values you require. +proxy.stub('http://example.com/api', :method => :options).and_return( + :headers => { + 'Access-Control-Allow-Methods' => 'GET, PATCH, POST, PUT, OPTIONS', + 'Access-Control-Allow-Headers' => 'X-Requested-With, X-Prototype-Version, Content-Type', + 'Access-Control-Allow-Origin' => '*' + }, + :code => 200 +) ``` Stubs are reset between tests. Any requests that are not stubbed will be diff --git a/examples/preflight_request.html b/examples/preflight_request.html new file mode 100644 index 0000000..ce9af70 --- /dev/null +++ b/examples/preflight_request.html @@ -0,0 +1,22 @@ + + +

Cross Domain Request

+
+ + + diff --git a/spec/features/examples/preflight_request_spec.rb b/spec/features/examples/preflight_request_spec.rb new file mode 100644 index 0000000..987872e --- /dev/null +++ b/spec/features/examples/preflight_request_spec.rb @@ -0,0 +1,29 @@ +require 'spec_helper' + +describe 'jQuery preflight request example', type: :feature, js: true do + let(:url) { 'http://example.com/api' } + + before do + proxy.stub(url, method: 'get').and_return( + headers: { 'Access-Control-Allow-Origin' => '*' }, + code: 201 + ) + end + + it 'stubs out the OPTIONS request' do + visit '/preflight_request.html' + expect(page.find('#result')).to have_content 'Fail!' + + proxy.stub(url, method: 'options').and_return( + headers: { + 'Access-Control-Allow-Methods' => 'GET, OPTIONS', + 'Access-Control-Allow-Headers' => 'Content-Type', + 'Access-Control-Allow-Origin' => '*' + }, + code: 200 + ) + + visit '/preflight_request.html' + expect(page.find('#result')).to have_content 'Success!' + end +end diff --git a/spec/lib/billy/proxy_request_stub_spec.rb b/spec/lib/billy/proxy_request_stub_spec.rb index 447ceb0..27d40f9 100644 --- a/spec/lib/billy/proxy_request_stub_spec.rb +++ b/spec/lib/billy/proxy_request_stub_spec.rb @@ -12,11 +12,22 @@ .matches?('GET', 'http://example.com')).to be expect(Billy::ProxyRequestStub.new('http://example.com', method: :post) .matches?('GET', 'http://example.com')).to_not be + expect(Billy::ProxyRequestStub.new('http://example.com', method: :options) + .matches?('GET', 'http://example.com')).to_not be expect(Billy::ProxyRequestStub.new('http://example.com', method: :post) .matches?('POST', 'http://example.com')).to be expect(Billy::ProxyRequestStub.new('http://fooxample.com', method: :post) .matches?('POST', 'http://example.com')).to_not be + expect(Billy::ProxyRequestStub.new('http://fooxample.com', method: :options) + .matches?('POST', 'http://example.com')).to_not be + + expect(Billy::ProxyRequestStub.new('http://example.com', method: :options) + .matches?('OPTIONS', 'http://example.com')).to be + expect(Billy::ProxyRequestStub.new('http://example.com', method: :options) + .matches?('OPTIONS', 'http://zzzzzexample.com')).to_not be + expect(Billy::ProxyRequestStub.new('http://example.com', method: :post) + .matches?('OPTIONS', 'http://example.com')).to_not be end it 'should match regexps' do diff --git a/spec/lib/proxy_spec.rb b/spec/lib/proxy_spec.rb index aa3c496..4878d47 100644 --- a/spec/lib/proxy_spec.rb +++ b/spec/lib/proxy_spec.rb @@ -22,6 +22,10 @@ it 'should proxy DELETE requests' do expect(http.delete('/echo').body).to eql 'DELETE /echo' end + + it 'should proxy OPTIONS requests' do + expect(http.run_request(:options, '/echo', nil, nil).body).to eql 'OPTIONS /echo' + end end shared_examples_for 'a request stub' do @@ -60,6 +64,12 @@ .and_return(text: 'hello, DELETE!') expect(http.delete('/bam').body).to eql 'hello, DELETE!' end + + it 'should stub OPTIONS requests' do + proxy.stub("#{url}/bim", method: :options) + .and_return(text: 'hello, OPTIONS!') + expect(http.run_request(:options, '/bim', nil, nil).body).to eql 'hello, OPTIONS!' + end end shared_examples_for 'a cache' do