注意:“对象”是占位符作品,因为我认为我不应该说控制器具体做什么。
所以,我有多种调用我的应用程序 API 的方法,以下在命令行中有效:
curl -H 'Content-Type: application/xml' -d '<object><name>Test API object</name><password>password</password><description>This is a test object</description></object>' "http://acme.example.dev/objects.xml?api_key=1234"
上述命令在 devlog 中生成以下请求:
Processing ObjectsController#create to xml (for 127.0.0.1 at 2011-07-07 09:17:51) [POST]
Parameters: {"format"=>"xml", "action"=>"create", "api_key"=>"1234", "controller"=>"objects",
"object"=>{"name"=>"Test API object", "description"=>"This is a test object", "password"=>"[FILTERED]"}}
现在,我正在尝试使用 API 为操作编写测试,以确保 API 以及控制器正常工作。这是我当前的(损坏的)httparty 命令:
response = post("create", :api_key => SharedTest.user_api_key, :xml => data, :format => "xml")
此命令在测试日志中生成以下请求:
Processing ObjectsController#create to xml (for 0.0.0.0 at 2011-07-07 09:37:35) [POST]
Parameters: {
"xml"=>"<object><name><![CDATA[first post]]></name>
<description><![CDATA[Things are not as they used to be]]></description>
<password><![CDATA[WHEE]]></password>
</object>",
"format"=>"xml",
"api_key"=>"the_hatter_wants_to_have_tea1",
"action"=>"create",
"controller"=>"objects
因此,如您所见,命令行命令实际上是从 xml 生成对象散列,而 httparty 命令最终停留在 xml 中,这会导致 create 方法出现问题,因为它需要散列。
任何想法/适当的文档?当前文档说该帖子需要一个网址和“选项”,然后从不说明可用的选项
* *编辑:
根据@Casper 的建议,我的方法现在看起来像这样:
def post_through_api_to_url(url, data, api_key = SharedTest.user_api_key)
response = post("create", {
:query => {
:api_key => api_key
},
:headers => {
"Content-Type" => "application/xml"
},
:body => data
})
ap @request.env["REQUEST_URI"]
assert_response :success
return response
end
不幸的是,assert_response 失败了,因为通过 api 密钥的身份验证失败。查看 request_uri 的内容,api_key 设置不正确......它显示:
api_key%5D=the_hatter_wants_to_have_tea1"
但它应该只是等于,没有 %5D (右方括号)