设置如下。对于每个http请求,经理都会在头部(名称,密码)中发送他的凭证。这些检查与数据库中的条目,如果他们成功返回所需的用户对象。在请求测试中如何实现基本的http_auth?我只想添加密码和用户名并测试返回值?请求测试的目标是什么?我想没有多少成功如下:Rspec Rails 4.2.5使用基本http验证的请求测试传递
我创建了一个化AuthHelper模块spec/support/auth_helper.rb
与
module AuthHelper
def http_login
user = 'test'
pw = 'test'
request.ENV['HTTP_AUTHORIZATION'] =ActionController::HttpAuthentication::Basic.encode_credentials(user,pw)
end
end
和如下
include AuthHelper
describe "User API get 1 user object" do
before(:each) do
http_login
end
,但我收到此错误信息使用它在requests/api/user_spec.rb
。我怎样才能解决这个问题,并使我的测试通过http_auth?我在这里看到很多类似的topis和问题,但 他们主要适用于旧版本的rspec和导轨,并且不适用于我的案例
在此先感谢!
Failure/Error: request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Basic.encode_credentials(user,pw)
NoMethodError:
undefined method `env' for nil:NilClass# ./spec/support
# ./spec/support/auth_helper.rb:5:in `http_login'
# ./spec/requests/api/user_spec.rb:8:in `block (2 levels) in <top (required)>'
更新
我移动的请求内的报头生成。我查了一下Auth的动词,所以我认为这个任务应该可以工作。我在rails控制台中测试了ActionController调用,并收到一个字符串。我想这也是correct.My整个测试现在看起来是这样的:
describe "User API get 1 user object", type: :request do
it 'Get sends back one User object ' do
headers = {
"AUTHORIZATION" =>ActionController::HttpAuthentication::Basic.encode_credentials("test","test")
# "AUTHORIZATION" =>ActionController::HttpAuthentication::Token.encode_credentials("test","test")
}
FactoryGirl.create(:user)
get "/api/1/user", headers
#json = JSON.parse(response.body)
expect(response).to be_success
# expect(response.content_type).to eq("application/json")
end
end
收到以下错误:
这incudles所以访问被拒绝还是行@buf=["HTTP Basic: Access denied.\n"]
。
Failure/Error: expect(response).to be_success
expected `#<ActionDispatch::TestResponse:0x000000070d1d38 @mon_owner=nil, @mon_count=0, @mon_mutex=#<Thread::Mutex:0x000000070d1c98>, @stream=#<ActionDispatch::Response::Buffer:0x000000070d1c48 @response=#<ActionDispatch::TestResponse:0x000000070d1d38 ...>,
@buf=["HTTP Basic: Access denied.\n"], @closed=false>, @header={"X-Frame-Options"=>"SAMEORIGIN", "X-XSS-Protection"=>"1; mode=block", "X-Content-Type-Options"=>"nosniff", "WWW-Authenticate"=>"Basic realm=\"Application\"", "Content-Type"=>"text/html; charset=utf-8", "Cache-Control"=>"no-cache", "X-Request-Id"=>"9c27d4e9-84c0-4ef3-82ed-cccfb19876a0", "X-Runtime"=>"0.134230", "Content-Length"=>"27"}, @status=401, @sending_file=false, @blank=false,
@cv=#<MonitorMixin::ConditionVariable:0x000000070d1bf8 @monitor=#<ActionDispatch::TestResponse:0x000000070d1d38 ...>, @cond=#<Thread::ConditionVariable:0x000000070d1bd0>>, @committed=false, @sending=false, @sent=false, @content_type=#<Mime::Type:0x00000002af78f8 @synonyms=["application/xhtml+xml"], @symbol=:html, @string="text/html">, @charset="utf-8", @cache_control={:no_cache=>true}, @etag=nil>.success?`
to return true, got false
SOLUTION 这个测试不抛光(还),但至少现在通过。
describe "User API get 1 user object", type: :request do
it 'Get sends back one User object ' do
@env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Basic.encode_credentials(user,pw)
FactoryGirl.create(:user)
get "/api/1/user", {}, @env
JSON.parse(response.body)
expect(response).to be_success
expect(response.status).to eq 200
end
end
不错!肯定的是,标题设置有点奇怪。是的,我认为在请求中定义头部会更好。我改变了http认证的设置,但我仍然坚持。仍然访问被拒绝,我不知道为什么。我更新我的问题。谢谢! – theDrifter
您可以使用[let block](http://betterspecs.org/#let)在测试间共享标题以保持干爽;)而且,在您更新的代码中,您似乎并没有将标题变量作为get方法的一个参数?这将解释为什么访问仍然被拒绝。赞同或标记我接受的答案当然是欢迎:) – Rafe
感谢您的区块链接。我会把它弄干,然后让它运行。是的,我的眼睛现在几乎是平方。我真的忽视了通过请求的头部。 (;但仍然不会成功,我仍然得到错误访问被拒绝...... – theDrifter