使用开发代理是模拟 API 的最简单方法。 无论是构建前端和 API 尚未准备就绪,都需要将后端与外部服务集成,或者想要使用不同的响应测试应用程序,开发代理可帮助你模拟 API 响应。 使用开发代理的出色功能是,它不需要对应用程序代码进行任何更改。 为应用程序与之交互的任何 API 定义模拟响应,开发代理会截获请求,并使用定义的模拟响应进行响应。
若要模拟 API 响应,需要执行两项作:
- 使用模拟响应来创建一个文件。
- 将开发代理配置为使用模拟响应。
小窍门
如果使用 Visual Studio Code,请考虑安装 开发代理工具包 扩展。 它极大地简化了对开发代理配置文件的操作。
创建包含模拟响应的文件
开发代理使用 MockResponsePlugin
来模拟 API 响应。 该插件允许你定义一组模拟响应。 可以在 单独的文件中定义模拟。 以下代码片段演示了对GET
请求至https://jsonplaceholder.typicode.com/posts/1
的简单模拟响应。
{
"$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v0.29.2/mockresponseplugin.schema.json",
"mocks": [
{
"request": {
"url": "https://jsonplaceholder.typicode.com/posts/1",
"method": "GET"
},
"response": {
"statusCode": 200,
"body": {
"userId": 1,
"id": 1,
"title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
"body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
},
"headers": [
{
"name": "Date",
"value": "Wed, 19 Feb 2025 09:03:37 GMT"
},
{
"name": "Content-Type",
"value": "application/json; charset=utf-8"
},
{
"name": "Content-Length",
"value": "292"
},
// [...] trimmed for brevity
]
}
}
]
}
小窍门
可以使用MockGeneratorPlugin
根据截获的请求生成模拟文件,而不是手动创建。
优先顺序
开发代理按照你在 mocks 文件中定义的顺序匹配模拟。 如果使用相同的 URL 和方法定义多个响应,则开发代理将使用第一个匹配的响应。
当使用以下配置时,代理对发往 GET
的所有 https://graph.microsoft.com/v1.0/me/photo
请求都以 500 Internal Server Error
进行响应。
{
"$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v0.29.2/mockresponseplugin.schema.json",
"mocks": [
{
"request": {
"url": "https://graph.microsoft.com/v1.0/me/photo",
"method": "GET"
},
"response": {
"statusCode": 500
}
},
{
"request": {
"url": "https://graph.microsoft.com/v1.0/me/photo",
"method": "GET"
},
"response": {
"statusCode": 404
}
}
]
}
通配符支持
开发代理支持在 URL 属性中使用通配符。 可以使用星号字符 (*
) 来匹配 URL 中的任何一系列字符。
使用以下配置时,开发代理会响应所有请求,以获取任何用户的具有相同响应的配置文件。
{
"$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v0.29.2/mockresponseplugin.mocksfile.schema.json",
"mocks": [
{
"request": {
"url": "https://graph.microsoft.com/v1.0/users/*"
},
"response": {
"body": {
"@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users/$entity",
"businessPhones": ["+1 425 555 0109"],
"displayName": "Adele Vance",
"givenName": "Adele",
"jobTitle": "Product Marketing Manager",
"mail": "AdeleV@M365x214355.onmicrosoft.com",
"mobilePhone": null,
"officeLocation": "18/2111",
"preferredLanguage": "en-US",
"surname": "Vance",
"userPrincipalName": "AdeleV@M365x214355.onmicrosoft.com",
"id": "87d349ed-44d7-43e1-9a83-5f2406dee5bd"
}
}
}
]
}
使用以下配置时,当请求获取任何用户照片的二进制文件时,Dev Proxy 会从磁盘返回相同的映像。
{
"$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v0.29.2/mockresponseplugin.mocksfile.schema.json",
"mocks": [
{
"request": {
"url": "https://graph.microsoft.com/v1.0/users/*/photo/$value"
},
"response": {
"body": "@picture.jpg",
"headers": [
{
"name": "content-type",
"value": "image/jpeg"
}
]
}
}
]
}
使用以下配置时,当请求获取具有任何查询字符串参数的当前用户配置文件时,Dev Proxy 将返回相同的响应。
{
"$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v0.29.2/mockresponseplugin.mocksfile.schema.json",
"mocks": [
{
"request": {
"url": "https://graph.microsoft.com/v1.0/me?*"
},
"response": {
"body": {
"@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users/$entity",
"businessPhones": [
"+1 412 555 0109"
],
"displayName": "Megan Bowen",
"givenName": "Megan",
"jobTitle": "Auditor",
"mail": "MeganB@M365x214355.onmicrosoft.com",
"mobilePhone": null,
"officeLocation": "12/1110",
"preferredLanguage": "en-US",
"surname": "Bowen",
"userPrincipalName": "MeganB@M365x214355.onmicrosoft.com",
"id": "48d31887-5fad-4d73-a9f5-3c356e68a038"
}
}
}
]
}
用文件内容进行响应
若要保持模拟文件干净有序,可以将响应的内容存储在单独的文件中,并在模拟文件中引用它。 若要指示开发代理从某个文件加载模拟响应正文,请将 body
属性设置为 @
,后跟相对于 mocks 文件的文件路径。
例如,以下模拟响应配置指示开发代理使用与 mocks 文件位于同一文件夹中的 https://graph.microsoft.com/v1.0/me
文件的内容来响应对 response.json
的任何请求。
{
"$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v0.29.2/mockresponseplugin.mocksfile.schema.json",
"mocks": [
{
"request": {
"url": "https://graph.microsoft.com/v1.0/me",
"method": "GET"
},
"response": {
"body": "@response.json",
"headers": [
{
"name": "content-type",
"value": "application/json; odata.metadata=minimal"
}
]
}
}
]
}
使用@
标记适用于文本和二进制文件。
将开发代理配置为使用模拟响应
创建模拟文件后,需要将开发代理配置为使用模拟响应。 若要将开发代理配置为模拟响应,请向 MockResponsePlugin
文件中的插件列表添加。
{
"$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v0.29.2/rc.schema.json",
"plugins": [
{
"name": "MockResponsePlugin",
"enabled": true,
"pluginPath": "~appFolder/plugins/DevProxy.Plugins.dll",
"configSection": "mockResponsePlugin"
}
],
"urlsToWatch": [
"https://jsonplaceholder.typicode.com/*"
],
"mockResponsePlugin": {
"$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v0.29.2/mockresponseplugin.schema.json",
"mocksFile": "mocks.json"
},
"logLevel": "information",
"newVersionNotification": "stable",
"showSkipMessages": true
}
首先,将 MockResponsePlugin
添加到插件列表。 你包含对其配置部分的引用,在其中指定 mocks 文件的路径。
当您启动 Dev Proxy 时,它会读取模拟文件,并使用模拟响应来回应与定义的模拟匹配的请求。
未模拟请求支持
开发代理支持在代理截获未模拟请求时抛出错误。 使未模拟的请求失败的功能对于识别你在 mocks 文件中错过的请求很有用。
若要启用此功能,请在 blockUnmockedRequests
文件中向 MockResponsePlugin 配置部分添加和启用设置。
{
"mocksPlugin": {
"$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v0.29.2/mockresponseplugin.schema.json",
"mocksFile": "mocks.json",
"blockUnmockedRequests": true
}
}
当 Dev Proxy 截获无法模拟的请求时,它将返回一个响应 502 Bad Gateway
。
后续步骤
详细了解 MockResponsePlugin。
示例
另请参阅相关的开发代理示例: