Ruby on Rails中RESTful POST的功能测试

时间:2021-02-20 03:53:11

I'd like to write a functional test of a RESTful web service I'm working on in a Ruby on Rails app.

我想编写一个RESTful Web服务的功能测试,我正在使用Ruby on Rails应用程序。

The test is of a POST request where the body of the request is a plain XML doc and not a form. Any pointers on how to do this? The problem I'm encountering is how to specify the body XML in the call to the post method.

测试是POST请求,其中请求的主体是纯XML文档而不是表单。有关如何做到这一点的任何指示?我遇到的问题是如何在post方法的调用中指定body XML。

5 个解决方案

#1


8  

The following worked for me:

以下对我有用:

@request.env['RAW_POST_DATA'] = MY_XML_STRING
post :create   

#2


1  

You may be able to do it by setting @request.env['RAW_POST_BODY'] to the desired input stream.

您可以通过将@ request.env ['RAW_POST_BODY']设置为所需的输入流来实现。

#3


0  

I found the following solution at http://de.softuses.com/6051

我在http://de.softuses.com/6051找到了以下解决方案

message = '<?xml version="1.0" encoding="UTF-8"?>
<tag>content</tag>'

@xml_request_headers ||= {}
@xml_request_headers['HTTP_ACCEPT'] = @xml_request_headers['CONTENT_TYPE'] = 'application/xml'

post '/controller/action.xml', message, @xml_request_headers

I actually defined @xml_request_headers in my setup method and can use it in all my tests in this file. Perhaps it would be a good idea to put it to test_helper.rb if it is required by more files.

我实际上在我的setup方法中定义了@xml_request_headers,并且可以在我的所有测试中使用它。如果更多文件需要它,将它放到test_helper.rb中也许是个好主意。

#4


-1  

Check out shoulda's "should_be_restful" macro. This macro will soon be deprecated from shoulda and only available in the in woulda gem.

查看shoulda的“should_be_restful”宏。这个宏很快就会从shoulda中弃用,只能在willa gem中使用。

#5


-1  

I just wrote a test script using Net:HTTP to test the REST service.

我刚刚使用Net:HTTP编写了一个测试脚本来测试REST服务。

#1


8  

The following worked for me:

以下对我有用:

@request.env['RAW_POST_DATA'] = MY_XML_STRING
post :create   

#2


1  

You may be able to do it by setting @request.env['RAW_POST_BODY'] to the desired input stream.

您可以通过将@ request.env ['RAW_POST_BODY']设置为所需的输入流来实现。

#3


0  

I found the following solution at http://de.softuses.com/6051

我在http://de.softuses.com/6051找到了以下解决方案

message = '<?xml version="1.0" encoding="UTF-8"?>
<tag>content</tag>'

@xml_request_headers ||= {}
@xml_request_headers['HTTP_ACCEPT'] = @xml_request_headers['CONTENT_TYPE'] = 'application/xml'

post '/controller/action.xml', message, @xml_request_headers

I actually defined @xml_request_headers in my setup method and can use it in all my tests in this file. Perhaps it would be a good idea to put it to test_helper.rb if it is required by more files.

我实际上在我的setup方法中定义了@xml_request_headers,并且可以在我的所有测试中使用它。如果更多文件需要它,将它放到test_helper.rb中也许是个好主意。

#4


-1  

Check out shoulda's "should_be_restful" macro. This macro will soon be deprecated from shoulda and only available in the in woulda gem.

查看shoulda的“should_be_restful”宏。这个宏很快就会从shoulda中弃用,只能在willa gem中使用。

#5


-1  

I just wrote a test script using Net:HTTP to test the REST service.

我刚刚使用Net:HTTP编写了一个测试脚本来测试REST服务。