2009-04-15 94 views
12

我想在我的Rails站点上测试REST api。使用rails测试框架做到这一点最简单/最好的方法是什么?我只是在做标准的资源丰富的东西,所以我特别想知道,因为如果有任何自动手段来测试这些东西,那么这就是标准。测试Rails REST XML API的最佳方式是什么?

+1

你是说如何使用某种工具向URL发出测试请求?还是你说的是创建某种自动单元测试? – laz 2009-04-17 18:57:20

回答

2

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

0

如果您正在测试手动创建的API,您可能需要试试这个!似乎运作良好!

REST Client - Simply Test REST APIs

你不能做这个的自动化测试,但!

+0

如果URI作为API的一部分提供,那么这并不是真正的REST。在RESTful API中只能有入口点URI。 – aehlke 2009-08-17 20:30:22

2

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

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

+1

我会告诫他们为授权请求提供用户名/密码。我建议在这种情况下抓住代码并安装以供私人使用。 https://github.com/defunkt/hurl – 2011-01-26 04:07:36

1

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

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

我们已经在我的团队一直在使用这个一两个月的,我不认为我们可以做我们的工作离不开它。起床和跑步非常容易,而且使用起来很方便。

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

的使用Firefox插件的很多优势

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

一个,就是它的跨plattform。即使我们使用不同的操作系统(Mac,Linux,Windows),我们也会为我们团队的所有成员使用相同的工具(RESTclient)。

1

您可以尝试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 "[email protected];type=application/msword;"

(2)

curl -F "[email protected]_experiments_with_truth.pdf;type=application/pdf;" --form-string "author=Gandhi" --form-string "[email protected]" -X PUT 'http://localhost:3000/api/submit_a_pdf_book?params1=value1&param2=value2' 

在控制器中你会得到params['email]="[email protected]"params["author"]="Gandhi"params["document"] = "File(object)"。只有当test.doc在当前目录中时,这才起作用。不要忘记通过mime-type作为服务器可能会把它作为"application octet-stream",并需要写入代码来处理这个分开。

5

我推出了自己的解决方案,并认为这会有所帮助。我编写了一个模块,它使用json,curbaddressable宝石发送GET,PUT,POST和DELETE请求到localhost: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

这里有一些简单的例子:

api_call("calls/41", :get)

创建资源与POST:

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

更新资源,GET 请求资源属性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"})
+1

为什么不把它放到一个可以在测试过程中使用的gem中? – masukomi 2011-05-20 17:51:42

相关问题