开发者

Ruby: HTTParty: can't format XML POST data correctly?

NOTE: "object" is a placeholder work, as I don't think I should be saying what the controller does specifically.

so, I have multiple ways of calling my apps API, the following works in the command line:

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"

the above command generates the following request in the 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]"}}

Now, I'm trying to write tests for the actions using the API, to make sure the API works, as well as the controllers. Here is my current (broken) httparty command:

  response = post("create", :api_key => SharedTest.user_api_key, :xml => data, :format => "xml")

this command generates the following request in the testlog:

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

So, as you can see, the command line command actually generates the object hash from the xml, whereas the httparty command ends up staying in xml, which causes problems for the create method, as it needs a hash.

Any ideas / proper documentation? Current documentation says that post takes an url, and "options" and then never says what options are available


**EDIT:

as per @Casper's suggestion, my method now looks like this:

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

unfortunately, the assert_response fails, because the authentication via the api key fails. looking at the very of of the request_uri, the api_key isn't being set properly... it shows:

api_key%5D=the_hatter_wants_to_have_tea1"

but it should just be equals, without the %5D (right square bracket)


I think this is how you're supposed to use it:

options = {
  :query => {
    :api_key => 1234
  },

  :headers => {
    "Content-Type" => "application/xml"
  },

  :body => "<xmlcode>goes here</xmlcode>"
}

post("/create", options)


Forgive me for being basic about it but if you only want to send one variable as a parameter, why don't you do as Casper suggests, but just do:

post("/create?api_key=1234", options)

Or rather than testing HTTParty's peculiarities in accessing your API, perhaps write your tests using Rack::Test? Very rough example...

require "rack/test"
require "nokogiri"

class ObjectsTest < Test::Unit::TestCase
  include Rack::Test::Methods

  def app
    MyApp.new
  end

  def create_an_object(o)
    authorize "x", "1234" # or however you want to authenticate using query params
    header 'Accept', 'text/xml'
    header 'Content-Type', 'text/xml'
    body o.to_xml
    post "/create"

    xml = Nokogiri::XML(last_response.body)
    assert something_logic_about(xml)
  end

end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜