Rspec Rails 4.2.5 使用基本的 http 身份验证请求测试通过
Rspec Rails 4.2.5 Request test pass with basic http auth
设置如下。对于每个 http 请求,经理在 header(name,pw) 中发送他的凭据。这些将根据数据库中的条目进行检查,如果它们成功 return 所需的用户 object。如何在请求测试中实现基本的 http_auth?我只想添加密码和用户名并测试 return 值?请求测试的目标是什么,对吗?我尝试了以下但没有成功:
我在 spec/support/auth_helper.rb
中使用
创建了一个 AuthHelper 模块
module AuthHelper
def http_login
user = 'test'
pw = 'test'
request.ENV['HTTP_AUTHORIZATION'] =ActionController::HttpAuthentication::Basic.encode_credentials(user,pw)
end
end
并在requests/api/user_spec.rb
中使用如下
include AuthHelper
describe "User API get 1 user object" do
before(:each) do
http_login
end
但我收到此错误消息。我该如何解决这个问题并让我的测试通过 http_auth?我也在这里阅读了很多类似的话题和问题,但是
它们主要适用于 rspec 和 rails 的旧版本,不适用于我的情况
提前致谢!
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)>'
更新
我在请求中移动了 header 代。我查找了 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
收到以下错误:
其中包含 @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
解决方案
这个测试还没有完善,但至少现在通过了。
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
这一行看起来很可疑:
request.ENV['HTTP_AUTHORIZATION'] =ActionController::HttpAuthentication::Basic.encode_credentials(user,pw)
你确定 "ENV"
应该大写吗?我觉得应该写成"env"
.
仔细阅读错误:undefined method `env' for nil:NilClass
表示request
为零。您是否尝试在 header before 测试中设置测试,而稍后在 测试中定义请求?
您可能需要查看有关如何设置 headers 的文档 for an example。
如果您仍然遇到困难,post您的一项测试也是如此。
设置如下。对于每个 http 请求,经理在 header(name,pw) 中发送他的凭据。这些将根据数据库中的条目进行检查,如果它们成功 return 所需的用户 object。如何在请求测试中实现基本的 http_auth?我只想添加密码和用户名并测试 return 值?请求测试的目标是什么,对吗?我尝试了以下但没有成功:
我在 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
并在requests/api/user_spec.rb
中使用如下
include AuthHelper
describe "User API get 1 user object" do
before(:each) do
http_login
end
但我收到此错误消息。我该如何解决这个问题并让我的测试通过 http_auth?我也在这里阅读了很多类似的话题和问题,但是 它们主要适用于 rspec 和 rails 的旧版本,不适用于我的情况
提前致谢!
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)>'
更新
我在请求中移动了 header 代。我查找了 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
收到以下错误:
其中包含 @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
解决方案 这个测试还没有完善,但至少现在通过了。
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
这一行看起来很可疑:
request.ENV['HTTP_AUTHORIZATION'] =ActionController::HttpAuthentication::Basic.encode_credentials(user,pw)
你确定 "ENV"
应该大写吗?我觉得应该写成"env"
.
仔细阅读错误:undefined method `env' for nil:NilClass
表示request
为零。您是否尝试在 header before 测试中设置测试,而稍后在 测试中定义请求?
您可能需要查看有关如何设置 headers 的文档 for an example。
如果您仍然遇到困难,post您的一项测试也是如此。