Understanding HTTP messages
In our quest to understand API testing, the first step would be to understand the general protocol used by client and server side of an application to communicate, which in API automation we would try to automate.
This post is summary of the discussion from the video below.
Sending an email is like posting a letter
The goal here would be to translate the concepts with daily life examples to understand client server communication easily. Transmitting of data from client to server is pretty much like sending a letter from one postal address to another, only it takes days instead of milliseconds!
Details in the video
The steps in both are very much the same, except the way of doing them. The takeaway here is what the main requirements for sending a letter are the same for what is needed to send an email, sender and receiver details and transmission details needed.
In our example, when Bob sends his mail, the courier company would seal the letter in their envelope along with some of their transportation data like from / To addresses and a tracking number.
All digital communications follow a similar model called the OSI Layers (http://searchnetworking.techtarget.com/definition/OSI) model. The actual message to be sent from sender to receiver is encapsulated with headers and additional transmission information. There can be different message types and header configurations, all explaining how to send and display the message to the receiver.
Constructing an HTTP Request
There are few main areas of an HTTP request, we’ll look into each of them and find out how they help in forming a request.
‘Request Methods’ refers to the different type of HTTP messages used for different purposes. Mostly commonly used are GET and POST messages.
Headers include communication related data, session IDs, client type, body data type are some very common ones. In some cases (e.g. for GET messages) the request need to be made is part of the header. A client side requesting a specific record will add the request in the header of a GET message.
The ‘Body’ includes data to send the server side. A common example would be adding data to the database. In our example when Bob sends the email, the text of his email goes in the body section of the message. There are different formats in which this data can be encoded.
HTTP Request and Response
When Bob sent the email, an ‘HTTP request’ was sent. To confirm the transmission an ‘HTTP response’ will be sent back from the server to the client.
While performing API automation, the tests would construct an HTTP request and assert the corresponding response received. An understanding of the response is therefore critical. In some cases further requests might be needed to confirm if all the data expected is saved and retrieved properly.