python - 替换路径dredd钩子中的ID
问题描述
我有这个 dredd.yml 配置文件:
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"
我想{id}
用 python dredd 钩子替换路径。我试过这样:
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)
测试执行成功,但只有 1 个 id。第一个操作的答案输出一个包含对象的列表,其中 id 在 ['id'] 中。如何替换{id}
路径并对所有网络进行测试?
解决方案
@hooks.before("Networks > ID > 200 > application/json;charset=utf-8)
它是钩子名称的正确定义。
您可以看到所有变量transaction['real']
,transaction['name']
以及一个带有
print(transaction)
挂钩函数的变量。
推荐阅读
- python - 可以连接到本地网络上的套接字,但不能连接到单独的套接字(python 套接字)
- html - 我如何居中在容器中间?
- c# - 如何设置是手动为EF中的虚拟属性修改的,我已经知道如何设置普通属性和集合
- javascript - 将 lang 替换为路由名称
- python - 如何为不同的产品设置不同的尺寸
- python - 并行化 for 循环的所有迭代
- google-app-engine - asia-south1 区域没有足够的资源来满足请求
- flutter - Flutter 错误:找不到正确的提供程序
- >
- flutter - 当textformfield不为空时颤动如何更改提交按钮颜色?
- flutter - 在flutter中同步gradle文件时接收java.lang.StackOverflowError