Menu Close
    Start a Project Request quote Reading list Switch to dark mode
    hyperlocal-marketplace-guide

    Fundamentals of API Testing

    What is API testing?

    Application Program Interface (API) is a set of commands used by an individual program to communicate with one another directly and use each other’s functions to get information and API testing is a type of software testing, which is done just to make sure that API is returning the correct response as per the expected format. API testing includes testing (Representational State Transfer) REST APIs  with (JavaScript Object Notation) JSON being over (Hypertext transfer protocol) HTTP and HTTPS.

    How REST Requests Work?

    An API is made up of set of REST requests. These are the requests made to an application server that retrieve, delete, or manipulate data in an application’s database.

    A REST request is made up of following parts:-

    • An HTTP verb that describes what action should be taken.
    • A URL that defines the location of the request.
    • HTTP headers that provide information to the server about the request.
    • Request body that provides further details for the request.

    Certainly, there are mainly 4 methods involve in testing REST API like GET, POST, PUT and DELETE.

    GET

    First of all, lets start with GET request. It is the most commonly and widely used method in API. The GET method is used to extract information from the given server using a given URL.

    For example, we have an API with /products endpoint. Making a GET request to that endpoint should return a list of all products in the shop and if we want to retrieve the detail of a particular product then we can fetch it by its product id using /products/{{product_id}}.

    Syntax:-


    Response will be like  :-


    We need to validate these things:-

    • Check that a valid GET request returns a 200 status code.
    • Ensure that a GET request to a specific resource returns the correct data. Here for request GET /products, it must return a list of all products.

     

    Post

    Another most important request is POST request which basically creates new entity. It can also be used to send data to the server. The data sent to the server is stored in the request body of the HTTP request. The body specifies exactly what information should be added to the database. It is usually in JavaScript Object Notation (JSON) or Extensible Markup Language (XML) format.

    Here is an example of what a JSON request body might look like for doing a POST request to add a product:-

    Syntax:-


    Request Body :-


    Response will be like  :-


    We need to follow these steps to test POST requests :-

    • Create a resource with a POST request and make sure that 200 status code is returned.
    • Secondly, make a GET request for that resource, and make sure that the data was saved correctly.
    • Add tests that ensure POST requests FAIL with incorrect or wrongly formatted data.

     

    PUT

    PUT requests are used to create a new entity or update an existing one. The difference between PUT and POST is that PUT is idempotent. An idempotent method means that the result of a successful performed request is independent of the number of times it is executed.

    Syntax:-


    Request body and Response will be same like POST requests.

    We need to follow these steps to test PUT requests :-

    • Repeatedly calling a PUT request always returns the same result (idempotent).
    • After updating a resource with a PUT request, a GET request for that resource should return the new data.
    • PUT requests should fail if invalid data is supplied in the request.

    DELETE

    DELETE request deletes the resource at the specified URL.

    If a new product is created with a POST request to /products , and it can be fetched with a GET request to /products/{{product_id}} , then making a DELETE request to /products/{{product_id}} will completely remove that product.

    Syntax:-


    Simple test case for DELETE request is :-

    • Create a new user with a POST request to /products
    • With the product id returned from the POST , make a DELETE request to  /products/{{product_id}}
    • After that on passing a GET request to /products/{{product_id}}  should return a 404 not found status code.

    Likewise, we will learn about how we use postman to test API methods and how to make test scripts in postman in another blog.

    . . .

    Comment

    Add Your Comment

    Be the first to comment.

    Back to Top
    css.php
    Hire Us!
    Brief us about your requirements and we'll get back to you.
    Woo! Hooy!
    We have just recieved your project brief and our expert will contact you shortly.
    Send Again
    Close

    Table of Content

    Hide Index