Skip to content

Commit

Permalink
make dynamic jsonp flexible about response body format
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelrkn committed Dec 23, 2014
1 parent 230f281 commit 24b848d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/billy/handlers/cache_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ def replace_response_callback(response, url)
request_uri = URI::parse(url)
if request_uri.query
params = CGI::parse(request_uri.query)
if params['callback'] and response[:content].match(/^\w+\({/)
response[:content].sub!(/^\w+\({/, params['callback'].first + '({')
if params['callback'] and response[:content].match(/\w+\(/)
response[:content].sub!(/\w+\(/, params['callback'].first + '(')
end
end
end
Expand Down
9 changes: 9 additions & 0 deletions spec/lib/billy/handlers/cache_handler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@
request[:headers],
request[:body])).to eql({:status=>200, :headers=>{"Connection"=>"close"}, :content=>'dynamicCallback5678({"yolo":"kitten"})'})
end

it 'is flexible about the format of the response body' 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(\n{\"yolo\":\"kitten\"})"})
expect(handler.handle_request(request[:method],
request[:url],
request[:headers],
request[:body])).to eql({:status=>200, :headers=>{"Connection"=>"close"}, :content=>"/**/ dynamicCallback5678(\n{\"yolo\":\"kitten\"})"})
end
end

context 'updating jsonp callback names disabled' do
Expand Down

0 comments on commit 24b848d

Please sign in to comment.