来自 Cucumber 的 HTTP POST XML 内容

发布于 2024-08-11 07:28:44 字数 1090 浏览 2 评论 0原文

我正在尝试通过 POST 将 XML 内容发送到一个简单的 Rails 项目中的控制器(“解析”)方法(“索引”)。它不是 RESTful,因为我的型号名称不同,例如“汽车”。我在一个有效的功能测试中有以下内容:

def test_index
   ...
   data_file_path = File.dirname(__FILE__) + 
        '/../../app/views/layouts/index.xml.erb'

   message = ERB.new( File.read( data_file_path ) )
   xml_result = message.result( binding )
   doc = REXML::Document.new xml_result

   @request.env['RAW_POST_DATA'] = xml_result
   post :index
   assert_response :success
end

我现在正在尝试 cucumber (0.4.3),并且想知道如何在“When”子句中模拟 POST 请求。我只有一种控制器方法“index”,并且在 config/routes.rb 中有以下内容:

ActionController::Routing::Routes.draw do |map|
  map.connect ':controller/:action/:id'
  map.connect ':controller/:action/:id.:format'
end
  1. cucumber 中的 webrat 仅适用于 HTML,并且不能执行 POST?
  2. @request 变量在 Cucumber 环境中不可用?
  3. 如果我在 features/step_definitions/car_steps.rb 中使用类似“访问索引”的内容(假设它是 Parse 控制器,索引方法),则会出现以下错误:

# 未定义方法“index”(NoMethodError)

感谢有关如何使用 Cucumber 对带有 XML 内容的 HTTP POST 进行集成测试的任何建议。

I am trying to sending XML content through POST to a controller ('Parse') method ('index') in a simple Rails project. It is not RESTful as my model name is different, say, 'cars'. I have the following in a functional test that works:

def test_index
   ...
   data_file_path = File.dirname(__FILE__) + 
        '/../../app/views/layouts/index.xml.erb'

   message = ERB.new( File.read( data_file_path ) )
   xml_result = message.result( binding )
   doc = REXML::Document.new xml_result

   @request.env['RAW_POST_DATA'] = xml_result
   post :index
   assert_response :success
end

I am now trying cucumber (0.4.3), and would like to know as to how I can simulate the POST request in a "When" clause. I have only one controller method 'index', and I have the following in config/routes.rb:

ActionController::Routing::Routes.draw do |map|
  map.connect ':controller/:action/:id'
  map.connect ':controller/:action/:id.:format'
end
  1. webrat within cucumber is only for HTML, and cannot do POST?
  2. @request variable is not available from cucumber environment?
  3. If I use something like 'visit index' (assuming it is Parse controller, index method) in features/step_definitions/car_steps.rb, I get the following error:

undefined method `index' for # (NoMethodError)

Appreciate any suggestions on how to do integration tests with Cucumber for HTTP POST with XML content.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

苹果你个爱泡泡 2024-08-18 07:28:44

Patrick Ritchie 的解决方案也帮了我的忙,但我需要稍作修改才能使其与 Rails 3 兼容。

post("/controller/index", xml_result, {"CONTENT_TYPE" => "text/xml"})

我认为这是因为在 v3 中,Rails 与 Rack 的集成更加紧密。

Patrick Ritchie's solution helped me out too, but I needed to make a slight modification to make it work with Rails 3.

post("/controller/index", xml_result, {"CONTENT_TYPE" => "text/xml"})

I think this is because, in v3, Rails is more tightly integrated with Rack.

终陌 2024-08-18 07:28:44

Webrat 在这里不会为您提供帮助,它用于基于浏览器的交互,因此如果您指定 API 它不会有帮助。

您可以在 Cucumber 中使用“post”,但您需要提供操作的完整路径,而不仅仅是操作。另外,传递 Content-type 标头,以便 Rails 知道您正在传递 XML。

post("/controller/index", xml_result, {"Content-type" => "text/xml"})

在响应端,您可以执行以下操作:

response.should be_success

Webrat won't help you here, it's for browser based interactions so if you are specing an API it won't help.

You can use 'post' in Cucumber but you need to provide the full path to the action, not just the action. Also, pass in the Content-type header so Rails knows you are passing in XML.

post("/controller/index", xml_result, {"Content-type" => "text/xml"})

On the response side you can do the following:

response.should be_success
欢烬 2024-08-18 07:28:44

非常感谢大家,我一整天都在骂人。如果我以外的其他人正在寻找它,我将添加一个带有基本身份验证和 json 的示例。顺便说一句,application/xmltext/xml 都可以,但对于 json,您需要 application/json

post("/myresource.xml", 
     some_xml_string,
     {"CONTENT_TYPE" => "text/xml",
      "HTTP_AUTHORIZATION" => ActionController::HttpAuthentication::Basic.encode_credentials("user", "secret")})

和 JSON

post("/myresource.json",
      some_json_string,
      {"CONTENT_TYPE" => "application/json",
       "HTTP_AUTHORIZATION" => ActionController::HttpAuthentication::Basic.encode_credentials("user", "secret")})

,我在 spec/requests 中使用它们,没有任何 webrat 或 capybara 浏览器内容。

Thanks a lot guys, I've spent all day swearing. I'll add an example with basic authentication and with json, if other people than me are looking for it. btw, both application/xml and text/xml works, but for json you need application/json.

post("/myresource.xml", 
     some_xml_string,
     {"CONTENT_TYPE" => "text/xml",
      "HTTP_AUTHORIZATION" => ActionController::HttpAuthentication::Basic.encode_credentials("user", "secret")})

and JSON

post("/myresource.json",
      some_json_string,
      {"CONTENT_TYPE" => "application/json",
       "HTTP_AUTHORIZATION" => ActionController::HttpAuthentication::Basic.encode_credentials("user", "secret")})

and I use these in spec/requests without any webrat or capybara browser stuff.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文