【问题标题】:Best Way to Test Rails REST XML API?测试 Rails REST XML API 的最佳方法?
【发布时间】:2009-04-15 16:46:17
【问题描述】:

我想在我的 Rails 站点上测试 REST api。使用 rails 测试框架最简单/最好的方法是什么?我只是在做标准的足智多谋的东西,所以我特别想知道,因为这太标准了,是否有任何自动的方法来测试这些东西。

【问题讨论】:

  • 您的意思是如何使用某种工具向 URL 发出测试请求?或者您是在说创建某种类型的自动化单元测试?

标签: ruby-on-rails xml testing rest


【解决方案1】:

我为此推出了自己的解决方案,并认为它会有所帮助。我编写了一个模块,它使用 jsoncurbaddressable gem 向 localhost:3000 发送 GET、PUT、POST 和 DELETE 请求。它可以请求 XML(如原始问题所要求的)或 json。它将响应正文作为哈希返回。它主要是一个包装器,我认为它有可怕的语法。

请注意,我会自动加载我的api_key。这可以通过传递:api_key => false 禁用或使用api_key => "wrong" 破坏。您可能希望忽略它或对其进行修改以适合您的身份验证方案。

这是模块:

module ApiTesting
  # requres the json, curb, and addressable gems

  require "addressable/uri"

  def api_call(path, verb, query_hash={}, options={})
    options.reverse_merge! :api_key => "abc1234", :format => "xml"
    query_hash.reverse_merge!({:api_key => options["api_key"]}) if options[:api_key]
    query = to_query_string(query_hash)
    full_path = "http://localhost:3000/#{path}.#{options[:format]}?#{query}"
    response = case verb
      when :get
        Curl::Easy.perform(full_path)
      when :post
        Curl::Easy.http_post("http://localhost:3000/#{path}.#{options[:format]}", query)
      when :put
        Curl::Easy.http_put(full_path, nil)
      when :delete
        Curl::Easy.http_delete(full_path)
    end
    case options[:format]
      when "xml"
        Hash.from_xml(response.body_str)
      when "json"
        JSON.parse(response.body_str)
    end
  end

  private

  def to_query_string(val)
    uri = Addressable::URI.new
    uri.query_values = val
    uri.query
  end

end

这里有一些简单的例子: 使用 GET 请求资源属性:

    api_call("calls/41", :get)

使用 POST 创建资源:

    api_call("people", :post, {:person => {:first => "Robert", :last => "Smith" } })

使用 PUT 更新资源:

    api_call("people/21", :put, {:person => { :first => "Bob" } })

使用 DELETE 删除资源:

    api_call("calls/41", :delete)

关闭api_key的自动插入:

    api_call("calls/41", :get, {}, {:api_key => false})

使用错误的 api_key:

    api_call("calls/41", :get, {}, {:api_key => "wrong"})

用作json(默认为xml):

    api_call("calls/41", :get, {}, {:format => "json"})

【讨论】:

  • 为什么不把它整合成一个可以在测试期间使用的 gem?
  • 你绝对应该把它变成一颗宝石。干得好
【解决方案2】:

我会推荐使用 Cucumber。 Cucumber 模拟浏览器,您可以验证它得到的结果。这适用于 XML 请求以及 JSON 和普通的旧 HTML。

【讨论】:

    【解决方案3】:

    这不是自动化的,但它非常适合查看您的 API 正在做什么。

    http://hurl.r09.railsrumble.com/

    【讨论】:

    • 我会提醒他们为授权请求提供您的用户名/密码。在这种情况下,我建议获取代码并安装以供私人使用。 github.com/defunkt/hurl
    【解决方案4】:

    我们使用 RESTClient 一个 Firefox 插件来访问和测试 REST 服务。

    https://addons.mozilla.org/en-US/firefox/addon/9780

    我们已经在我的团队中使用它几个月了,我认为没有它我们就无法完成我们的工作。它很容易启动和运行,并且使用起来很友好。

    如果您从 Sourceforge 获得最新版本,甚至还有 Oauth 支持,这是我在任何其他 REST 客户端中都找不到的。

    http://sourceforge.net/projects/restclient/develop

    使用 Firefox 插件的众多优势之一是它是跨平台的。即使我们使用不同的操作系统(Mac、Linux、Windows),我们也会为团队的所有成员使用相同的工具(RESTclient)。

    【讨论】:

      【解决方案5】:

      你可以试试curl

      使用--form-string 将表单数据传递给服务器

      (1)

      curl --form-string "book_key=BOOK1234" --form-string "author=Gandhi"  -X PUT 'http://localhost:3000/api/show_all_books_of_a_particular_author?params1=value1&param2=value2'
      

      在控制器中你会得到params['book_key']=BOOK1234params["author"]="Gandhi"

      使用-F "document=@test.doc;type=application/msword;"

      (2)

      curl -F "document=@my_experiments_with_truth.pdf;type=application/pdf;" --form-string "author=Gandhi" --form-string "email=joe@mymail.com"  -X PUT 'http://localhost:3000/api/submit_a_pdf_book?params1=value1&param2=value2'
      

      在控制器中,您将获得 params['email]="joe@mymail.com"params["author"]="Gandhi"params["document"] = "File(object)" 。这仅在test.doc 在当前目录中时才有效。不要忘记传递mime-type,因为服务器可能会将其视为"application octet-stream",并且需要编写代码来单独处理。

      【讨论】:

        【解决方案6】:

        如果您正在考虑测试您手动创建的 API - 您可能想试试这个!似乎运作良好!

        REST Client - Simply Test REST APIs

        你不能用这个来做自动化测试!

        【讨论】:

        • 如果 URI 可以作为 API 的一部分使用,那么这并不是真正的 REST。 RESTful API 中只能有入口点 URI。
        猜你喜欢
        • 2011-10-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-04-08
        • 2014-01-16
        • 1970-01-01
        • 1970-01-01
        • 2020-12-25
        相关资源
        最近更新 更多