Skip to content

Instantly share code, notes, and snippets.

@mertyildiran
Last active May 20, 2021 13:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mertyildiran/685b983751c03f56468665562f725dd8 to your computer and use it in GitHub Desktop.
Save mertyildiran/685b983751c03f56468665562f725dd8 to your computer and use it in GitHub Desktop.
Mockintosh: mocks for microservice environments - Part 3: HTTP Response Templating (Handlebars & Jinja2)
services:
- port: 8081
endpoints:
- path: /api-call
response:
status: "{{ request.queryString.rc }}"
headers:
content-type: '{{ request.headers.accept }}'
x-custom-id: '{{ random.int 0 1000 }}'
body: '{"result": "created", "name": "{{fake.lastname}}" }'
response:
body: '@some/path/my_template.json.hbs'
{
"random_int": {{ random.int 1 5 }},
"random_float": {{ random.float 1.0 2.0 3 }},
"random_alphanum": "{{ random.alphanum 7 }}",
"random_hex": "{{ random.hex 5 }}",
"random_uuid": "{{ random.uuid4 }}",
"random_ascii": "{{ random.ascii 11 }}"
}
{
"now": "{{ date.date '%Y-%m-%d %H:%M %f' }}",
"1_week_back": "{{ date.date '%Y-%m-%d %H:%M %f' -604800 }}",
"1_week_forward": "{{ date.date '%Y-%m-%d %H:%M %f' 604800 }}",
"1_day_back": "{{ date.date '%Y-%m-%d %H:%M %f' -86400 }}",
"1_day_forward": "{{ date.date '%Y-%m-%d %H:%M %f' 86400 }}",
"1_hour_back": "{{ date.date '%Y-%m-%d %H:%M %f' -3600 }}",
"1_hour_forward": "{{ date.date '%Y-%m-%d %H:%M %f' 3600 }}",
"1_minute_back": "{{ date.date '%Y-%m-%d %H:%M %f' -60 }}",
"1_minute_forward": "{{ date.date '%Y-%m-%d %H:%M %f' 60 }}"
}
{
"first_name": "{{ fake.first_name }}",
"first_name_female": "{{ fake.first_name_female }}",
"first_name_male": "{{ fake.first_name_male }}",
"first_name_nonbinary": "{{ fake.first_name_nonbinary }}",
"last_name": "{{ fake.last_name }}",
"last_name_female": "{{ fake.last_name_female }}",
"last_name_male": "{{ fake.last_name_male }}",
"last_name_nonbinary": "{{ fake.last_name_nonbinary }}",
"address": "{{ replace ( fake.address ) old='\n' new='\\n' }}",
"city": "{{ fake.city }}",
"country": "{{ fake.country }}",
"hexify_args": "{{ fake.hexify text="MAC Address: ^^:^^:^^:^^:^^:^^" upper=true }}",
"lexify_args": "{{ fake.lexify text="Random Identifier: ??????????" }}",
"numerify_args": "{{ fake.numerify text="Intel Core i%-%%##K vs AMD Ryzen % %%##X" }}",
"random_choices": "{{ fake.random_choices elements=( array 'a' 'b' 'c' 'd' 'e' ) }}",
"random_digit": {{ fake.random_digit }},
"random_element": "{{ fake.random_element elements=( array 'a' 'b' 'c' 'd' 'e' ) }}",
"random_elements": {{ tojson ( fake.random_elements elements=( array 'a' 'b' 'c' 'd' 'e' ) length=3 unique=True ) }},
"random_int_args": {{ fake.random_int min=10000 max=50000 step=500 }},
"random_letter": "{{ fake.random_letter }}",
"random_letters": {{ tojson ( fake.random_letters ) }},
"random_letters_args": {{ tojson ( fake.random_letters length=32 ) }},
"random_lowercase_letter": "{{ fake.random_lowercase_letter }}",
"random_sample": {{ tojson ( fake.random_sample elements=( array 'a' 'b' 'c' 'd' 'e' ) ) }},
"random_uppercase_letter": "{{ fake.random_uppercase_letter }}"
}
{
"request_method": "{{ request.method }}",
"request_uri": "{{ request.uri }}",
"request_host": "{{ request.host }}",
"request_host_name": "{{ request.hostName }}",
"request_protocol": "{{ request.protocol }}",
"request_remote_ip": "{{ request.remoteIp }}",
"request_version": "{{ request.version }}",
"request_path": "{{ request.path }}",
"request_path_segment": "{{ request.path.1 }}",
"request_headers": "{{ request.headers }}",
"request_header": "{{ request.headers.User-Agent }}",
"request_query_string": "{{ request.queryString }}",
"request_query_string_param": "{{ request.queryString.q }}",
"request_body": "{{ request.body }}",
"request_body_username": "{{ request.body.username }}",
}
{
"message": "Welcome {{ jsonPath request.json '$.name' }} {{ jsonPath request.json '$.surname' }}!"
}
{{ escapeHtml '& < \" >' }}
templatingEngine: Jinja2
services:
- name: Mock for Service1
hostname: service1.example.com
port: 8001
endpoints:
- path: "/users"
method: GET
response:
headers:
Content-Type: "application/json; charset=UTF-8"
useTemplating: true
body: "@templates/users.json.j2"
- path: "/users/{{ id }}"
method: GET
response:
headers:
Content-Type: "application/json; charset=UTF-8"
useTemplating: true
body: "@templates/user.json.j2"
{
"id": {{ id }},
"firstName": "{{ fake.first_name() }}",
"lastName": "{{ fake.last_name() }}",
"friends": [{% for n in range(range(5) | random) %}
{
"id": {{ random.int(100000, 999999) }}
}{% if not loop.last %},{% endif %}
{% endfor %}]
}
{
"users": [{% for n in range(100) %}
{
"id": {{ random.int(100000, 999999) }},
"firstName": "{{ fake.first_name() }}",
"lastName": "{{ fake.last_name() }}",
"friends": [{% for n in range(range(5) | random) %}
{
"id": {{ random.int(100000, 999999) }}
}{% if not loop.last %},{% endif %}
{% endfor %}]
}{% if not loop.last %},{% endif %}
{% endfor %}],
"total": 10
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment