模擬回應。
外掛程式實例定義
{
"name": "MockResponsePlugin",
"enabled": true,
"pluginPath": "~appFolder/plugins/DevProxy.Plugins.dll",
"configSection": "mocksPlugin"
}
設定範例
{
"mocksPlugin": {
"$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v1.0.0/mockresponseplugin.schema.json",
"mocksFile": "mocks.json"
}
}
組態屬性
| 房產 | 說明 | 預設 |
|---|---|---|
mocksFile |
包含模擬回應的檔案路徑 | mocks.json |
blockUnmockedRequests |
針對未仿真的要求傳回 502 Bad Gateway 回應 |
false |
命令列選項
| 名稱 | 說明 | 預設 |
|---|---|---|
-n, --no-mocks |
停用載入模擬要求 | false |
--mocks-file |
包含模擬回應的檔案路徑 | - |
模擬檔案範例
以下是模擬物件的範例。
以本文回應
使用 200 OK 回應和 JSON 主體來回應要求。
{
"$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v1.0.0/mockresponseplugin.mocksfile.schema.json",
"mocks": [
{
"request": {
"url": "https://graph.microsoft.com/v1.0/me",
"method": "GET"
},
"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"
},
"headers": [
{
"name": "content-type",
"value": "application/json; odata.metadata=minimal"
}
]
}
}
]
}
回應錯誤
使用 404 找不到回應來回應要求。
{
"$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v1.0.0/mockresponseplugin.mocksfile.schema.json",
"mocks": [
{
"request": {
"url": "https://graph.microsoft.com/v1.0/me/photo",
"method": "GET"
},
"response": {
"statusCode": 404
}
}
]
}
回應二進位數據
回應從磁碟上的檔案載入二進位映像的要求。
{
"$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v1.0.0/mockresponseplugin.mocksfile.schema.json",
"mocks": [
{
"request": {
"url": "https://graph.microsoft.com/v1.0/users/*/photo/$value",
"method": "GET"
},
"response": {
"body": "@picture.jpg",
"headers": [
{
"name": "content-type",
"value": "image/jpeg"
}
]
}
}
]
}
依 nth 要求回應
只有在第二次呼叫要求之後,才會回應要求。
{
"$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v1.0.0/mockresponseplugin.mocksfile.schema.json",
"mocks": [
{
"request": {
"url": "https://graph.microsoft.com/v1.0/external/connections/*/operations/*",
"method": "GET",
"nth": 2
},
"response": {
"statusCode": 200,
"body": {
"id": "1.neu.0278337E599FC8DBF5607ED12CF463E4.6410CCF8F6DB8758539FB58EB56BF8DC",
"status": "completed",
"error": null
}
}
}
]
}
回應比對要求本文
回應包含本文中特定字串的要求。
{
"$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v1.0.0/mockresponseplugin.mocksfile.schema.json",
"mocks": [
{
"request": {
"url": "https://login.microsoftonline.com/fa15d692-e9c7-4460-a743-29f29522229/oauth2/v2.0/token",
"method": "POST",
"bodyFragment": "scope=https%3A%2F%2Fapi.contoso.com%2FDocuments.Read"
},
"response": {
"headers": [
{
"name": "Content-Type",
"value": "application/json; charset=utf-8"
}
],
"body": {
"token_type": "Bearer",
"expires_in": 3599,
"ext_expires_in": 3599,
"access_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJSU..."
}
}
}
]
}
回應中的鏡像要求資料
使用預留位置,以 @request.body.* 鏡像要求本文中值的資料來回應。
{
"$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v1.2.0/mockresponseplugin.mocksfile.schema.json",
"mocks": [
{
"request": {
"url": "https://graph.microsoft.com/v1.0/users",
"method": "POST"
},
"response": {
"statusCode": 201,
"body": {
"@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users/$entity",
"id": "12345678-1234-1234-1234-123456789abc",
"businessPhones": "@request.body.businessPhones",
"displayName": "@request.body.displayName",
"givenName": "@request.body.givenName",
"jobTitle": "@request.body.jobTitle",
"mail": "@request.body.mail",
"userPrincipalName": "@request.body.userPrincipalName",
"accountEnabled": "@request.body.accountEnabled",
"createdDateTime": "2024-01-15T10:30:00Z"
},
"headers": [
{
"name": "Content-Type",
"value": "application/json; odata.metadata=minimal"
},
{
"name": "Location",
"value": "https://graph.microsoft.com/v1.0/users/12345678-1234-1234-1234-123456789abc"
}
]
}
}
]
}
鑑於以下請求:
POST https://graph.microsoft.com/v1.0/users
Content-Type: application/json
{
"displayName": "Megan Bowen",
"userPrincipalName": "MeganB@M365x214355.onmicrosoft.com",
"accountEnabled": true,
"givenName": "Megan",
"surname": "Bowen",
"jobTitle": "Product Manager"
}
回應是:
HTTP/1.1 200 Connection Established
Content-Length: 0
HTTP/1.1 201 Created
Cache-Control: no-store
x-ms-ags-diagnostic:
Strict-Transport-Security:
request-id: 12345678-1234-1234-1234-123456789abc
client-request-id: 12345678-1234-1234-1234-123456789abc
Date: 9/10/2025 10:28:35 AM
Content-Type: application/json; odata.metadata=minimal
Location: https://graph.microsoft.com/v1.0/users/12345678-1234-1234-1234-123456789abc
OData-Version: 4.0
Content-Length: 648
{
"@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users/$entity",
"id": "12345678-1234-1234-1234-123456789abc",
"businessPhones": null,
"displayName": "Megan Bowen",
"givenName": "Megan",
"jobTitle": "Product Manager",
"mail": null,
"mobilePhone": null,
"officeLocation": null,
"preferredLanguage": null,
"surname": "Bowen",
"userPrincipalName": "MeganB@M365x214355.onmicrosoft.com",
"accountEnabled": true,
"createdDateTime": "2024-01-15T10:30:00Z",
"department": null,
"companyName": null,
"city": null,
"country": null,
"postalCode": null,
"state": null,
"streetAddress": null,
"usageLocation": null
}
模擬檔案屬性
| 房產 | 說明 | 為必填項目 |
|---|---|---|
request |
定義 回應要求的要求物件 | 是的 |
response |
定義要傳回之回應的回應物件 | 是的 |
請求物件
每個要求都有下列屬性:
| 房產 | 說明 | 為必填項目 | 預設值 | 範例值 |
|---|---|---|---|---|
url |
要回應之 API 端點的絕對 URL | 是的 | https://jsonplaceholder.typicode.com/posts |
|
method |
用來比對要求與的 HTTP 動詞 url |
否 | GET |
GET |
nth |
判斷 Proxy 應該只在第 n 次攔截要求之後才回應 | 否 | 2 |
|
bodyFragment |
要求本文中應存在的字串 | 否 | foo |
備註
如果您想要比對 URL 中的任何字元系列,請使用 屬性中的星號 (*url)。 例如,https://jsonplaceholder.typicode.com/* 符合 https://jsonplaceholder.typicode.com/posts 和 https://jsonplaceholder.typicode.com/comments。 在執行時間上,Dev Proxy 會將每個 * 轉換成正則表示式 .*。
定義模擬時,請先放置最特定的模擬。 例如,如果您有兩個模擬,一個用於 https://jsonplaceholder.typicode.com/posts ,一個用於 https://jsonplaceholder.typicode.com/*,先放置第一個模擬。 否則,Dev Proxy 會先比對第二個模擬,並傳回所有要求的回應 https://jsonplaceholder.typicode.com/* 。
nth如果您需要傳送不同的要求 URL,請使用 屬性。 例如,使用它來模擬長時間執行的作業。 第一次呼叫 API 時,它會傳回含有訊息的 inprogress 回應。 第二次呼叫 API 時,它會傳回包含訊息的 completed 回應。 如需 屬性的詳細資訊 nth ,請參閱 模擬 nth 要求。
bodyFragment使用 屬性,您可以根據本文內容比對要求。 例如,如果您想要比對內文 foo 中包含字串的要求,請將 bodyFragment 屬性設定為 foo。 開發 Proxy 只會針對 以外的bodyFragment要求使用GET。
Response 物件
每個回應都有下列屬性:
| 房產 | 說明 | 為必填項目 | 預設值 | 範例值 |
|---|---|---|---|---|
body |
當做要求回應傳送的本文 | 否 | 空 | { "foo": "bar" } |
statusCode |
回應 HTTP 狀態代碼 | 否 | 200 |
404 |
headers |
要包含在回應中的標頭陣列 | 否 | 空 | [{ name: "content-type", "value": "application/json" }] |
備註
如果您想要傳回二進位數據,請將 body 屬性設定為字串值,其開頭 @ 為 ,後面接著相對於模擬檔案的檔案路徑。 例如, @picture.jpg 傳回儲存在檔案中 picture.jpg 與模擬檔案相同目錄中的影像。
若要在回應中鏡像要求資料,請在 @request.body.* 回應內文中使用預留位置。 例如, @request.body.displayName 從要求本文傳回屬性的 displayName 值。 此功能適用於巢狀物件和陣列,可讓您建立反映傳入請求資料的動態回應。 預留位置取代支援各種 JSON 值類型,包括字串、數字、布林值和複雜物件。