0
我正在通過法拉第提出請求。我使用FakeWeb來剔除請求。爲什麼我用這個Ruby代碼獲取Net :: HTTPBadResponse?
法拉第代碼
middleware = [
Faraday::Request::UrlEncoded,
AirbrakeAPI::Middleware::RaiseResponseError,
FaradayMiddleware::Mashify,
FaradayMiddleware::ParseXml,
AirbrakeAPI::Middleware::ScrubResponse,
AirbrakeAPI::Middleware::RaiseServerError]
@connection ||= Faraday.new(default_options.deep_merge(connection_options)) do |builder|
middleware.each { |mw| builder.use *mw }
builder.adapter :net_http
builder.response :json, :content_type => /\bjson$/
fakeweb代碼
DEFAULTS = {:content_type => "application/json; charset=utf-8", :status => ["200", "OK"]}
def fixture_request(verb, url, file)
FakeWeb.register_uri(verb, url, DEFAULTS.merge(:response => File.join(File.dirname(__FILE__), 'fixtures', file)))
end
fixture_request :get, "http://example.com/notices.json", 'notices.json'
notices.json文件
HTTP/1.1 200 OK
Content-Type: application/json
Date: Sun, 20 Mar 2016 05:24:04 GMT
Transfer-Encoding: chunked
{
"notices": [
{
"id": "1",
"projectId": 1,
"groupId": "1696170",
"createdAt": "2014-10-22T03:00:00.407Z"
},
{
"id": "2",
"projectId": 1,
"groupId": "1234",
"createdAt": "2014-10-22T03:00:00.407Z"
}
],
"count": 2
}
錯誤我得到:
# Net::HTTPBadResponse:
# wrong header line format
在notices.json文件,如果刪除線1-4(行1 = HTTP/1.1 200 OK),那麼我仍然得到這個錯誤:
# Net::HTTPBadResponse:
# wrong status line: "{"