Testing with Web API Tester
Last Updated: March 2020
If you created 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;
Three auth models (basic authentication, bearer token, and OAuth 2.0) for authenticating the requests;
Enabling cookies in the requests and responses;
Ability to set query, and path parameters with your request URL. Query and path parameters are added to the request URL: query parameters follow "?" and query parameters follow ":"
Ability to send data models (such as .NET DataStore or DataPacker) as body parameters in requests.
The Web API Tester can support complicated data models as testing parameters in Web API requests that perform CRUD operations.
Configuring the Web API Tester
Use the Tools > Options > Web API Tester to configure how the Web API Tester shall behave. You may configure the General options and Proxy options.
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 any .cs file in the project you would like to perform a Web API test;
Right mouse click in the code block of the Web API method, and select Run Test(s) if you want to perform the Web API testing in normal mode, or Debug Test(s) if you want to perform the Web API Testing in debug mode.
Wait a short while. The Web API Tester will be launched, with the left sidebar listing all the Web API methods contained in the project, and the historical Web APIs you have tested.
Select from the sidebar the Web API you would like to test.
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.
It is more complicated to specify a body parameter. For detailed instructions, 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 the Send button 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.
|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).|