From 65b8e79f0ffb960bbff13a3dcecf6064453b1770 Mon Sep 17 00:00:00 2001 From: Rimian Perkins Date: Sun, 28 Feb 2016 00:20:46 +1100 Subject: [PATCH 1/4] stubs out options requests --- examples/preflight_request.html | 22 ++++++++++++++++ .../examples/preflight_request_spec.rb | 26 +++++++++++++++++++ spec/lib/billy/proxy_request_stub_spec.rb | 11 ++++++++ spec/lib/proxy_spec.rb | 10 +++++++ 4 files changed, 69 insertions(+) create mode 100644 examples/preflight_request.html create mode 100644 spec/features/examples/preflight_request_spec.rb 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..943960a --- /dev/null +++ b/spec/features/examples/preflight_request_spec.rb @@ -0,0 +1,26 @@ +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 + 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 From 09c33800c64228e1fd09fb9b917e8d36e932afb9 Mon Sep 17 00:00:00 2001 From: Rimian Perkins Date: Sun, 28 Feb 2016 00:32:46 +1100 Subject: [PATCH 2/4] the preflight request fails before the OPTIONS request is stubbed out --- spec/features/examples/preflight_request_spec.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/spec/features/examples/preflight_request_spec.rb b/spec/features/examples/preflight_request_spec.rb index 943960a..987872e 100644 --- a/spec/features/examples/preflight_request_spec.rb +++ b/spec/features/examples/preflight_request_spec.rb @@ -11,6 +11,9 @@ 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', From 2d21e66dbeb52cb3dcd6adc257a38a91ebc3e74c Mon Sep 17 00:00:00 2001 From: Rimian Perkins Date: Mon, 11 Apr 2016 18:03:48 +1000 Subject: [PATCH 3/4] document stubbing out OPTIONS request in README --- README.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/README.md b/README.md index 611abf6..926116b 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(url, :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 From 2cf76e808b24cfca0065c8549a1eb506101d858d Mon Sep 17 00:00:00 2001 From: Rimian Perkins Date: Mon, 11 Apr 2016 18:05:04 +1000 Subject: [PATCH 4/4] Whoops! Fix the URL --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 926116b..971ff03 100644 --- a/README.md +++ b/README.md @@ -100,7 +100,7 @@ proxy.stub('http://example.com/api', :method => 'post').and_return( ) # Stub out an OPTIONS request. Set the headers to the values you require. -proxy.stub(url, :method => :options).and_return( +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',