REST API testing using the Postman Chrome extension

This book uses the Postman – REST Client extension for Chrome to test our REST service. We use the 0.8.4.16 version that can be downloaded using https://chrome.google.com/webstore/detail/postman-rest-client/fdmmgilgnpjigdojojpjoooidkmcomcm. This extension is no longer searchable but you can add it to your Chrome using the given link. You can also use the Postman Chrome app or any other REST Client to test your sample REST application:

REST API testing using the Postman Chrome extension

Postman – Rest Client Chrome extension

Let's test our first REST resource once you have the Postman – REST Client installed. We start the Postman – REST Client from either the start menu or from a shortcut.

Tip

By default the embedded web server starts on port 8080. Therefore, we need to use the http://localhost:8080/<resource> URL for accessing the sample REST application. Example: http://localhost:8080/calculation/sqrt/144.

Once it's started, you can type the Calculation REST URL for sqrt and value 144 as the path parameter. You could see it in the following image. This URL is entered in the URL (Enter request URL here) input field of the Postman extension. By default, the request method is GET. We use the default value for the request method, as we have also written our RESTful service to serve the request GET method.

Once you are ready with your input data as mentioned above, you can submit the request by clicking the Send button. You can see in the following image that the response code 200 is returned by your sample rest service. You can find the Status label in the following image to see the 200 OK code. A successful request also returns the JSON data of the Calculation Resource, shown in the Pretty tab in the screenshot. The returned JSON shows the sqrt, value of the function key. It also displays 144 and 12.0 as the input and output lists respectively:

REST API testing using the Postman Chrome extension

Calculation (sqrt) resource test with Postman

Similarly, we also test our sample REST service for the calculating power function. We input the following data in the Postman extension:

  • URL: http://localhost:8080/calculation/power?base=2&exponent=4
  • Request method: GET

Here, we are passing the request parameters base and exponent with values of 2 and 4 respectively. It returns the following JSON with a response status of 200 as shown in the following screenshot.

Returned JSON:

{
    "function": "power",
    "input": [
        "2",
        "4"
    ],
    "output": [
        "16.0"
    ]
}
REST API testing using the Postman Chrome extension

Calculation (power) resource test with Postman

Some more positive test scenarios

In the following table, all the URLs start with http://localhost:8080:

URL

Output JSON

/calculation/sqrt/12344.234

{
    "function": "sqrt",
    "input": [
        "12344.234"
    ],
    "output": [
        "111.1046083652699"
    ]
}

/calculation/sqrt/-9344.34

Math.sqrt function's special scenario:

  • If the argument is NaN or less than zero, then the result is NaN
{
    "function": "sqrt",
    "input": [
        "-9344.34"
    ],
    "output": [
        "NaN"
    ]
}

/calculation/power?base=2.09&exponent=4.5

{
    "function": "power",
    "input": [
        "2.09",
        "4.5"
    ],
    "output": [
        "27.58406626826615"
    ]
}

/calculation/power?base=-92.9&exponent=-4

{
    "function": "power",
    "input": [
        "-92.9",
        "-4"
    ],
    "output": [
        "1.3425706351762353E-8"
    ]
}

Negative test scenarios

Similarly, you could also perform some negative scenarios as shown in the following table. In this table, all the URLs start with http://localhost:8080:

URL

Output JSON

/calculation/power?base=2a&exponent=4

{
    "function": "power",
    "input": [
        "2a",
        "4"
    ],
    "output": [
        "Base or/and Exponent is/are not set to numeric value."
    ]
}

/calculation/power?base=2&exponent=4b

{
    "function": "power",
    "input": [
        "2",
        "4b"
    ],
    "output": [
        "Base or/and Exponent is/are not set to numeric value."
    ]
}

/calculation/power?base=2.0a&exponent=a4

{
    "function": "power",
    "input": [
        "2.0a",
        "a4"
    ],
    "output": [
        "Base or/and Exponent is/are not set to numeric value."
    ]
}

/calculation/sqrt/144a

{
    "function": "sqrt",
    "input": [
        "144a"
    ],
    "output": [
        "Input value is not set to numeric value."
    ]
}

/calculation/sqrt/144.33$

{
    "function": "sqrt",
    "input": [
        "144.33$"
    ],
    "output": [
        "Input value is not set to numeric value."
    ]
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset