Functional testing of a RESTful POST in Ruby on Ra

2019-03-27 18:46发布

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

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.

5条回答
叛逆
2楼-- · 2019-03-27 19:07

The following worked for me:

@request.env['RAW_POST_DATA'] = MY_XML_STRING
post :create   
查看更多
霸刀☆藐视天下
3楼-- · 2019-03-27 19:12

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

查看更多
仙女界的扛把子
4楼-- · 2019-03-27 19:14

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

查看更多
闹够了就滚
5楼-- · 2019-03-27 19:20

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

查看更多
狗以群分
6楼-- · 2019-03-27 19:22

I found the following solution at 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.

查看更多
登录 后发表回答