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.
Click the Enable Templating checkbox. This will make Hoverfly parse the Response and replace the handlebar syntax {{ }} with values based on the expression used.
Scroll to the top of the page and click Save
Provide the name "product-sim" when prompted for a name
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:
Navigate to the Dashboard
Click the orange +Add button in the Services section, this will open a dialogue window.
From the Simulation drop-down choose the "product-sim" simulation file that was just created
In the Name type "product". You will notice that this prefixes your unique hoverfly.io URL and will be the endpoint for this service.
Leave the rest of the defaults and click Confirm
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.
Copy the URL of the service - it will be something like https://product-youraccounthere.hoverfly.io
Open the URL in a browser, cURL or Postman and append the path /product/12345
The result should be some json generated from the Response template you set up earlier. For example:
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.
If you refresh you will get different values for the each of the name and price values.
If you change the path variable, it will be reflected in the Response as the productId
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:
The service you created in step 2 runs a copy of the master simulation file you created in step.
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.
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:
In the editor that opens you will see the single end point " GET/product/[^/]+ "
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.
Click the ellipse next to the existing endpoint and choose "Duplicate". A new replica of the endpoint is created beneath the existing one.
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"
Now change the Response Body of the new endpoint to the following json:
We are not using Templating in this Response, so it can be unchecked. If you wanted to add templating you would simply check it.
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.
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.
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:
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.
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:
Unless you want to keep the service and simulation, they can be deleted.
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.
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."