Testing with Web API Tester
Last Updated: July 2021
Introduction
If you create a Web API project in SnapDevelop, you can take advantage of a built-in Web API Tester for testing Web APIs locally during development. The Web API Tester provides you with:
A friendly GUI for constructing requests and reading responses;
Normal testing mode and Debug testing mode;
Various authorization schemas (basic authorization, bearer token, and OAuth 2.0);
Enabling cookies in the requests and responses;
Ability to send data models (such as .NET DataStore or DataPacker) as body parameters in requests.
The Web API Tester supports complicated data models as testing parameters in Web API requests that perform CRUD operations.
Configuring the Web API Tester
Configure how the Web API Tester shall behave by selecting Tools > Options > Web API Tester.
General Web API Tester settings
Under Web API Tester | General, you can configure how the built-in Web API Tester will behave when performing web API tests.
Allow redirection of requests
If you enable the option, and the web site that the Web API Tester sends requests to has configured redirection, the Web API Tester will still make the test request to the redirected URL.
Request timeout
You can configure the time interval between sending a web API test request and the request gets a timeout. Because a request usually takes longer time to complete in debug mode, you shall set the interval to a much longer period in debug mode than in normal mode.
Maximum response size
The setting is self-explanatory. If the response returned from a request exceeds certain size, the Web API Tester can either trigger a download prompt that asks you to download the response to a text file, or just throws an exception.
Store server cookies and use them when sending requests
This option means that the Web API Tester will actively store server cookies and use the cookies for the subsequent requests.
Proxy settings
Under Web API Tester | Proxy, you can configure how the Web API Tester will connect to the target server.
Configure global proxy settings
You can configure the global proxy settings to use by the Web API Tester when it sends requests to the server.
Use system proxy
If you want the Web API Tester to use the default proxy settings on your local machine, enable this option.
Performing a Web API Test
Let's see how to use the Web API Tester to send an HTTP request to a Web API and then check the response.
Open a .cs file in the project you would like to perform a Web API test.
Right click in the code block of a Web API method, and select Run Test(s) to perform the Web API testing in normal mode, or Debug Test(s) to perform the Web API Testing in debug mode.
Wait a short while till the Web API Tester is launched.
Select the Web API you would like to test from the sidebar.
If required, enter the parameter value for the Web API in the Params tab.
If it is a path or query parameter, you can directly add it to the Value field. For information about specifying a body parameter, see Sending Body Parameters in Requests.
Double check the headers in the Headers tab to make sure you will send correct headers along with the request.
If necessary, go to the Authorization tab and select an auth type. You will be prompted to complete the required authorization details for the selected type.
If necessary, go to the Cookies tab to add cookie(s) in the response. You may create a new cookie or import an existing cookie.
If you create a new cookie, you will be prompted to specify the basic cookie attributes:
Click Send to send the request to the provided URL. The response will be displayed in the Response panel with the response body (in Pretty, Raw, or Preview mode), response header, (if any) cookies, and overall response status.
Sending Body Parameters in Requests
If a parameter in a Web API is a data model (such as .NET DataStore or DataPacker), the parameter is a body parameter:
With the data model being the parameter, you need to decide two things: (1) Select the correct Content-Type header; (2) Specify the data to be included in the body data.
Selecting the Content-Type
You can select the correct Content-Type in either the Headers or Body tab.
The Content-Type determines how the Web API Tester will process the data that you specify for the parameter. For example, if you select the "application/json" Content-Type, the tester will export the data that you specify for the parameter into JSON, and then send the JSON data as part of the request to the server.
Specifying the Data for the Body Parameter
The Web API Tester provides a few options/buttons to assist you to specify the data for the body parameters. The table below shows you the functions of the options and icons.
Icon/Button | Description |
---|---|
Retrieves data into the body parameter. If you click the button, a Retrieve dialog will display. You can specify the request to execute, or provide an external file, so that the initial data can be loaded into the body parameter. | |
Filters the data that currently displays in the body parameter. If you click the button, a Filter dialog will display. You can specify the filter expression to execute on the current body parameter. | |
Adds a row. If you click the button, an empty row will be added into the current body parameter. | |
If the content type is application/JSON or application/XML, defines the mapping method for mapping columns in the body parameter to the target JSON/XML. There are three options: Index (using the index of the JSON/XML item, to map with the columns of body parameter); ColumnIndex (using the index defined in the meta-columns for the JSON/XML item to map with the columns of the body parameter); and Key (using the key of the JSON/XML item to map with the columns of body parameter). | |
Defines whether to only export the changed data from the body parameter to the target Content-Type. | |
Removes all the rows except the current one in the body parameter. | |
Removes only the current row in the body parameter. | |
Deletes the current row (moves it to the Delete buffer in the body parameter). |