【问题标题】:testing rails with rspec calling api with authorization使用授权调用 api 的 rspec 测试 rails
【发布时间】:2014-07-03 23:47:15
【问题描述】:

我正在尝试使用 rspec 2.12.2 编写测试,以测试用 rails 3.2.6 编写的 api。

但是授权令牌没有被传递,从 curl 调用它没有问题。

get '/API/V1/voucher/XXXXXXXXXXXXXXX/redeem.json', {}, { 'Authorization' => 'Token token=XXXXXXXXXXXXXXX'}

从覆盖方法的请求返回的响应以在下面的 json 中返回错误。

def request_http_token_authentication(realm = "Application")  
    self.headers["WWW-Authenticate"] = %(Token realm="#{realm.gsub(/"/, "")}")
    self.__send__ :render, :json => { :error => "HTTP Token: Access denied. You did not       provide an valid API key." }.to_json, :status => :unauthorized
end

【问题讨论】:

  • 这是功能规范还是控制器规范?

标签: ruby-on-rails testing rspec


【解决方案1】:

将标题设置为此有效。

get '/API/V1/voucher/XXXXXXXXXXXXXXX/redeem.json', {}, { 'HTTP_AUTHORIZATION' => 'Token token=XXXXXXXXXXXXXXX'}

【讨论】:

    猜你喜欢
    • 2017-11-03
    • 1970-01-01
    • 1970-01-01
    • 2021-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多