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

Fix scope breaking change of request/response interception. (#241) #242

Merged
merged 1 commit into from
May 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,10 @@ proxy.stub('https://example.com/proc/').and_return(Proc.new { |params, headers,
# You can also use Puffing Billy to intercept requests and responses. Just pass
# a Proc and use the pass_request method. You can manipulate the request
# (headers, URL, HTTP method, etc) and also the response from the upstream
# server.
# server. The scope of the delivered callable is the user scope where
# it was defined.
proxy.stub('http://example.com/').and_return(Proc.new { |*args|
response = pass_request(*args)
response = Billy.pass_request(*args)
response[:headers]['Content-Type'] = 'text/plain'
response[:body] = 'Hello World!'
response[:code] = 200
Expand Down
14 changes: 14 additions & 0 deletions lib/billy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,18 @@ def self.proxy
def self.certificate_authority
@certificate_authority ||= Billy::Authority.new
end

# This global shortcut can be used inside of request stubs. You can modify
# the request beforehand and/or modify the actual response which is passed
# back by this method. But you can also implement a custom proxy passing
# method if you like to. This is just a shortcut.
def self.pass_request(params, headers, body, url, method)
Copy link
Contributor

Choose a reason for hiding this comment

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

Just to confirm, is params a dead variable here?

handler = proxy.request_handler.handlers[:proxy]
response = handler.handle_request(method, url, headers, body)
Copy link
Contributor

@AlanFoster AlanFoster May 1, 2018

Choose a reason for hiding this comment

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

nab: It's interesting to see the difference between internet/external APIs here.

i.e. the method args are: params, headers, body, url, method - but we're calling internally with method, url, headers, body

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes because of:

proxy.stub('http://example.com/').and_return(Proc.new { |*args|
  response = pass_request(*args)
end

Copy link
Contributor Author

@Jack12816 Jack12816 May 2, 2018

Choose a reason for hiding this comment

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

Makes it easier to use here without introduce a breaking change (on the order of and_return arguments). Its more likely to intercept the response and modify it as to tweak the request. It was fine in first place, no need to debate on it in a fixup.

{
code: response[:status],
body: response[:content],
headers: response[:headers]
}
end
end
12 changes: 1 addition & 11 deletions lib/billy/proxy_request_stub.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def call(method, url, params, headers, body)
push_request(method, url, params, headers, body)

if @response.respond_to?(:call)
res = instance_exec(params, headers, body, url, method, &@response)
res = @response.call(params, headers, body, url, method)
else
res = @response
end
Expand Down Expand Up @@ -71,16 +71,6 @@ def matches?(method, url)
end
end

def pass_request(params, headers, body, url, method)
handler = Billy.proxy.request_handler.handlers[:proxy]
response = handler.handle_request(method, url, headers, body)
{
code: response[:status],
body: response[:content],
headers: response[:headers]
}
end

private

attr_writer :requests
Expand Down
68 changes: 49 additions & 19 deletions spec/features/examples/tumblr_api_spec.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,6 @@
require 'spec_helper'

describe 'Tumblr API example', type: :feature, js: true do
before do
proxy.stub('http://blog.howmanyleft.co.uk/api/read/json').and_return(
jsonp: {
posts: [
{
'regular-title' => 'News Item 1',
'url-with-slug' => 'http://example.com/news/1',
'regular-body' => 'News item 1 content here'
},
{
'regular-title' => 'News Item 2',
'url-with-slug' => 'http://example.com/news/2',
'regular-body' => 'News item 2 content here'
}
]
})
end

RSpec.shared_examples 'tumblr/expectations' do
it 'should show news stories' do
visit '/tumblr_api.html'
expect(page).to have_link('News Item 1', href: 'http://example.com/news/1')
Expand All @@ -27,3 +9,51 @@
expect(page).to have_content('News item 2 content here')
end
end

describe 'Tumblr API example', type: :feature, js: true do
context 'without scope external references' do
before do
proxy.stub('http://blog.howmanyleft.co.uk/api/read/json').and_return(
jsonp: {
posts: [
{
'regular-title' => 'News Item 1',
'url-with-slug' => 'http://example.com/news/1',
'regular-body' => 'News item 1 content here'
},
{
'regular-title' => 'News Item 2',
'url-with-slug' => 'http://example.com/news/2',
'regular-body' => 'News item 2 content here'
}
]
})
end

include_examples 'tumblr/expectations'
end

context 'with scope external references' do
let(:posts) do
[
{
'regular-title' => 'News Item 1',
'url-with-slug' => 'http://example.com/news/1',
'regular-body' => 'News item 1 content here'
},
{
'regular-title' => 'News Item 2',
'url-with-slug' => 'http://example.com/news/2',
'regular-body' => 'News item 2 content here'
}
]
end

before do
proxy.stub('http://blog.howmanyleft.co.uk/api/read/json')
.and_return(proc { { jsonp: { posts: posts } } })
end

include_examples 'tumblr/expectations'
end
end
10 changes: 2 additions & 8 deletions spec/lib/billy/proxy_request_stub_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,6 @@
expected_headers = { 'header1' => 'three', 'header2' => 'four' }
expected_body = 'body text'

# Required due to the instance_exec implementation
subject.extend(RSpec::Matchers)

subject.and_return(proc do |params, headers, body, url, method|
expect(params).to eql expected_params
expect(headers).to eql expected_headers
Expand All @@ -175,15 +172,12 @@
]
end

it 'should use a callable with pass_request' do
it 'should use a callable with Billy.pass_request' do
# Add the missing em-synchrony call which is done by
# ProxyConnection#handle_request instead.
EM.synchrony do
# Required due to the instance_exec implementation
subject.extend(RSpec::Matchers)

subject.and_return(proc do |*args|
response = pass_request(*args)
response = Billy.pass_request(*args)
response[:body] = 'modified'
response[:code] = 205
response
Expand Down