다음을 통해 공유


MockResponsePlugin

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

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

플러그 인 instance 정의

{
  "name": "MockResponsePlugin",
  "enabled": true,
  "pluginPath": "~appFolder/plugins/dev-proxy-plugins.dll",
  "configSection": "mocksPlugin"
}

구성 예

{
  "mocksPlugin": {
    "mocksFile": "mocks.json"
  }
}

구성 속성

속성 Description 기본값
mocksFile 모의 응답이 포함된 파일의 경로 mocks.json
blockUnmockedRequests 모의로 생성되지 않은 요청에 대한 응답 반환 502 Bad Gateway false

명령줄 옵션

Name Description 기본값
-n, --no-mocks 모의 요청 로드 사용 안 함 false
--mocks-file 모의 응답이 포함된 파일의 경로 -

모의 파일 예제

다음은 모의 개체의 예입니다.

본문으로 응답

200 OK 응답 및 JSON 본문이 있는 요청에 대한 응답입니다.

{
  "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 찾을 수 없음 응답을 사용하여 요청에 응답합니다.

{
  "request": {
    "url": "https://graph.microsoft.com/v1.0/me/photo",
    "method": "GET"
  },
  "response": {
    "statusCode": 404
  }
}

이진 데이터로 응답

디스크의 파일에서 로드된 이진 이미지를 사용하여 요청에 응답합니다.

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

두 번째로 호출된 후에만 요청에 응답합니다.

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

요청 본문과 일치하는 응답

본문에 특정 문자열이 포함된 요청에 응답합니다.

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

모의 파일 속성

속성 Description 필수
request 응답 할 요청을 정의하는 요청 개체
response 반환할 응답을 정의하는 응답 개체

요청 개체

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

속성 Description 필수 기본값 샘플 값
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과 일치합니다. 런타임 시 개발 프록시는 각각 * 을 정규식 .*으로 변환합니다.

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

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

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

응답 개체

각 응답에는 다음과 같은 속성이 있습니다.

속성 Description 필수 기본값 샘플 값
body 요청에 대한 응답으로 보낼 본문 아니요 empty { "foo": "bar" }
statusCode 응답 HTTP 상태 코드 아니요 200 404
headers 응답에 포함할 헤더 배열 아니요 empty [{ name: "content-type", "value": "application/json" }]

설명

이진 데이터를 반환하려면 속성을 모의 body 파일에 상대적인 파일 경로 다음에 시작하는 문자열 값으로 @ 설정합니다. 예를 들어 는 @picture.jpg 모의 파일과 동일한 디렉터리에 있는 파일에 저장된 picture.jpg 이미지를 반환합니다.