Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/oesmith/puffing-billy int…
Browse files Browse the repository at this point in the history
…o simulate_slow_responses
  • Loading branch information
Ron Smith committed Apr 6, 2017
2 parents 84ecdae + 56e5c23 commit d1357a9
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 6 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,9 @@ using `c.dynamic_jsonp`. This is helpful when JSONP APIs use cache-busting
parameters. For example, if you want `http://example.com/foo?callback=bar&id=1&cache_bust=12345` and `http://example.com/foo?callback=baz&id=1&cache_bust=98765` to be cache hits for each other, you would set `c.dynamic_jsonp_keys = ['callback', 'cache_bust']` to ignore both params. Note
that in this example the `id` param would still be considered important.

`c.dynamic_jsonp_callback_name` is used to configure the name of the JSONP callback
parameter. The default is `callback`.

`c.path_blacklist = []` is used to always cache specific paths on any hostnames,
including whitelisted ones. This is useful if your AUT has routes that get data
from external services, such as `/api` where the ajax request is a local URL but
Expand Down
7 changes: 4 additions & 3 deletions lib/billy/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ class Config
attr_accessor :logger, :cache, :cache_request_headers, :whitelist, :path_blacklist, :ignore_params,
:persist_cache, :ignore_cache_port, :non_successful_cache_disabled, :non_successful_error_level,
:non_whitelisted_requests_disabled, :cache_path, :proxy_host, :proxy_port, :proxied_request_inactivity_timeout,
:proxied_request_connect_timeout, :dynamic_jsonp, :dynamic_jsonp_keys, :merge_cached_responses_whitelist,
:strip_query_params, :proxied_request_host, :proxied_request_port, :cache_request_body_methods, :after_cache_handles_request, :cache_simulates_network_delays, :cache_simulates_network_delay_time,
:record_stub_requests
:proxied_request_connect_timeout, :dynamic_jsonp, :dynamic_jsonp_keys, :dynamic_jsonp_callback_name, :merge_cached_responses_whitelist,
:strip_query_params, :proxied_request_host, :proxied_request_port, :cache_request_body_methods, :after_cache_handles_request,
:cache_simulates_network_delays, :cache_simulates_network_delay_time, :record_stub_requests

def initialize
@logger = defined?(Rails) ? Rails.logger : Logger.new(STDOUT)
Expand All @@ -28,6 +28,7 @@ def reset
@persist_cache = false
@dynamic_jsonp = false
@dynamic_jsonp_keys = ['callback']
@dynamic_jsonp_callback_name = 'callback'
@ignore_cache_port = true
@non_successful_cache_disabled = false
@non_successful_error_level = :warn
Expand Down
5 changes: 3 additions & 2 deletions lib/billy/handlers/cache_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ def replace_response_callback(response, url)
request_uri = Addressable::URI.parse(url)
if request_uri.query
params = CGI.parse(request_uri.query)
if params['callback'].any? && response[:content].match(/\w+\(/)
response[:content].sub!(/\w+\(/, params['callback'].first + '(')
callback_name = Billy.config.dynamic_jsonp_callback_name
if params[callback_name].any? && response[:content].match(/\w+\(/)
response[:content].sub!(/\w+\(/, params[callback_name].first + '(')
end
end
end
Expand Down
23 changes: 22 additions & 1 deletion spec/lib/billy/handlers/cache_handler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

describe Billy::CacheHandler do
let(:handler) { Billy::CacheHandler.new }
let(:request_url) { 'http://example.test:8080/index?some=param&callback=dynamicCallback5678' }
let(:request) do
{
method: 'post',
url: 'http://example.test:8080/index?some=param&callback=dynamicCallback5678',
url: request_url,
headers: { 'Accept-Encoding' => 'gzip',
'Cache-Control' => 'no-cache' },
body: 'Some body'
Expand Down Expand Up @@ -114,6 +115,26 @@
request[:body])).to eql(status: 200, headers: { 'Connection' => 'close', 'Access-Control-Allow-Origin' => "*" }, content: 'Some body')
end
end

context 'when dynamic_jsonp_callback_name is set' do
let(:dynamic_jsonp_callback_name) { 'customCallback' }
let(:request_url) { "http://example.test:8080/index?some=param&#{dynamic_jsonp_callback_name}=dynamicCallback5678" }

before do
allow(Billy.config).to receive(:dynamic_jsonp_callback_name) do
dynamic_jsonp_callback_name
end
end

it 'should call the callback with the specified name' do
expect(Billy::Cache.instance).to receive(:cached?).and_return(true)
expect(Billy::Cache.instance).to receive(:fetch).and_return(status: 200, headers: { 'Connection' => 'close' }, content: 'dynamicCallback1234({"yolo":"kitten"})')
expect(handler.handle_request(request[:method],
request[:url],
request[:headers],
request[:body])).to eql(status: 200, headers: { 'Connection' => 'close' }, content: 'dynamicCallback5678({"yolo":"kitten"})')
end
end
end

context 'updating jsonp callback names disabled' do
Expand Down

0 comments on commit d1357a9

Please sign in to comment.