http接口测试工具——RESTClient
摘要: RESTClient是用java Swing编写的基于http协议的接口测试工具,工具比较灵巧,便于做接口的调试,源码在官网上可以下到,感兴趣的可以研究一下
WizTools.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/posts?user=subwiz&resourcetype=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:
<?xml version="1.0"?>
<bibsonomy>
<post description="WizTools.org OpenSource">
<user name="subwiz"/>
<tag name="opensource"/>
<tag name="java"/>
<group name="public"/>
<bookmark url="http://wiztools.org/" title="WizTools.org"/>
</post>
</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/users/subwiz/posts/03fb1f5d1634e4bd72e6ccdc0cdafcc2
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:
<?xml version="1.0"?>
<bibsonomy>
<post description="WizTools.org OpenSource">
<user name="subwiz"/>
<tag name="opensource"/>
<tag name="java"/>
<tag name="REST"/>
<bookmark url="http://wiztools.org/" title="WizTools.org--OpenSource Software"
intrahash="03fb1f5d1634e4bd72e6ccdc0cdafcc2"
interhash="03fb1f5d1634e4bd72e6ccdc0cdafcc2"/>
</post>
</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 performOperation 1 to validate.
Operation 4: Delete post
The final operation is the DELETE operation. The URL for this is:
http://www.bibsonomy.org/api/users/subwiz/posts/03fb1f5d1634e4bd72e6ccdc0cdafcc2
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.
Topics:
原文地址:https://dzone.com/articles/wiztoolsorg-restclient-21-rele