다음을 통해 공유


MockResponsePlugin

응답을 시뮬레이션합니다.

GitHub API에 대한 요청에 대한 응답을 시뮬레이션하는 개발자 프록시가 있는 명령 프롬프트의 스크린샷

플러그 인 인스턴스 정의

{
  "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
}

Mocks 파일 속성

재산 설명 필수
request 응답할 요청을 정의하는 요청 개체
response 반환할 응답을 정의하는 응답 개체

요청 객체

각 요청에는 다음과 같은 속성이 있습니다.

재산 설명 필수 기본값 샘플 값
url 응답할 API 엔드포인트에 대한 절대 URL https://jsonplaceholder.typicode.com/posts
method 요청을 일치시킬 때 사용되는 HTTP 동사 url 아니요 GET GET
nth n번째 시간 동안 요청을 가로채는 경우에만 프록시가 응답해야 한다고 결정합니다. 아니요 2
bodyFragment 요청 본문에 있어야 하는 문자열 아니요 foo

비고

URL의 일련의 문자와 일치하려면 속성에 * 별표(url)를 사용합니다. 예를 들어 https://jsonplaceholder.typicode.com/*https://jsonplaceholder.typicode.com/posts, https://jsonplaceholder.typicode.com/comments과 일치합니다. 런타임 시 개발자 프록시는 각각 * 을 정규식 .*으로 변환합니다.

모의 개체를 정의할 때 가장 구체적인 모의 개체를 먼저 배치합니다. 예를 들어 두 개의 모의 모의 항목이 있는 경우 하나는 for https://jsonplaceholder.typicode.com/posts 와 1에 대해 https://jsonplaceholder.typicode.com/*하나씩 첫 번째 모의 모의 개체를 먼저 배치합니다. 그렇지 않으면 개발자 프록시가 먼저 두 번째 모의 개체와 일치하고 모든 요청에 대한 https://jsonplaceholder.typicode.com/* 응답을 반환합니다.

동일한 요청 URL에 nth 다른 항목을 보내야 하는 경우 속성을 사용합니다. 예를 들어 장기 실행 작업을 시뮬레이션하는 데 사용합니다. API를 처음 호출하면 메시지와 함께 응답을 반환합니다 inprogress . API를 두 번째로 호출하면 메시지와 함께 응답을 반환합니다 completed . 속성에 대한 nth 자세한 내용은 Mock nth 요청을 참조하세요.

속성을 bodyFragment 사용하여 본문 내용에 따라 요청을 일치시킬 수 있습니다. 예를 들어 본문에 문자열이 포함된 foo 요청을 일치하려면 속성을 bodyFragment.로 설정합니다foo. 개발 프록시는 bodyFragmentGET.

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 값 형식을 지원합니다.

다음 단계