-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #129 from natenho/feature/docs-scripting
Improve scripting documentation
- Loading branch information
Showing
33 changed files
with
644 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"label": "Scripting Reference", | ||
"position": 3 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
# Faker | ||
|
||
## Basic usage | ||
|
||
A `Faker` facade object is available to generate fake data within a script. | ||
|
||
``` | ||
{ | ||
"request": { | ||
"method": "GET" | ||
}, | ||
"response": { | ||
"status": "OK", | ||
"headers": { | ||
"Content-Type": "application/json" | ||
}, | ||
"body": { | ||
"name": "<#= Faker.Name.FullName() #>", | ||
"company": "<#= Faker.Company.CompanyName() #>", | ||
"city": "<#= Faker.Address.City() #>" | ||
} | ||
} | ||
} | ||
``` | ||
|
||
```shell | ||
$ curl http://localhost:5000 | ||
{ | ||
"name": "Mollie Beahan", | ||
"company": "Ziemann, Anderson and Durgan", | ||
"city": "Ritchiemouth" | ||
} | ||
``` | ||
|
||
## Generating a list of items | ||
|
||
This example creates a list of 10 items: | ||
|
||
``` | ||
{ | ||
"request": { | ||
"method": "GET", | ||
"route": "/names" | ||
}, | ||
"response": { | ||
"body": <#= | ||
class Person | ||
{ | ||
public int ID { get; set; } | ||
public string Name { get; set; } | ||
} | ||
var count = 10; | ||
var people = new Person[count]; | ||
for(var i = 0; i < count; i++ ) { | ||
people[i] = new Person { ID = i + 1, Name = new Faker().Person.FullName }; | ||
} | ||
return JsonConvert.SerializeObject(people); | ||
#> | ||
} | ||
} | ||
``` | ||
|
||
## Localization | ||
|
||
To generate localized data, use the `Accept-Language` HTTP header when sending a request to Mockaco. Defaults to `en` (english) fake data. | ||
|
||
``` | ||
{ | ||
"request": { | ||
"method": "GET" | ||
}, | ||
"response": { | ||
"status": "OK", | ||
"headers": { | ||
"Content-Type": "application/json" | ||
}, | ||
"body": { | ||
"name": "<#= Faker.FullName() #>", | ||
"company": "<#= Faker.Company.CompanyName() #>", | ||
"city": "<#= Faker.Address.City() #>" | ||
} | ||
} | ||
} | ||
``` | ||
|
||
```shell | ||
$ curl -X GET "http://localhost:5000" -H "Accept-Language: ru" | ||
{ | ||
"name": "Екатерина Мельникова", | ||
"company": "Гусев - Никонов", | ||
"city": "Тула" | ||
} | ||
``` | ||
|
||
```shell | ||
$ curl -X GET "http://localhost:5000" -H "Accept-Language: pt-BR" | ||
{ | ||
"name": "Maitê Albuquerque", | ||
"company": "Costa S.A.", | ||
"city": "Santo André" | ||
} | ||
``` | ||
|
||
## Using Bogus extensions | ||
|
||
To use [Bogus API Extension Methods](https://github.com/bchavez/Bogus?tab=readme-ov-file#api-extension-methods), consider the following example, using the `Bogus.Extensions.Brazil` namespace to generate brazilian CPF numbers: | ||
|
||
``` | ||
{ | ||
"request": { | ||
"method": "GET" | ||
}, | ||
"response": { | ||
"status": "OK", | ||
"body": <#= Faker.Person.Cpf() #> | ||
} | ||
} | ||
``` | ||
|
||
Use the [Imports option](/docs/configuration/#imports) to import the Bogus extension methods on Mockaco startup: | ||
|
||
```shell | ||
$ mockaco --urls=http://+:5000 --Mockaco:Imports:0="Bogus.Extensions.Brazil" | ||
``` | ||
|
||
Then call the mock endpoint: | ||
|
||
```shell | ||
$ curl -i "http://localhost:5000" | ||
HTTP/1.1 200 OK | ||
Content-Type: application/json | ||
Date: Sun, 10 Mar 2024 23:44:50 GMT | ||
Server: Kestrel | ||
Transfer-Encoding: chunked | ||
|
||
"422.244.459-62" | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
# Global | ||
|
||
The Global object is a global variable that can be used to store variables that are shared between all mock requests. Its underlying storage is a `Dictionary<string, object>` object, meaning that you can store any type of variable in it. The state is not persisted between server restarts. | ||
|
||
## Store a variable along a mock request | ||
|
||
``` | ||
<# | ||
Global["my-custom-variable"] = "hello"; | ||
#> | ||
{ | ||
"request": { | ||
"method": "GET", | ||
"route": "ping" | ||
}, | ||
"response": { | ||
"status": "OK", | ||
"body": { | ||
"response": "<#=Global["my-custom-variable"] #>" | ||
} | ||
} | ||
} | ||
``` | ||
|
||
```shell | ||
$ curl http://localhost:5000/ping | ||
{ | ||
"response": "hello" | ||
} | ||
``` | ||
|
||
## Store state between mock calls | ||
|
||
The Global object is shared between all mock requests, meaning that you can store a variable in one mock and use it in another. | ||
|
||
``` | ||
{ | ||
"request": { | ||
"method": "GET", | ||
"route": "store" | ||
}, | ||
"response": { | ||
"status": "OK", | ||
"body": { | ||
"response": "This request stores a variable" | ||
} | ||
} | ||
<# | ||
Global["my-custom-variable"] = "hello!"; | ||
#> | ||
} | ||
``` | ||
|
||
``` | ||
{ | ||
"request": { | ||
"method": "GET", | ||
"route": "retrieve" | ||
}, | ||
"response": { | ||
"status": "OK", | ||
"body": { | ||
"response": "The variable is <#=Global["my-custom-variable"] #>" | ||
} | ||
} | ||
} | ||
``` | ||
|
||
```shell | ||
$ curl http://localhost:5000/store | ||
{ | ||
"response": "This request stores a variable" | ||
} | ||
|
||
$ curl http://localhost:5000/retrieve | ||
{ | ||
"response": "The variable is hello!" | ||
} | ||
``` | ||
|
||
This feature can be used to simulate stateful APIs behaviors. Refer to the [Stateful mocks guide](/docs/guides/mocking-stateful) for more information. |
Oops, something went wrong.