【问题标题】:Rails responds with status code 406Rails 响应状态码 406
【发布时间】:2017-07-10 06:33:55
【问题描述】:

我一直在 Ruby on Rails 中测试我的函数。但是,在收到状态代码 406 后,测试(期望状态代码为 :success)失败。这是确切的失败日志:

Failure: Expected response to be a <:success>, but was <406>.
test_should_post_comment_through_token_successfully(CommentControllerTest)
test/functional/comment_controller_test.rb:271:in `block in <class:CommentControllerTest>'

我阅读了一些关于406 响应的信息,发现它代表“不可接受”。所以我尝试设置AcceptContent-TypeAccept-LanguageAccept-Charset 标头,但我没有运气。

这是我的测试代码:

test 'should post comment through token successfully' do
  @params = {
    id: 1,
    body: "Test Comment",
    username: "Bob"
  }

  @headers = {
    "Accept" => "application/json",
    "Accept-Language" => "en-US",
    "Accept-Charset" => "utf-8",
    "Content-Type" => "application/json",
    "Token" => "abcdefg12345"
  }

  get :create_by_token, @params, @headers
  assert_response :success
end

控制器内部的create_by_token函数:

def create_by_token
  @node = Node.find params[:id]
  @user = User.find_by_username params[:username]
  @body = params[:body]
  @token = request.headers['Token']
  p request.headers["Accept"]
  p request.headers["Content-Type"]
  p request.headers["Token"]

  if @user && @user.token == @token
    begin
      @comment = create_comment(@node, @user, @body)
      msg = {
        status: :created,
        message: "Created"
      }
      respond_to do |format|
        format.xml { render xml: msg.to_xml }
        format.json { render json: msg.to_json }
      end
    rescue CommentError
      msg = {
        status: :bad_request,
        message: "Bad Request"
      }
      respond_to do |format|
        format.xml { render xml: msg.to_xml }
        format.json { render json: msg.to_json }
      end
    end
  else
    msg = {
      status: :unauthorized,
      message: "Unauthorized"
    }
    respond_to do |format|
      format.xml { render xml: msg.to_xml }
      format.json { render json: msg.to_json }
    end
  end
end

我的路线:

post '/bot/comment.:format', to: 'comment#create_by_token'

我错过了什么重要的东西吗?我该如何解决这个问题?

我很乐意提供您需要的任何其他信息。

【问题讨论】:

  • 如果没有看到实际的控制器代码,我们不太可能提供帮助。
  • @mudasobwa 我也会添加相关功能。
  • @mudasobwa 完成。
  • 我不知道您的具体问题,但我建议您通过调试自己开始挖掘问题的根源。安装pry-byebug(这是我用于rails应用程序的调试器,还有其他解决方案)并使用binding.pry设置[debug]断点。以自上而下的方式开始设置断点,如果您不能在第一眼(-> if 子句)中隔离有问题的部分,从而逐步分析通过 [控制器] 代码的流程。大多数情况下,这比在 SO 上发布并等待回复要快得多。希望对你有帮助,祝你好运
  • 谢谢@A.Neumann。我一定会尝试的。

标签: ruby-on-rails ruby http http-status-code-406 actiondispatch


【解决方案1】:

似乎这可能是 respond_to do 块的错误。请检查您是否已配置为资源或资源的路线。

更新资源而不是单数,这将有助于 response_to 阻止。

您也可以尝试将路线更新为/;

resources :samples, defaults: {format: :json}

【讨论】:

  • 我在上面添加了我的路线。请检查是否有问题。
【解决方案2】:

哦,我傻了。我意识到在我传递的所有参数中,格式也在 URL 中传递。但是,正如在测试中我没有提到可以以格式作为后缀(.xml.json)传递的 URL,我必须在参数中明确提及 format。这是更新后的测试代码:

test 'should post comment through token successfully' do
  @params = {
    id: 1,
    body: "Test Comment",
    username: "Bob",
    format: 'json'
  }

  @headers = {
    "token" => "abcdefg12345"
  }

  post :create_by_token, @params, @headers
  assert_response :success
end

感谢@Sowmiya 让我得出这个结论。你的回答并不完全是我需要的解决方案,但它激发了我的思考。

【讨论】:

    猜你喜欢
    • 2019-10-17
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 2020-10-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多