2013-04-18 33 views
1

我正在爲我的測試套件記錄帶有VCR的http請求。我必須忽略名爲callback的參數,因爲參數是隨機的,並且當此參數更改時(因爲只有參數發生更改,而不是請求本身),我不希望VCR記錄新請求。VCR忽略參數:獲取真實的http請求

但我的問題是,我需要修改響應正文錄像機服務於我的應用程序,根據我的客戶端在運行時生成的原始值callback

這裏是我的錄像機配置:

VCR.configure do |c| 
    c.cassette_library_dir = 'spec/vcr_cassettes' 
    c.hook_into :webmock 
    c.allow_http_connections_when_no_cassette = false 
    c.ignore_localhost = true 

    c.before_http_request(:stubbed?) do |request| 
    # request.uri is the ORIGINAL request made by the client 
    puts request.uri 
    end 

    c.before_playback do |interaction, cassette| 
    # request uri got the value from the cassette 
    interaction.request.uri 
    # and here I want to modify the interaction.response.body 
    # based on the callback parameter from the original request from the client 
    interaction.response.body = "...." 
    end 

    # add the callback and no caching _ parameter to the ignored parameters 
    c.default_cassette_options = { 
    record: :once, 
    match_requests_on: [:method, VCR.request_matchers.uri_without_params("callback", "_")] 
    } 
end 

c.before_http_request(:stubbed?)回調的callback真正的價值是在request.uri。然後錄像機忽略此參數,並重放以前錄製的錄像帶。在c.before_playback回調中,我可以修改interaction.response.body,但callback參數在錄製時從盒帶中獲得了該值。

怎麼可能在c.before_playback回調裏面得到實數callback?因爲這個回調是唯一允許修改響應主體的回調函數。我試用了c.after_http_request(:stubbed?),在那裏我得到了響應主體和callback參數的實際值,但是這個鉤子太晚了,因爲我的應用已經收到了響應主體。

任何monkeypatch,骯髒的黑客或詭計將是偉大的!

+0

也沒有人對此有一個解決方案嗎?我看着錄像機的寶石源代碼,但我還沒有想出如何做到這一點。我搜索了VCR首先識別http請求的地方,但我沒有。我不確定,也許第一個地方是模擬框架,並且VCR不能訪問來自客戶端的真實請求 – 23tux 2013-04-18 20:26:49

回答