DEV Community

Shubhendu Madhukar
Shubhendu Madhukar

Posted on • Edited on

Mocking backends with Camouflage

In my previous post I introduced Camouflage as a mocking tool for HTTP backends. After a number of feedbacks, numerous tests and code optimisations, documentation proof reading and much more, Camouflage is now on its way to a stable release, with lots of new bells and whistles.


What are mocks?

Mocks or Stubs or Virtual Services, are replacements for your actual backend systems, which can be used for testing purposes in case your backend is unavailable. Mocks are great for several reasons:

  • You are building a frontend application but your backend isn't ready yet. Use mocks to quickly create a virtual backend which provides you a dummy response and lets you test your frontend application without actually hitting a backend. Once your backend is ready just replace the mock server host with the actual server host in your configs and everything else remains the same. No more reading static responses from files.
  • You can have similar use cases while running unit tests, functional tests or even performance tests, where as long as the virtual service can simulate the latency and provide a response similar to actual response, you don't need your complete backend and downstream to be ready for running tests in silos.
  • Mocks are also great for debugging purposes, when you are testing multiple microservices together. Even with advanced monitoring tools, sometimes it's hard to pinpoint the exact cause of the issue. With mocks you can plug and play and debug which component is causing problems. And assess if rest of your application stack will work fine if the problematic component wasn't so…problematic.

How do you create mocks?

While there are a lot many tools available in open source world that allow you to create mocks, however in this article I'll discuss a new tool I have been working on. Camouflage.

Camouflage works just as the name suggests. It allows you to create and use dummy backends. And your front end or dependant applications wouldn't be able to tell the difference if the response is coming from a mock or an actual API.

Though Camouflage isn't an original idea in it's essence, it has a lot of cool features and enhancements over existing tools that help you get up and running within seconds. Few of the prominent features are:

  • Camouflage has a near minimum learning curve. Create a directory mocks/hello-world. Place a file named GET.mock containing your raw HTTP Response. And you are done. Make a GET request to /hello-world and you get your expected response. Make any changes to the content of your .mock file name and you get the new response without any downtime.
  • Camouflage heavily uses handlebars, which enables you to add character to your response. Insert dynamic random values that change on each invocation, fetch data from incoming request and send out a conditional response, simulate delays and lot more.
  • Camouflage comes in two modes, functional and performance. By default Camouflage runs in functional mode, which is sufficient for unit tests, frontend testing, even a small scale performance test. However if your machine has multiple CPUs and you are planning to run a performance test, why not make use of the full potential of your machine. You can use performance mode which lets Camouflage utilise multiple CPUs using node's cluster module.
  • And finally, Camouflage supports multiple protocols, such as HTTP, HTTPS, HTTP2 and gRPC. More protocols are in development (i.e. TCP and SMTP)

Enough talk. How do we create a mock?

Well, you follow few simple steps:

  • Install Camouflage as a global package using your favourite package manager
npm install -g camouflage-server

Or,

yarn global add camouflage-server
Enter fullscreen mode Exit fullscreen mode
  • Create a mocks directory which will contain all your mocks. e.g. ~/mocks
  • Create a config file. config.yml
loglevel: info
cpus: 1
monitoring:
  port: 5555
protocols:
  http:
    mocks_dir: "./mocks"
    port: 8080
  https:
    enable: false
    port: 8443
    cert: "./certs/server.cert"
    key: "./certs/server.key"
  http2:
    enable: false
    port: 8081
    cert: "./certs/server.cert"
    key: "./certs/server.key"
  grpc:
    enable: false
    host: "10.111.22.333"
    port: 4312
    mocks_dir: "./grpc/mocks"
    protos_dir: "./grpc/protos"
Enter fullscreen mode Exit fullscreen mode
  • Start Camouflage: camouflage --config config.yml
  • Create another directory in the format your APIs basepath would be. For example: For an API http://localhost:8080/hello/world, create directories as ~/mocks/hello/world
  • Create a file ${HTTP_METHOD}.mock and insert your HTTP raw response. e.g vi ~/mocks/hello/world/GET.mock and paste following content. (If you are on windows, simply use notepad.)
HTTP/1.1 200 OK
X-Custom-Header: Custom-Value
Content-Type: application/json
{
     "greeting": "Hey! It works!"
}
Enter fullscreen mode Exit fullscreen mode

And you are done, navigate to http://localhost:8080/hello/world, to see your mock in action.

Conclusion:

There are already a lot of mocking tools already available like Wiremock, mountebank etc. And those are really great tools, but in my experience, it took me sometime to familiarise myself with the tool, their JSON schema and other available options. The problem statement Camouflage tries to tackle is simple, how to shorten the learning curve and get started with mocks creation within seconds.

On more details on how to use Camouflage, visit the documentation. If you'd like to contribute, you can do so by reporting bugs, providing suggestions on what more protocols can be implemented and use cases to cover and finally by creating pull requests for the changes you'd like to see.

Happy Mocking!

Top comments (0)