23t*_*tux 44 ruby ajax https phantomjs vcr
我已经在这个领域做了一些研究,但没有找到任何解决方案.我有一个网站,在那里对facebook进行异步ajax调用(使用JSONP).我正在使用VCR在Ruby端记录我的所有HTTP请求,所以我认为将这个功能用于AJAX调用也很酷.
所以我玩了一下,并提出了代理尝试.我正在使用PhantomJS作为Capybara内部集成的无头浏览器和恶作剧者.Poltergeist现在配置为使用这样的代理:
Capybara.register_driver :poltergeist_vcr do |app|
options = {
:phantomjs_options => [
"--proxy=127.0.0.1:9100",
"--proxy-type=http",
"--ignore-ssl-errors=yes",
"--web-security=no"
],
:inspector => true
}
Capybara::Poltergeist::Driver.new(app, options)
end
Capybara.javascript_driver = :poltergeist_vcr
Run Code Online (Sandbox Code Playgroud)
出于测试目的,我编写了一个基于WEbrick的代理服务器,它集成了VCR:
require 'io/wait'
require 'webrick'
require 'webrick/httpproxy'
require 'rubygems'
require 'vcr'
module WEBrick
class VCRProxyServer < HTTPProxyServer
def service(*args)
VCR.use_cassette('proxied') { super(*args) }
end
end
end
VCR.configure do |c|
c.stub_with :webmock
c.cassette_library_dir = '.'
c.default_cassette_options = { :record => :new_episodes }
c.ignore_localhost = true
end
IP = '127.0.0.1'
PORT = 9100
reader, writer = IO.pipe
@pid = fork do
reader.close
$stderr = writer
server = WEBrick::VCRProxyServer.new(:BindAddress => IP, :Port => PORT)
trap('INT') { server.shutdown }
server.start
end
raise 'VCR Proxy did not start in 10 seconds' unless reader.wait(10)
Run Code Online (Sandbox Code Playgroud)
这适用于每个localhost调用,并且它们得到很好的记录.HTML,JS和CSS文件由VCR记录.然后我启用了该c.ignore_localhost = true
选项,因为它(在我看来)无法记录localhost调用.
然后我再次尝试,但我不得不弄清楚,没有记录在页面上进行的AJAX调用.更糟糕的是,他们不再在测试中工作了.
所以说到这一点,我的问题是:为什么所有对本地主机上的JS文件的调用都被记录下来,而JSONP调用外部资源却没有?它不能是jsonP的东西,因为它是一个"正常"的ajax请求.或者phantomjs中是否存在错误,AJAX调用未被代理?如果是这样,我们怎么能解决这个问题?
如果它正在运行,我想在里面集成启动和停止程序
-------更新-------
我做了一些研究并得出以下观点:代理在HTTPS调用和通过HTTPS调用的二进制数据方面存在一些问题.
我启动了服务器,并进行了一些卷曲调用:
curl --proxy 127.0.0.1:9100 http://d3jgo56a5b0my0.cloudfront.net/images/v7/application/stories_view/icons/bug.png
Run Code Online (Sandbox Code Playgroud)
此呼叫会按原样记录.代理的请求和响应输出是
GET http://d3jgo56a5b0my0.cloudfront.net/images/v7/application/stories_view/icons/bug.png HTTP/1.1
User-Agent: curl/7.24.0 (x86_64-apple-darwin12.0) libcurl/7.24.0 OpenSSL/0.9.8r zlib/1.2.5
Host: d3jgo56a5b0my0.cloudfront.net
Accept: */*
Proxy-Connection: Keep-Alive
HTTP/1.1 200 OK
Server: WEBrick/1.3.1 (Ruby/1.9.3/2012-10-12)
Date: Tue, 20 Nov 2012 10:13:10 GMT
Content-Length: 0
Connection: Keep-Alive
Run Code Online (Sandbox Code Playgroud)
但是这个调用没有记录,HTTPS必然存在一些问题:
curl --proxy 127.0.0.1:9100 https://d3jgo56a5b0my0.cloudfront.net/images/v7/application/stories_view/icons/bug.png
Run Code Online (Sandbox Code Playgroud)
标头输出是:
CONNECT d3jgo56a5b0my0.cloudfront.net:443 HTTP/1.1
Host: d3jgo56a5b0my0.cloudfront.net:443
User-Agent: curl/7.24.0 (x86_64-apple-darwin12.0) libcurl/7.24.0 OpenSSL/0.9.8r zlib/1.2.5
Proxy-Connection: Keep-Alive
HTTP/1.1 200 OK
Server: WEBrick/1.3.1 (Ruby/1.9.3/2012-10-12)
Date: Tue, 20 Nov 2012 10:15:48 GMT
Content-Length: 0
Connection: close
Run Code Online (Sandbox Code Playgroud)
所以,我想也许代理不能处理HTTPS,但它可以(只要我在cURL调用后得到控制台上的输出).然后我想,也许VCR无法模拟HTTPS请求.但是当我不在代理中使用它时,使用此脚本,VCR会模拟HTTPS请求:
require 'vcr'
VCR.configure do |c|
c.hook_into :webmock
c.cassette_library_dir = 'cassettes'
end
uri = URI("https://d3jgo56a5b0my0.cloudfront.net/images/v7/application/stories_view/icons/bug.png")
VCR.use_cassette('https', :record => :new_episodes) do
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
response = http.request_get(uri.path)
puts response.body
end
Run Code Online (Sandbox Code Playgroud)
那么问题是什么?VCR处理HTTPS,代理处理HTTPS.他们为什么不一起玩?
所以我做了一些研究,现在我有一个非常基本的工作VCR代理服务器示例,它将HTTPS调用作为MITM代理服务器处理(如果您在客户端中停用安全检查).如果有人能够做出贡献并帮助我将这件事变为现实,我将非常高兴.
这是github repo:https://github.com/23tux/vcr_proxy
归档时间: |
|
查看次数: |
3694 次 |
最近记录: |