Mock Implementation with Choreo Connect¶
This Mock Implementation allows you to generate mock responses based on examples provided in the OpenAPI specification and directly get the response. For non-default cases, the exact response can be requested using the Prefer
and Accept
headers.
Important
This is currently supported only when Choreo Connect is used as the Gateway.
Step 1 - Create the interface of the API¶
Follow the instructions below to enable mock response generation based on the OpenAPI specification:
-
Start the WSO2 API Manager server with Choreo Connect as the gateway.
-
Open API Publisher in the browser and create a REST API with the following values.
Field Value Name SwaggerPetstore Context /v3 Version 1.0.6 Endpoint Leave the endpoint field empty.
Step 2 - Implement the API¶
Update the OpenAPI Specification¶
-
Navigate to API Definition under API Configurations to view the OpenAPI specification.
-
Click edit to add mock examples to required resources.
Replace the default resources with the resource definitions given below.
-
Add response examples to OpenAPI Specification.
With OpenAPI 3.0 you can provide the expected payloads and headers in the following formats.
Single Example for an Operation
<resource path>: <operation>: responses: <response code>: description: <description> headers: <header>: example: <header example value> content: <media type>: example: <example>
/pet/findByStatus: get: responses: '200': description: OK headers: x-wso2-example: example: example header value content: application/json: example: mock response: hello world
Multiple Examples for an Operation
<resource path>: <operation>: responses: <response code>: description: <description> headers: <header>: example: <header example value> content: <media type>: examples: <example reference> value: <example>
/pet/findByStatus: get: responses: 50X: description: Service Unavailable headers: x-wso2-example: example: example header value content: application/json: examples: ref1: value: mock response: hello world ref2: value: mock response: Welcome default: description: default response headers: x-wso2-example: example: default header value content: application/json: examples: ref1: value: mock response: default hello world ref2: value: mock response: default Welcome
Place Holder Usage response code
Can be 3 digit status code or a wildcard format like 2XX. default
can be also provided instead of a particular status code.header
Header name. You can provide multiple headers similarly under headers
.media type
Mock response content type. Provide allowed content types for the resource. When accept header is presented in a request, Choreo Connect will return the content which suited to accepted media type among them. example
Provide the content body as a simple string or as an object. If an object is given as the example
, it will be parsed to JSON format.Note
For more information on defining response body examples in open API specification, follow Request and Response Body Examples.
Example
If you take the example in Multiple Examples for an Operation and update the API definition with it, you can use
Prefer
header andAccept
header to get different examples for a resource if multiple examples were defined for the resource. UsingPrefer
header you can specify whichcode
and/orexample
should be returned as the response for the mock request.Invoking
GET
for/pet/findByStatus
will return the default example as given below.curl -X GET https://localhost:9095/v3/1.0.6/pet/findByStatus
< HTTP/1.1 200 OK < content-type: application/json < x-wso2-example: "default header value" < {"mock response":"default hello world"}
Invoking
GET
for/pet/findByStatus
with the headerPrefer
will return the matched example for the particular code and the example reference.curl -H 'Prefer: code=503, example=ref2' -X GET https://localhost:9095/v3/1.0.6/pet/findByStatus
< HTTP/1.1 503 Service Unavailable < content-type: application/json < x-wso2-example: "example header value" < {"mock response":"Welcome"}
Behavioral Characteristics
The following are the behavioral characteristics of the Mock Implementation with Choreo Connect.
Example not provided
- If the invoked resource does not contain mock response examples provided in the api definition, then a response with
501, Not Implemented
status code would be returned with900871
error code.
Preferred Status Code in the Prefer Header
-
If the request has a valid integer value as the code preference, Choreo connect picks the most matched response examples from the OpenAPI specification checking on the exact codes and wild cards matches. Status code of the response will be same as the code preference given in the request. However, if there are no response examples matching the preferred status code, then
501, Not Implemented
status code would be returned with900871
error code. -
If the request does not have a code preference, Choreo Connect will check for examples under
default
responses, if even no default responses are defined, then picks a one out of the response examples.
Accept Type in the Accept Header
-
If the request has accept-types, Choreo connect will pick examples defined under a matching media type. However, if there are no matches for accept types in the examples, then
501, Not Implemented
status code would be returned with900871
error code. -
If the request does not have accept-types, Choreo connect will check for examples with 'application/json' as the media type. If there are no examples for 'application/json', then picks one out of available media-type examples.
Preference not found
- If the request has an example preference,
Choreo Connect will pick the example content with the matching example name from the example list we got after applying above steps. If there's no matching example found, then
501, Not Implemented
status code would be returned with900871
error code.
- If the invoked resource does not contain mock response examples provided in the api definition, then a response with
-
After adding examples to the OpenAPI specification, remember to click Update Content and Save.
Change the Endpoint Type¶
-
Navigate to Endpoints under API Configurations.
-
Select Mock Implementation as the endpoint type by clicking Add.
-
Select Choreo Connect as the gateway type.
-
Click proceed if a dialog box appears to disregard "mock endpoint implementation scripts".
-
View the mock examples that would be returned for each resource. If you need to modify the examples, you have to edit the API definition as mentioned before.
-
Click Save to enable mock implementation with OAS examples.
Step 3 - Deploy the API¶
Deploy the API in order to make the API available in the respective Gateway.
Step 4 - Test the API via the Publisher¶
Once deployed to the Gateway, you can test the REST type APIs via the Publisher Portal.
Step 5 - Publish the API as a Prototype¶
Important
- By default, from WSO2 API Manager 4.1.0 onwards, security is enabled for all the resources of the Prototype API. As a result, you need a subscription to invoke the API.
-
Skip this step if you have disabled security for all the resources in the API.
Follow the instructions below to attach Business Plans to the API.
-
Navigate to Portal Configurations and click Subscriptions.
-
Select the required Business Plans and click Save.
-
Follow the instructions below to publish the API to the Developer Portal as a Prototype:
-
Navigate to the Lifecycle page listed under Publish.
For both options, When creating an API with the Mock Implementation and When creating an API with an actual backend URL, you will have the option to either promote your API to the PRE-RELEASED state or to the PUBLISHED state in the API lifecycle.
-
Click Pre-Release to publish the API as a Prototype API to the Developer Portal
Step 6 - Invoke the API¶
-
Click View in Dev Portal to navigate to the Developer Portal.
Note
If you have enabled security for the prototype API, follow the Subscribe to an API guide to subscribe and obtain an access token to invoke the prototype API.
-
Click Try Out to navigate to the API Console.
-
If you have enabled security, you can either use the access token that you got from the above step or use the GET TEST KEY option.
Note
Skip this step if you have disabled security for the API, and leave the Access Token field empty.
-
Expand any method and click Try it out.
-
Click Execute to invoke the API.
-
You can use the
Prefer
header and theAccept
header to get different examples for a resource if multiple examples were defined for the resource. -
Use the
Prefer
header to specify whichcode
and/orexample
should be returned as the response for the mock request.
-
See also¶
- Deploy a REST API with a Mock Implementation in Choreo Connect
- Expose an existing backend implementation as a Pre-Released API