Web Design Process Reflection

From the beginning of this whole process, we were assigned to something pretty easy because we already know where we were heading. This is because it was already an existing app, so all we had to do…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Mock it until you have it

Mock the HTTP and HTTPS websites and REST APIs so that you don’t depend on these services or internet while working on any related development or automated testing.

When developing a new client facing application either desktop, web or mobile, on of the biggest challenge is availability of right web services of API which might be part of your own server or some other third party web services if you are integrating your application to third party system. This problem also arises if application and the web services which are required to be consumed are being developed in parallel due to unavoidable situations.

This problem is not just limited to development, its also a challenge in automated/manual testing. Any testing is only useful if its reliable. Having this in mind, it can be really problematic if required third party services/website changes, are down or some other issue or even if you are consuming your own web services, availability of right ad complete data every time during test itself can be big challenge.

And if sometimes your test failed due to data contract changes in any of these services, its really hard to find out the exact cause.

Fortunately, there are many tools available which can help you to mock the web services so that you no longer wait or get stuck due to unavailability or changes in the services.

In this article, we will learn how to use Flashback to much the web services. Flashback is developed by LinkedIn which was open sourced in June 2017.

Flashback s Java based cross platform tool written to mock HTTP and HTTPS services/ REST APIs. Basically it records the HTTP and HTTPS requests and stores locally and later it plays back the same request by matching the requests based on various request matching rules. Each such recording of request is called Scene.A match rule associates an incoming request with a previously-recorded request, which is then used to generate a response. HTTP requests generally contain a URL, method, headers, and body. Flashback allows match rules to be defined for any combination of these components.

Mocking a HTTP request is very simple using Flashback. Flashback works exactly like Man in Middle proxy which intercepts and then records all the web traffic. Flashback has two parts of it:

Flashback Admin Server — Its centrally controls the Flashback proxies created for recording and replaying HTTP and HTTPS scenes.

Flashback Proxy server — It actually intercepts the HTTP/HTTPS traffic and record and play them back. Save these recording as scene and then plays them back based on various match rules and provided configuration.

Before we actually mock a service, lets us first setup Flashback for mocking HTTP services. First of all make sure that http server is enabled in your machine if you are using the MAC.

1. Clone the Flashback repo

2. On terminal, change your current working directory to this clone repo directory and start Flashback admin server by running below script:

3. Once admin server started, you can interact with it using REST APIs it exposes. Next step is to start a Flashback HTTP proxy server to record the HTTP scene. Simply hit the below API either using Curl or any of your favourite REST client, I have used Postman for this demo:

Request Body:

In body, we pass the various config parameter, you can refer here for more details of parameters.

4. Everything is done now, our proxy is running to record the traffic but before that you need to configure this HTTP proxy settings on the device or MAC where you have run this proxy and where you want to record the traffic (typically the same machine). Sample setting below on MAC.

Once configured save settings and restart you browser or any other client you are using.

5. Simply hit any HTTP website or REST API and once you are done, stop the Flashback proxy server. Flashback saves the Scenes on disk only once its stopped.

6. To verify if everything went well, jus go to your ‘scenePath’ location which you configure while starting proxy server and you should see a file with same name as ‘sceneName’, just open it and you will find all the HTTP websites/APIs you have visited.

To replay a previously stored scene, use the same basic setup as is used when recording; the only difference is that you set the “sceneMode” to “playback” in step 3 above:

Request Body:

One way to verify that the response is from the scene, not the external source, is to disable your internet connectivity temporarily when you go through Steps 1 through 6. Another way is to modify your scene file and see if the response is the same as what you have in the file. Make sure you still have system proxy setting configured in you machine as mentioned in step 4 above.

There are various configuration parameter available for Flashback Admin server to start or stop HTTP to HTTPS proxy service which you would need as you go. Most of these parameters are not directly provided anywhere on git page of Flashback. Only way is to dig through he code and find out. Don’t worry you need not to do that I have found a few which you can use directly. Lets go through these parameters:

Mocking a HTTPs transaction is similar to HTTP transactions but it required few more configuration. HTTPS transactions are encrypted and Flashback can’t encode the transactions by default. In order to enable Flashback, we need to create a Certificate Authority (CA) certificate. This certificate will be used by Flashback to intercept the HTTPS traffic by violating man in middle attack.

There are two way you can obtain and provide certificate to Flashback.

I will now be covering on how to created your own CA certificate, you can follow below guide blindly step by step to create your own certificate:

Once you obtain the certificate, its just matter for some configuration to mock HTTPS traffic. If you follow the steps in above like to create the certificate, you will get one certificate ‘ca.cert.pem’ and one private key ‘ca.key.pem’ also a password (say its ‘secretpassword’) for these certificate for certificate and private key. Keep them handy, we would need them soon.

There are two way you can provide the certificate to Flashback either directly input as .pem file or create the .p12 file.

To create the .p12 file, run the below command after obtaining both of above files:

It will generate you intermediateCA.p12 certificate. Now we are ready with all the prerequisites.

1. Simply hit the below API

Request Body:

Refer here for more details of parameters.

2. Make sure you system proxy is configured as below, you can remove the HTTP proxy if you want which we configured earlier.

3. Hit any HTTPS websites or web services

4. Stop the Flashback Proxy server:

5. Replay the HTTPS recorded Scene:

Request Body:

6. Make sure you system proxy is configures as in Step 2. And hit the web service or REST api from your favourite REST client or browser. Congratulations you have done it!

Here is the description and other possible value used for various Flashback REST APIs. You can choose them based on your need. ‘matchRule would be the possibly most used.

If you have any question, please post below. Happing Mocking!

Add a comment

Related posts:

How I realized clouds are not made of cotton candy

Mr. Cool was what his friends would call him in his younger days, because he never, ever lost his cool. Dad remained calm in any situation he found himself in, it didn’t matter if he was being…

Domestic Violence Awareness Month

This week marked the beginning of Domestic Violence Awareness Month. The web is slowly filling with stories, articles, and blogs about domestic violence. This is a very important issue and one that…

The Productivity Lie

Productivity is important. Efficiency is important. However, idle moments are also crucial to be successful. Especially, when you work in a creative profession.