gpt4 book ai didi

ruby-on-rails - Webmock:如何伪造 gzipped 响应?

转载 作者:行者123 更新时间:2023-12-03 17:34:29 25 4
gpt4 key购买 nike

我在 stub 方面没有太多经验,并且在处理对 Braintree 的请求时遇到问题使用 webmockbraintree-rails .

规范/spec_helper.rb

RSpec.configure do |config|
config.include(ConnectionHelper)

config.before(:each) do
stub_request(:post, /.*braintree.*/).
with(braintree_hash).to_return(gzipped_response)
end
end

规范/支持/connection_helper.rb

def gzipped_response
{
status: 200,
body: "\u001F\x8B\b\0:\x87GU\0\u0003\u0003\0\0\0\0\0\0\0\0\0",
headers: {}
}
end

def braintree_hash
{ :body => /.*/,
:headers => {'Accept'=>'application/xml', 'Content-Type'=>'application/xml',
'User-Agent'=>'Braintree Ruby Gem 2.42.0 (braintree-rails-1.4.0)',
'X-Apiversion'=>'4'}
}
end

Rspec 错误:

2) Content: when ordering content show page has relevant information 
Failure/Error: click_button "Order"
Braintree::UnexpectedError:
expected a gzipped response
# ./app/classes/payment.rb:13:in `generate_token'
# ./app/controllers/posts_controller.rb:44:in `pay'
# ./spec/features/content_spec.rb:251:in `block (4 levels) in <top (required)>'

我正在尝试测试页面,而不是付款本身,但是在呈现页面时需要先检索 token ,所以我收到此错误。

我将如何伪造 gzip 响应,或者在我的测试中跳过与 Braintree 请求有关的任何事情?

app/controllers/posts_controller.rb

def pay
@post = Post.find(params[:id])
@client_token = Payment.new(current_user).generate_token
end

app/classes/payment.rb

class Payment    
def initialize(customer)
@customer = customer
@customer_id = @customer.id
end

def generate_token
Braintree::ClientToken.generate(customer_id: @customer_id)
end
end

最佳答案

我在布伦特里工作。如果您对我们的 API 和客户端库有任何疑问,您可以随时 reach out to our support team .

您的 stub 响应主体需要压缩。您可以像这样创建一个空的压缩字符串:

irb(main):010:0> require 'stringio' 
=> false
irb(main):011:0> require 'zlib'
=> false
irb(main):012:0> Zlib::GzipWriter.new(StringIO.new("w")).close.string
=> "\u001F\x8B\b\0:\x87GU\0\u0003\u0003\0\0\0\0\0\0\0\0\0"

因此,为您的 status_ok 方法试试这个:

def status_ok
{
status: 200,
body: "\u001F\x8B\b\0:\x87GU\0\u0003\u0003\0\0\0\0\0\0\0\0\0",
headers: {"Content-Encoding" => "gzip"}
}
end

关于ruby-on-rails - Webmock:如何伪造 gzipped 响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30028604/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com