jmo*_*ina 7 rspec ruby-on-rails http-headers
我正在使用rails 4开发rails API,我想测试它.要做到这一点,我正在使用Rspec框架.
我的API有一个身份验证方法,就像这样调用:
class UsersController < ApplicationController
# Token authentication
before_action :set_user, except: [:index, :create]
before_action :authenticate, except: [:index, :create]
before_action :authenticate_admin, only: [:index, :create]
...
end
Run Code Online (Sandbox Code Playgroud)
然后我有我的UsersController Rspec:
describe UsersController do
render_views
describe "GET /users" do
it "gets all users" do
@request.env["AUTHORIZATION"] = "Token token=xxxxxxxxx"
@request.env["HTTP_ACCEPT"] = "application/json"
@request.env["CONTENT_TYPE"] = "application/json"
get :index
response.status.should be(200)
end
end
end
Run Code Online (Sandbox Code Playgroud)
当我执行测试时,它总是给我一样的:
UsersController GET /users getting all users
Failure/Error: response.status.should be(200)
expected #<Fixnum:401> => 200
got #<Fixnum:803> => 401
...
Run Code Online (Sandbox Code Playgroud)
我使用authenticate_or_request_with_http_token方法从头文件中获取令牌.
如果有人可以帮我找到跳过before_action方法或正确设置Authentication头的方法,我将非常感激.
谢谢!
jmo*_*ina 10
我终于解决了这个问题.
错误是:
request.env["AUTHORIZATION"]
Run Code Online (Sandbox Code Playgroud)
一定是
request.env["HTTP_AUTHORIZATION"].
Run Code Online (Sandbox Code Playgroud)
除此之外,我从一个未在我的测试环境中创建的用户那里获得了一个错误的令牌.
:)
| 归档时间: |
|
| 查看次数: |
3133 次 |
| 最近记录: |