<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: AbId KhAn</title>
    <description>The latest articles on Forem by AbId KhAn (@abidkhan484).</description>
    <link>https://forem.com/abidkhan484</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F759357%2F2276a1a4-af82-407b-a0fb-8d8bced765bd.jpeg</url>
      <title>Forem: AbId KhAn</title>
      <link>https://forem.com/abidkhan484</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/abidkhan484"/>
    <language>en</language>
    <item>
      <title>Automate API Testing using Postman &amp; Newman</title>
      <dc:creator>AbId KhAn</dc:creator>
      <pubDate>Mon, 14 Mar 2022 16:12:17 +0000</pubDate>
      <link>https://forem.com/abidkhan484/automate-api-testing-using-postman-newman-2c2p</link>
      <guid>https://forem.com/abidkhan484/automate-api-testing-using-postman-newman-2c2p</guid>
      <description>&lt;h2&gt;
  
  
  Introduction:
&lt;/h2&gt;

&lt;p&gt;Postman is one of the most used tools for a developer. Most of the time, we develop APIs and test those individually. But in some scenarios, we have to test APIs in batch if they are dependent on each other. By utilizing the knowledge, we can do our tasks easier as well as minimize the repeating tasks. Through this, our dev testing time can be reduced.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://learning.postman.com/docs/getting-started/installation-and-updates/"&gt;Postman&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.npmjs.com/package/newman"&gt;Newman&lt;/a&gt; and some of it’s required library&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Playground:
&lt;/h2&gt;

&lt;p&gt;In this task, we are using Json placeholder APIs. Postman has the feature to create assertion in the &lt;code&gt;Tests&lt;/code&gt; tab. Also, it has the &lt;code&gt;Pre-request script&lt;/code&gt; tab to run the script before API request. To minimize the complexity, we are checking only the returned status code of the Tests tab and set variables accordingly. &lt;br&gt;
A snippet to set variables in the POST request body is given below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;userId&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{{&lt;/span&gt;&lt;span class="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;}},&lt;/span&gt;
   &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;title&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;{{title}}&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;body&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;{{body}}&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Some most used snippets are already attached beside the Tests tab in Postman to make it easy. A snippet of assertion to check status code is shared below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;pm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Successful POST request&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="nx"&gt;pm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;code&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;to&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;be&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;oneOf&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;201&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;202&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Afterward, we are adding the below code to set variables in Postman to use it later. This is like just another Javascript code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;pm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;json&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;pid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="nx"&gt;pm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;environment&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;postId&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;pid&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we can use this &lt;code&gt;postId&lt;/code&gt; variable in another API request inside the &lt;em&gt;header, body, URL or query parameter&lt;/em&gt;. &lt;br&gt;
Now the most interesting part comes, that is, how can we set the variables automatically? We can use Postman runner to do the job. But for this task, we will use the Newman tool. To do so, we will create a CSV file with the header of the variable names (e.g &lt;em&gt;userId, title, body&lt;/em&gt;). Now, we can run the below command to request with the CSV file data.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;newman&lt;/span&gt; &lt;span class="nx"&gt;run&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;jsonplaceholder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;postman_collection&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;json&lt;/span&gt; &lt;span class="o"&gt;\&lt;/span&gt;
   &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;d&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;sample_posts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;csv&lt;/span&gt; &lt;span class="o"&gt;\&lt;/span&gt;
   &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;timeout&lt;/span&gt; &lt;span class="mi"&gt;300000&lt;/span&gt; &lt;span class="o"&gt;\&lt;/span&gt;
   &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nb"&gt;global&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;json_placeholder&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nx"&gt;https&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="c1"&gt;//jsonplaceholder.typicode.com \&lt;/span&gt;
   &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;reporters&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nx"&gt;cli&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;htmlextra&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;csv&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Also, we can request a specific folder of the collection with the &lt;em&gt;--folder&lt;/em&gt; flag. Try to search for the &lt;em&gt;--reporters&lt;/em&gt; flag and its usage.&lt;br&gt;
A Github repo &lt;a href="https://github.com/abidkhan484/automate-api-testing-using-postman"&gt;automate-api-testing-using-postman&lt;/a&gt; is made to review with the document.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion:
&lt;/h2&gt;

&lt;p&gt;Last but not the least, with Newman, we can get a html-like report view where we can easily get the request/ response information or CSV report if the request volume is high. Finally, we can focus on test cases that are more necessary for developer speed.&lt;/p&gt;

</description>
      <category>testingapi</category>
      <category>postman</category>
      <category>newman</category>
      <category>intro</category>
    </item>
    <item>
      <title>Load Balancing Playground with Nginx and Docker</title>
      <dc:creator>AbId KhAn</dc:creator>
      <pubDate>Sun, 13 Mar 2022 07:02:10 +0000</pubDate>
      <link>https://forem.com/abidkhan484/load-balancing-playground-with-nginx-and-docker-1fc1</link>
      <guid>https://forem.com/abidkhan484/load-balancing-playground-with-nginx-and-docker-1fc1</guid>
      <description>&lt;h2&gt;
  
  
  Introduction:
&lt;/h2&gt;

&lt;p&gt;Assuming you have already heard the term &lt;code&gt;Load Balancing&lt;/code&gt;. If not, then refer you to learn a very basic about the technique from this &lt;a href="https://en.wikipedia.org/wiki/Load_balancing_(computing)"&gt;load balancing wiki&lt;/a&gt;. The concept of the task is to make multiple App servers and a Load balancer to balance the traffic. In this writeup, I am sharing my experience to start learning load balancing.&lt;/p&gt;

&lt;h2&gt;
  
  
  Background:
&lt;/h2&gt;

&lt;p&gt;Different VMs can be created for the app servers and the load balancer in the virtual machine. Also, we have to install multiple services (like nginx) in those VMs that are relatively time consuming. Though the repetitive tasks can be done with some tool like Ansible or the different VMs can be created with Terraform. But to avoid that type of repetitive task or the technique mentioned above, we are using Docker to do the task. &lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Virtual Machine&lt;/li&gt;
&lt;li&gt;Docker&lt;/li&gt;
&lt;li&gt;Docker Compose&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Playground:
&lt;/h2&gt;

&lt;p&gt;Initially, we have started with running multiple docker containers to create a couple of app servers and a load balancer. The commands are given below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# To create the app servers&lt;/span&gt;
docker run &lt;span class="nt"&gt;--name&lt;/span&gt; nginx_1 &lt;span class="nt"&gt;-d&lt;/span&gt; &lt;span class="nt"&gt;-p&lt;/span&gt; 9998:80 nginx
docker run &lt;span class="nt"&gt;--name&lt;/span&gt; nginx_2 &lt;span class="nt"&gt;-d&lt;/span&gt; &lt;span class="nt"&gt;-p&lt;/span&gt; 9997:80 nginx
docker run &lt;span class="nt"&gt;--name&lt;/span&gt; nginx_3 &lt;span class="nt"&gt;-d&lt;/span&gt; &lt;span class="nt"&gt;-p&lt;/span&gt; 9996:80 nginx
&lt;span class="c"&gt;# To create a load balancer&lt;/span&gt;
docker run &lt;span class="nt"&gt;--name&lt;/span&gt; nginx_balancing &lt;span class="nt"&gt;-d&lt;/span&gt; &lt;span class="nt"&gt;-p&lt;/span&gt; 9999:80 nginx
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Afterwards, we have logged in to the different app servers and executed the below command to update the default html file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# login to app server container&lt;/span&gt;
docker &lt;span class="nb"&gt;exec&lt;/span&gt; &lt;span class="nt"&gt;-it&lt;/span&gt; nginx_1 bash
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s1"&gt;'&amp;lt;h1&amp;gt;app 1&amp;lt;/h1&amp;gt;'&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; /usr/share/nginx/html/index.html

&lt;span class="c"&gt;# run similar command with different h1 tags &lt;/span&gt;
&lt;span class="c"&gt;# to the rest of the servers app 2 and app 3&lt;/span&gt;
&lt;span class="c"&gt;# try it yourself&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then we have logged in to the load balancer container and updated the default conf with the below configuration according to the guided &lt;a href="https://docs.nginx.com/nginx/admin-guide/load-balancer/http-load-balancer/"&gt;nginx load balancer configuration&lt;/a&gt;. Here the IP (172.17.0.1) is the docker interface IP of the host machine.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# add this to outside the server block&lt;/span&gt;
upstream backend &lt;span class="o"&gt;{&lt;/span&gt;
    server 172.17.0.1:9996&lt;span class="p"&gt;;&lt;/span&gt;
    server 172.17.0.1:9997&lt;span class="p"&gt;;&lt;/span&gt;
    server 172.17.0.1:9998&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="c"&gt;# add this inside the server block&lt;/span&gt;
&lt;span class="c"&gt;# also inside the "location /" block if exist&lt;/span&gt;
location / &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="c"&gt;# load balancer&lt;/span&gt;
    proxy_pass http://backend/&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we are ready to explore the output of the load balancing. Open the browser and browse to the below URL and check the output by reloading the page.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# load balancer URL&lt;/span&gt;
http://localhost:9999
&lt;span class="c"&gt;# app server 1 URL&lt;/span&gt;
http://localhost:9998
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The same thing I have done with the docker compose shared in the &lt;a href="https://github.com/abidkhan484/nginx-load-balancing-docker"&gt;nginx-load-balancing-docker&lt;/a&gt; github repo. In this docker compose file, I’ve just reset the networking technique in the docker compose file and updated the nginx default conf file accordingly. Refer to the trailing snippet to learn about it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Nginx default conf for the docker compose file&lt;/span&gt;
upstream backend &lt;span class="o"&gt;{&lt;/span&gt;
   server 10.10.0.2:80&lt;span class="p"&gt;;&lt;/span&gt;
   server 10.10.0.3:80&lt;span class="p"&gt;;&lt;/span&gt;
   server 10.10.0.4:80&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="c"&gt;# the below changes are inside the server block of default conf&lt;/span&gt;
location / &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="c"&gt;# load balancer&lt;/span&gt;
    proxy_pass http://backend/&lt;span class="p"&gt;;&lt;/span&gt; 
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion:
&lt;/h2&gt;

&lt;p&gt;For me, Docker is a nice tool to play new techniques without installing in the host machine. Therefore I always try to find a docker image to R&amp;amp;D any service that is available in docker hub. And with following this, one can focus on learning the technique instead of setting the environment to decrease time consumption.&lt;/p&gt;

</description>
      <category>loadbalancing</category>
      <category>nginx</category>
      <category>docker</category>
      <category>playground</category>
    </item>
  </channel>
</rss>
