You can generate fake data for fields in your mock.
Examples include, but are not limited to:
- Names
- Email addresses
- Addresses
To use it, install the fake-data
plugin:
imposter plugin install -d fake-data
Alternatively, you can use the all
distribution, which includes the fake-data
plugin.
This is the docker-all
engine type if you're using the CLI or the imposter-all.jar
file if you're using the JAR file approach.
You can use template expressions in responses to insert fake data. The expressions start with fake.
followed by a type of fake data to generate.
For example, to return a random first name, use the following expression:
${fake.Name.firstName}
For an email address:
${fake.Internet.emailAddress}
Some common examples:
Fake data | Expression |
---|---|
First name | fake.Name.firstName |
Last name | fake.Name.lastName |
Email address | fake.Internet.emailAddress |
Username | fake.Name.username |
Street address | fake.Address.streetAddress |
City | fake.Address.city |
Country | fake.Address.country |
Phone number | fake.PhoneNumber.phoneNumber |
Note Valid values are those supported by the Datafaker library.
See the example directory for a working example.
curl http://localhost:8080/users/1
{
"id": 1,
"firstName": "Linda",
"lastName": "Smith",
"email": "[email protected]"
}
Note This section applies to the OpenAPI plugin.
When using the fake-data
plugin with the OpenAPI plugin, common property names in the OpenAPI specification, such as firstName
, email
etc., are replaced with fake data. This happens when no matching example is found in the specification.
See the example directory for a working example.
For example:
curl http://localhost:8080/users/1
{
"id": 1,
"firstName": "Linda",
"lastName": "Smith",
"email": "[email protected]",
"address": {
"street": "123 Main St",
"city": "New York",
"postcode": "10001"
},
"favouriteColour": "red"
}
You can customize the fake data by adding a x-fake-data
property to the field in the OpenAPI file.
This is done for the favouriteColour
field in the example:
favouriteColour:
type: string
x-fake-data: Color.name
Note Valid values are those supported by the Datafaker library.