简体   繁体   English

如何使用xml作为输入来测试Rails RESTful API发布请求?

[英]How can I test a Rails RESTful API post request with xml as input?

I am adding a RESTful API to the Rails Tutorial sample app. 我正在向Rails教程示例应用程序添加RESTful API。 The controller actions should take XML as input and respond with XML. 控制器动作应将XML作为输入并以XML响应。 I am trying to follow a TDD approach but have been unable to find a definitive method of making a post request. 我正在尝试遵循TDD方法,但是无法找到发出后期请求的确定方法。 Here are my tests, as best as I've written them so far: 这是我到目前为止所写的最好的测试:

it "should increment the Relationship count" do
  expect do
    # valid_xml = "<xml><:followed_id>#{other_user.id}</:followed_id></xml>"
    valid_xml = { followed_id: other_user.id }.to_xml
    post :create, relationship: valid_xml, format: 'xml', content_type: 'application/xml'
  end.to change(Relationship, :count).by(1)
end

it "should respond with success" do
  # valid_xml = "<xml><:followed_id>#{other_user.id}</:followed_id></xml>"
  valid_xml = { followed_id: other_user.id }.to_xml
  post :create, relationship: valid_xml, format: :xml, content_type: 'application/xml'
  expect(response).to be_success
end

This particular test is verifying that posting XML will create a new Relationship. 此特定测试正在验证发布XML将创建新的Relationship。 As you can see, I have tried several ways to specify that the input is XML. 如您所见,我尝试了几种方法来指定输入为XML。 Here is the controller action: 这是控制器动作:

class RelationshipsController < ApplicationController
  before_action :signed_in_user
  respond_to :html, :js, :xml 

  def create
    # Problem is that the xml is not being read as such; it's a string
    relationship = params[:relationship]
    puts relationship
    puts relationship[:followed_id]
    @user = User.find(relationship[:followed_id])
    current_user.follow!(@user)
    respond_with @user 
  end

For the given tests, the controller prints out relationship as XML but errors on the following line, where it attempts to get the value of key :followed_id . 对于给定的测试,控制器将relationship打印为XML,但在下一行显示错误,并尝试获取key :followed_id的值。 The error is TypeError: no implicit conversion of Symbol into Integer for both tests. 错误是TypeError: no implicit conversion of Symbol into Integer两个测试都TypeError: no implicit conversion of Symbol into Integer

I assume that this means that the type of relationship is a string, not a hash, and Ruby thinks that the bracket is supposed to be an index. 我认为这意味着relationship的类型是字符串,而不是哈希,并且Ruby认为括号应该是索引。 Does anyone know where I am going wrong, either in the test or the controller action? 在测试或控制器操作中,有人知道我要去哪里哪里吗?

Are there any better ways to test a RESTful API or gems I should use? 有没有更好的方法来测试我应该使用的RESTful API或gem?

The XML parameters parser was removed from core in Rails 4.0. XML参数解析器已从Rails 4.0的核心中删除。 This was due to several vulnerabilities and the fact that most API´s worth using today are JSON or moving towards JSON . 这是由于存在多个漏洞,以及当今大多数值得使用的API是JSON或向JSON过渡的事实。

The removed functionality is available from the actionpack-xml_parser gem. 可从actionpack-xml_parser gem获得已删除的功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM