I have this dredd.yml config file:
paths:
"/network":
get:
produces:
- application/json;charset=utf-8
responses:
'200':
description: Ok
schema:
"$ref": "#/definitions/NetworkResponse"
"/network/{id}":
get:
produces:
- application/json;charset=utf-8
parameters:
- name: id
in: path
description: id
required: true
type: string
enum:
- "net-76faddf092e62151"
responses:
'200':
description: Ok
schema:
"$ref": "#/definitions/NetworkIdResponse"
I want to replace {id}
in path with python dredd hooks.
I tried so:
import json
import dredd_hooks as hooks
response_stash = {}
@hooks.after("Networks > ALL ")
def save_network_id_to_stash(transaction):
# save HTTP response with IDs to the stash
response_stash[transaction['id']] = transaction['id']
print(transaction['id'])
@hooks.before("Networks > ID")
def add_network_id_to_request(transaction):
parsed_body = json.loads(response_stash['Networks > ALL'])
network_id = parsed_body['id']
transaction['fullPath'] = transaction['fullPath'].replace('net-76faddf092e62151', network_id)
Test execution was successful , but only with 1 id.
Answer from 1st operation outputs a list with objects, where are ids in ['id'].
How to replace {id}
in path and make tests for all networks?
@hooks.before("Networks > ID > 200 > application/json;charset=utf-8)
it is the correct definition of the hook name.
You can see all variables transaction['real']
, transaction['name']
and so one with
print(transaction)
within hook function.