Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add documentation and specs for stubbing out options requests #141

Merged
merged 4 commits into from
May 6, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions examples/preflight_request.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!doctype html>
<body>
<h1>Cross Domain Request</h1>
<div id="result"></div>
<script type='text/javascript' src='http://code.jquery.com/jquery-1.8.2.min.js'></script>
<script type='text/javascript'>
$(function () {
$.ajax(
{
url: 'http://example.com/api',
method: 'GET',
contentType: 'json' // setting this forces an OPTIONS request
}
).done(function(data) {
$('#result').append('Success!');
})
.fail(function(data) {
$('#result').append('Fail!');
});
})
</script>
</body>
26 changes: 26 additions & 0 deletions spec/features/examples/preflight_request_spec.rb
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions spec/lib/billy/proxy_request_stub_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions spec/lib/proxy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It collides with Faraday #options here. This work around does the trick.

end

shared_examples_for 'a request stub' do
Expand Down Expand Up @@ -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
Expand Down