我有以下代码生成用于测试内部 Restful API 身份验证的哈希。
问题是生成的哈希返回转义值。
该json_headers
方法应返回如下内容:
{"Authorization"=>"Basic ZnBfZXJwOllrdGh4aHRSZTZWM1d1d09oeVlmcW0xTg==", "Accept"=>"application/json"}
但是正在返回:
{\"Authorization\"=>\"Basic ZnBfZXJwOllrdGh4aHRSZTZWM1d1d09oeVlmcW0xTg==\\n\", \"Accept\"=>\"application/json\"}
有谁知道从 Ruby 哈希返回原始键/值的简单方法?
这是代码:
module ERP
class CustomersMock
def json_headers
authorization_hash.merge("Accept"=>"application/json")
end
def authorization_hash
{"Authorization" => "Basic #{base64_user_pass}"}
end
def user_pass
# user:password
[ ERP_CONF['user'], ERP_CONF['password'] ].join(':')
end
def base64_user_pass
Base64.encode64(user_pass)
end
def add(customer)
customers << customer
end
def customers
[]
end
def run
ActiveResource::HttpMock.respond_to do |mock|
customers.each do |customer|
mock.get "/customers/#{customer.id}.json", json_headers, customer.to_json
end
end
end
end
end
出于某种原因,如果我对标头散列进行硬编码,它们就可以工作,但是如果我调用该json_headers
方法,该respond_to
块会将它们转换为字符串并转义值。
这是 rspec 失败:
Could not find a response recorded for <GET: /api/customers/1011.json [{"Authorization"=>"Basic ZnBfZXJwOllrdGh4aHRSZTZWM1d1d09oeVlmcW0xTg==", "Accept"=>"application/json"}] ()> - Responses recorded are: ["<GET: /api/customers/1011.json [{\"Authorization\"=>\"Basic ZnBfZXJwOllrdGh4aHRSZTZWM1d1d09oeVlmcW0xTg==\\n\", \"Accept\"=>\"application/json\"}] ()>"]