代码之家  ›  专栏  ›  技术社区  ›  jcnnghm

测试Rails REST XML API的最佳方法?

  •  12
  • jcnnghm  · 技术社区  · 16 年前

    我想在我的Rails站点上测试其余的API。对于Rails测试框架,最简单/最好的方法是什么?我只做标准资源的东西,所以我特别想知道,因为这是如此糟糕的标准,如果有任何自动的方法来测试这些东西。

    6 回复  |  直到 16 年前
        1
  •  2
  •   Ariejan    15 年前

    我建议你用黄瓜。Cucumber模拟浏览器,您可以验证它得到的结果。这对于XML请求以及JSON和普通的旧HTML都很好。

        2
  •  5
  •   jrouquie    12 年前

    我提出了自己的解决方案,认为这会有所帮助。我编写了一个使用 json , curb addressable gems向localhost发送get、put、post和delete请求:3000。它可以请求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" } })

    使用“删除”删除资源:

        api_call("calls/41", :delete)

    关闭自动插入api_密钥:

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

    使用错误的api_密钥:

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

    用作JSON(默认为XML):

        api_call("calls/41", :get, {}, {:format => "json"})
        3
  •  2
  •   jluebbert    15 年前

    这不是自动化的,但是它对于了解您的API在做什么是非常好的。

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

        4
  •  1
  •   Jonas Söderström    15 年前

    我们使用一个firefox附加组件restclient来访问和测试rest服务。

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

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

    如果您从sourceforge获得最新版本,甚至还有对它的OAuth支持,这是我在任何其他REST客户机中都没有发现的。

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

    使用火狐插件的一个优点是它是跨平台的。我们对团队的所有成员使用相同的工具(restclient),即使我们使用不同的操作系统(Mac、Linux、Windows)。

        5
  •  1
  •   prayagupa soxunyi    11 年前

    你可以试试 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']=BOOK1234 params["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”]=“甘地” params["document"] = "File(object)" . 只有当 test.doc 在当前目录中。别忘了传球 mime-type 因为服务器可能认为 "application octet-stream" 需要编写代码来单独处理这个问题。

        6
  •  0
  •   Dasharatham Bitla    16 年前

    如果您正在测试手工创建的API,您可能需要尝试一下!似乎工作得很好!

    REST Client - Simply Test REST APIs

    但是,您不能用它来进行自动测试!