Simulation "Hello, World!"

Overview

This is a simple example demonstrating how easy it is to model and run API simulations.

You would need API Simulator installed to be able to run the example but that is as easy as downloading the distro and unpacking it (and, optionally, adding the apisimulator script to user’s path) - see the installation document.

"Hello, World!" Stub

  1. Let' create the following directory structure:

    hello-sim (1)
    └─ simlets (2)
       └─ _default (3)
    1 hello-sim is the name of the simulation.
    2 All simlets for the simulation will reside under the simlets directory.
    3 _default is the folder where we will create the configuration for the default simlet.
  2. Place a file called simlet.yaml under the _default directory with this content while making sure the indentation is preserved:

    _default/simlet.yaml
    response:
      from: stub
      body: "Hello, World!"

    The configuration uses the API Simulator’s YAML-based Domain-Specific Language (DSL).

  3. Start API Simulator for the hello-sim simulation:

    apisimulator start "/path/to/hello-sim"
  4. Point your browser to http://localhost:6090/ or use curl:

    curl localhost:6090

    In both cases API Simulator replies with

    Hello, World!

Because this is how we configured the default simlet and because the simulation doesn’t have any other simlets yet, API Simulator will actually reply with same response to any request. For example, try in the browser http://localhost:6090/greetings or http://localhost:6090/greetings/Luke, and these using curl:

curl localhost:6090/greetings
Hello, World!

curl -X POST localhost:6090/greetings
Hello, World!

curl -X GET localhost:6090/greetings/Luke
Hello, World!

The same Hello, World! response comes back every time.

Execute this command to stop API Simulator:

apisimulator stop "/path/to/hello-sim"

"Greetings" Dynamic Simlet

Replying with the same static Hello, World! response every time may be OK for some use cases but often times we need more than that. How about an API simulation where we pass in a name and API Simulator greets us using that name?

  1. Let’s extend the current setup and add another directory under simlets called greetings and create in it a file named simlet.yaml having the following content (again, make sure the indentation is preserved):

    greetings/simlet.yaml
    matchers:
    - method: GET
    
    - where: uriPathPattern
      matches: /greetings/{name}
    
    
    NameParm:
      is: parameter
      from: uriPathPattern
      pattern: /greetings/{name}
    
    
    response:
      from: template
      template: Simula
      body: "Greetings, ${NameParm}!"

    This is what you should have:

    hello-sim
    └─ simlets
       ├─ _default
       │  └─ simlet.yaml
       └─ greetings
          └─ simlet.yaml
  2. Start API Simulator for the hello-sim simulation:

    apisimulator start "/path/to/hello-sim"
  3. Try in your browser to access http://localhost:6090/greetings/Luke and http://localhost:6090/greetings/Princess Leia, or with curl:

    curl -X GET localhost:6090/greetings/Luke
    Greetings, Luke!
    
    curl -X GET localhost:6090/greetings/Princess+Leia
    Greetings, Princess Leia!

We can now stop the API Simulator instance:

apisimulator stop "/path/to/hello-sim"

The Explanation

Let’s take a deeper look at each part of the simlet’s configuration:

greetings/simlet.yaml
matchers:
- method: GET  (1)

- where: uriPathPattern  (2)
  matches: /greetings/{name}


NameParm:  (3)
  is: parameter
  from: uriPathPattern
  pattern: /greetings/{name}


response:
  from: template  (4)
  template: Simula  (5)
  body: "Greetings, ${NameParm}!"  (6)
1 The greetings simlet matches requests that use the HTTP GET method.
2 Also, requests' URI path must match the pattern /greetings/{name}. The pattern describes URI path that starts with /greetings/ and has one non-empty path segment {name}; how the segment is called isn’t important.
3 The simlet configuration defines a parameter - NameParm - which value comes from the URI path pattern /greetings/{name}, so it gets the value of whatever is in the name path segment after /greetings/.
4 The response is rendered from a template.
5 The template is an API Simulator’s Simula template.
6 The response body has a placeholder - ${NameParm} - which is resolved (substituted) using the value of the NameParm parameter.

There is no HTTP status code configured explicitly so API Simulator will return 200 OK by default.

The response to any request that doesn’t match the greetings simlet is rendered and returned according to the definition of the _default simlet.

In Conclusion

Using API Simulator’s YAML DSL, in no time we created an API simulation and modeled a simlet for static replies, and a simlet that dynamically returns responses based on what is in the request. Then we started an API Simulator instance locally and exercised the functionality of the API simulation.

This only scratched the surface of what is possible with API Simulator.

Interested? We invite you to learn more about API Simulator, download and install it, run the examples found in the distro…​ Why not even create API simulations to help your own testing and development?


We would love to hear your feedback! Shoot us a quick email to [feedback at APISimulator.com] to let us know what you think.

Happy API Simulating!