51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 3829|回复: 0
打印 上一主题 下一主题

[转贴] restclient[转]

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2011-2-23 07:46:07 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
WizTools.org RESTClient 2.1—Getting Started
Submitted by Subhash Chandran on Mon, 2008/04/07 - 12:37am
     Tags: OpenSource REST RESTClient
Ads by DZoneWizTools.org RESTClient is a Java Swing application for testing RESTful web services. It might be used for testing other HTTP communications too. This is a short introduction of this tool explaining its various features.

The First Step

RESTClient can be downloaded from http://code.google.com/p/rest-client/downloads/list. It is a single Jar file. To run it, you need to have Java 5 installed. After downloading, Windows users need to double-click to start the application. Non-Windows users, fire up your command prompt/shell, and issue:

$ java -jar restclient-2.1-jar-with-dependencies.jar

The UI

Once it is started, you will see this UI:



As seen in the screenshot, the UI is divided into two sections. The top portion has the HTTP request details and the bottom portion has the HTTP response details. To get started, just enter the value “http://wiztools.org/” in the URL field. Now press the green “>>” button near to the URL field. You should find the response details in the Response section of the UI.

RESTful Web services Example

Now we will use RESTClient to learn how it can be used to test a RESTful web services. We will take the example of social bookmarking site BibSonomy.org for example. If you do not have an account with this site, please sign up for a free account. The REST API of this service is documented in this page: http://www.bibsonomy.org/help/doc/api.html. For using this service, you have to request API key here: http://www.bibsonomy.org/help/doc/gettingaccess.html.

Once you get the API key, you are ready to start! Fire up RESTClient. We will do the following operations on BibSonomy.org:

List all the posts you made
Create a post (bookmark entry)
Change post
Delete post
Before we start trying out the features of BibSonomy.org, you have to understand the authentication mechanism of this site. In the authentication tab, make sure you give:



BASIC authentication method should be selected.
Preemptive can be selected (not mandatory). Having this selected will send the authentication details to the server even before the server challenges for it.
Give your BibSonomy username and API key as the password.
Now we jump on to the operations. All the operations will require that this authentication detail to be configured.

Operation 1: List all the posts you made

To list all the posts you have made, enter this URL in the URL box:

http://www.bibsonomy.org/api/pos ... sourcetype=bookmark

(Note: Replace all mentions of `subwiz' with your BibSonomy.org userid. `subwiz' is the author's BibSonomy.org userid.)

Make sure you have entered the authentication details as mentioned. Now with the method entered as GET, hit the green “>>” button.



As you see in the body tab of the Response section, it has returned an XML saying that this user does not have any posts yet. We will be using Operation 1 further down in the tutorial to see the status of our additions, updations and deletions.

Operation 2: Create a post

To create a post, you have to hit this URL:

http://www.bibsonomy.org/api/users/subwiz/posts

The HTTP method to be used is POST for create operation. Make sure you select this option in the Method tab.



After selecting the POST method, the Body tab controls get enabled. Body is supported only for POST and PUT methods. In the Body tab, add the bookmark post detail in the following format:

view sourceprint?01.<?xml version="1.0"?>
02.<bibsonomy>
03.  
<post description="WizTools.org OpenSource">
04.   
<user name="subwiz"/>
05.   
<tag name="opensource"/>
06.   
<tag name="java"/>
07.   
<group name="public"/>
08.   
<bookmark url="http://wiztools.org/" title="WizTools.org"/>
09.  
</post>
10.</bibsonomy>
Make sure you change the content-type to application/xml and charset to UTF-8 (click the Edit button near to the content-type/charset text field to change). Finally, it should look like:



Now execute the request by pressing the “>>” button. The response screen should be something similar to:



and:



Response status 201 means success. Make note of the hash returned (which in our case is: 03fb1f5d1634e4bd72e6ccdc0cdafcc2) in the body of the response. This will be needed for the next operation. Now perform Operation 1 to see your new post getting displayed.

Operation 3: Change post

The URL for updating a existing post is:

http://www.bibsonomy.org/api/use ... 4bd72e6ccdc0cdafcc2

For updating a post with new/changed data, HTTP PUT method is used. Select PUT from the Methods tab, and enter in the body text:

view sourceprint?01.<?xml version="1.0"?>
02.<bibsonomy>
03.  
<post description="WizTools.org OpenSource">
04.   
<user name="subwiz"/>
05.   
<tag name="opensource"/>
06.   
<tag name="java"/>
07.   
<tag name="REST"/>
08.   
<bookmark url="http://wiztools.org/" title="WizTools.org--OpenSource Software"
09.              
intrahash="03fb1f5d1634e4bd72e6ccdc0cdafcc2"
10.              
interhash="03fb1f5d1634e4bd72e6ccdc0cdafcc2"/>
11.  
</post>
12.</bibsonomy>
As you see, we have added a new tag REST and updated the title. Also remember to set the Content-type and charset to application/xml and UTF-8 respectively. Execute the request, and perform Operation 1 to validate.

Operation 4: Delete post

The final operation is the DELETE operation. The URL for this is:

http://www.bibsonomy.org/api/use ... 4bd72e6ccdc0cdafcc2

From the Methods tab, select DELETE. If the DELETE operation is successful, the Response status code will be 200 OK. Perform Operation 1 to validate.



Testing Using RESTClient

Another interesting feature in RESTClient is the ability to attach test scripts written in Groovy with each request. More on this is covered in the tutorial at the site.

Persisting Request et all

RESTClient also supports persisting request, response and request-response combination. This helps in record and playback support. These options are available in the File menu.

What Else?

RESTClient has been developed with active input from the REST-Discuss community. If you have any request (both bugs and features), just file a issue.
http://java.dzone.com/announceme ... -restclient-21-rele
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏
回复

使用道具 举报

本版积分规则

关闭

站长推荐上一条 /1 下一条

小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

GMT+8, 2024-5-25 03:28 , Processed in 0.073867 second(s), 28 queries .

Powered by Discuz! X3.2

© 2001-2024 Comsenz Inc.

快速回复 返回顶部 返回列表