Tutorial 1: Create a service and simulation manually

In this walkthrough you will create a virtual product API using the editor that exposes two HTTP GET endpoints; one that returns a json product for a given Id, and one that returns a list of products.

Part 1. Setup the Simulation file with a GET product for a given productId Request/Response pair

Follow these steps:

  1. Navigate to Simulations.

  2. Click Create

  3. Click "Add endpoint"

    • This immediately creates a simple endpoint that matches any HTTP verb and responds with a status of 200 - OK.

  4. Change the Method to GET

  5. Change the Path to Regex match and enter /product/[^/]+ in the textbox.

    • This will make the simulation respond when it receives a request that has a path ending in /product/[some value here]

    • [some value here] in this case will be the Id of the product that was sent as part of the request. You will use this later.

  6. Add the following snippet to the Response Body:

    {
        "productId": "{{ Request.Path.[1] }}",
        "name": "Fund {{ randomStringLength 1 }}",
        "price": {{ randomFloatRange 1.0 50.0 }}
    }
  7. Click the Enable Templating checkbox. This will make Hoverfly parse the Response and replace the handlebar syntax {{ }} with values based on the expression used.

  8. Scroll to the top of the page and click Save

  9. Provide the name "product-sim" when prompted for a name

  10. You will be returned to the Simulations page where your new simulation will appear in the list. From here using the buttons on the right, you can edit it, download it (useful if you wanted to manually edit the file, or store it in source control), and delete it.

Part 2. Setup the Service

Follow these steps:

  1. Navigate to the Dashboard

  2. Click the orange +Add button in the Services section, this will open a dialogue window.

  3. From the Simulation drop-down choose the "product-sim" simulation file that was just created

  4. In the Name type "product". You will notice that this prefixes your unique hoverfly.io URL and will be the endpoint for this service.

  5. Leave the rest of the defaults and click Confirm

  6. You will be returned to the dashboard, and if all went ok your new service will be spinning up. When it is ready it will have a green tick next to it.

  7. Copy the URL of the service - it will be something like https://product-youraccounthere.hoverfly.io

  8. Open the URL in a browser, cURL or Postman and append the path /product/12345

  9. The result should be some json generated from the Response template you set up earlier. For example:

    {
        "productId": "12345",
        "name": "Fund a",
        "price": 10.587209
    }
  10. You will notice that the productId is taken from the Request Path variable that you used in the Request. The name and price, were randomly generated using templating functions.

  11. If you refresh you will get different values for the each of the name and price values.

  12. If you change the path variable, it will be reflected in the Response as the productId

  13. If you remove the path variable, you will notice that you receive a matching error from Hoverfly. It was not able to find a suitable Response match, given the Request provided.

Part 3. Add another GET Request/Response pair to the same simulation file to return all Products

Follow these steps:

  1. The service you created in step 2 runs a copy of the master simulation file you created in step.

  2. If you were to make changes to the simulation file at the service level, it will not automatically update the master simulation file stored under "Simulations". You can however "Sync" any changes you make to the simulation at the service level back to the master simulation file, which will overwrite the master simulation file with your changes. You will do that now.

  3. From the dashboard find the product service you created and click the Simulation link for the service. This will open the simulation editor with simulation that the service is running. Because you are opening the service's copy of the simulation, and not the original master copy, you will see this message:

  4. In the editor that opens you will see the single end point " GET/product/[^/]+ "

  5. You can either click +Add endpoint to add a new endpoint, or in this case we will simply duplicate the existing endpoint and make changes to it.

  6. Click the ellipse next to the existing endpoint and choose "Duplicate". A new replica of the endpoint is created beneath the existing one.

  7. This new endpoint will return a list of products and won't take any parameters, so change the Path under Request matches to an exact match with a Path of "/products"

  8. Now change the Response Body of the new endpoint to the following json:

[
    {
        "productId": "P001",
        "name": "Fund A",
        "price": 9.99
    },
    {
        "productId": "P002",
        "name": "Fund B",
        "price": 19.99
    },
    {
        "productId": "P003",
        "name": "Fund C",
        "price": 14.99
    },
    {
        "productId": "P004",
        "name": "Fund D",
        "price": 24.99
    },
    {
        "productId": "P005",
        "name": "Fund E",
        "price": 29.99
    }
]
  1. We are not using Templating in this Response, so it can be unchecked. If you wanted to add templating you would simply check it.

  2. Scroll up to the top of the page and click Apply changes. This will update the running service with the changes you have just made.

  3. Now, if you navigate back to the Dashboard, you can copy the URL for the service and open it in a browser, cURL or Postman. This time append the path "/products" to the URL. You should get back your list of products as a response.

  4. If you change the path from "/products" to "/product/abc", you should still get back a single product with productId abc.

Part 4. Update the master simulation with the local changes

Follow these steps:

  1. In order to keep the master simulation file in sync with the changes you have just made, you can update the master simulation by selecting "Sync simulation" from the drop down under the "Apply changes" button at the top of the service simulation editor.

  2. Alternatively, you could also export your changes as a new simulation from here if that were the need.

Part 5. Remove the service and simulation

Follow these steps:

  1. Unless you want to keep the service and simulation, they can be deleted.

  2. To delete the service and it's copy of the simulation navigate to the Dashboard, open the ellipse to the right of the product service that you created, and select Delete.

  3. To delete the master simulation, navigate to the Simulations page, locate the simulation you created, and click the corresponding x at the right of page.

Congratulations! You have completed this Tutorial, "Create a Service and Simulation manually."

Last updated