【问题标题】:POST Authorization trouble: no implicit conversion of nil into StringPOST 授权问题:没有将 nil 隐式转换为 String
【发布时间】:2019-09-17 12:17:35
【问题描述】:

有时我在运行测试时会收到此错误: TypeError: no implicit conversion of nil into String,是由这段代码引起的:

params = {
  .....
}

auth_headers = {
  'Authorization': "Basic #{Base64.encode64('XXX:XXX')}"
}

post back_request_url, params: params, headers: auth_headers

back_request_url 路径有效,参数相同。当授权失败时,我得到(应该是)只是401 - 但是,当授权通过时 - 就会发生这个错误。

这是 rails 中的内容 - 此操作不会进入 back_request_url 提供的操作。

可能是什么? 我的rails版本是5.1.3

【问题讨论】:

  • 请为此请求提供完整的服务器日志
  • 这只是一个测试,但我已经解决了这个问题:)

标签: ruby-on-rails ruby ruby-on-rails-5 authorization basic-authentication


【解决方案1】:

#FIXED

问题 - 测试开始时已设置授权参数(名称和密码):

test 'failure no id' do
  MyGem.auth_name = 'xxx'
  MyGem.auth_pass = 'xxx'
  params = {
    ...
    ....
    (no id)
  }

  post back_request_url, params: params, headers: @auth_headers
  assert_response 400
end

以及行动:

class BackRequestController < ApplicationController
    protect_from_forgery except: [:new]
    http_basic_authenticate_with name: MyGem.auth_name, password: MyGem.auth_pass

...

当测试开始时,它们有时并没有设置和刷新。

解决方案

简单:)

class BackRequestControllerTest < ActionDispatch::IntegrationTest
  include Engine.routes.url_helpers

  setup do
    MyGemauth_name = 'xxx'
    MyGem.auth_pass = 'xxx'
    @auth_headers = {
      'Authorization': "Basic #{Base64.encode64(MyGem.auth_name. + ':' + MyGem.auth_pass)}"
    }
  end

...
...
...
(TESTS)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-14
    • 2016-11-25
    • 1970-01-01
    • 1970-01-01
    • 2019-02-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多