Mock request and response pairs
Use the Kong Service Virtualization plugin to mock two request-response pairs.
Trigger each mock by including the test name in the X-VirtualRequest
header, for example:
X-VirtualRequest: TestCase1
or X-VirtualRequest: TestCase2
.
Prerequisites
- The Kong Service Virtualization plugin is installed.
Add this section to your declarative configuration file:
_format_version: "3.0"
plugins:
- name: kong-service-virtualization
config:
virtual_tests:
- name: TestCase1
requestHttpMethod: POST
requestHash: '0296217561490155228da9c17fc555cf9db82d159732f3206638c25f04a285c4'
responseHttpStatus: '200'
responseContentType: application/json
response: eyJtZXNzYWdlIjogIkEgQmlnIFN1Y2Nlc3MhIn0=
- name: TestCase2
requestHttpMethod: GET
requestHash: e2c319e4ef41706e2c0c1b266c62cad607a014c59597ba662bef6d10a0b64a32
responseHttpStatus: '200'
responseContentType: application/json
response: eyJtZXNzYWdlIjogIkFub3RoZXIgU3VjY2VzcyEifQ==
Make the following request:
curl -i -X POST http://localhost:8001/plugins/ \
--header "Accept: application/json" \
--header "Content-Type: application/json" \
--data '
{
"name": "kong-service-virtualization",
"config": {
"virtual_tests": [
{
"name": "TestCase1",
"requestHttpMethod": "POST",
"requestHash": "0296217561490155228da9c17fc555cf9db82d159732f3206638c25f04a285c4",
"responseHttpStatus": "200",
"responseContentType": "application/json",
"response": "eyJtZXNzYWdlIjogIkEgQmlnIFN1Y2Nlc3MhIn0="
},
{
"name": "TestCase2",
"requestHttpMethod": "GET",
"requestHash": "e2c319e4ef41706e2c0c1b266c62cad607a014c59597ba662bef6d10a0b64a32",
"responseHttpStatus": "200",
"responseContentType": "application/json",
"response": "eyJtZXNzYWdlIjogIkFub3RoZXIgU3VjY2VzcyEifQ=="
}
]
}
}
'
echo "
apiVersion: configuration.konghq.com/v1
kind: KongClusterPlugin
metadata:
name: kong-service-virtualization
namespace: kong
annotations:
kubernetes.io/ingress.class: kong
labels:
global: 'true'
config:
virtual_tests:
- name: TestCase1
requestHttpMethod: POST
requestHash: '0296217561490155228da9c17fc555cf9db82d159732f3206638c25f04a285c4'
responseHttpStatus: '200'
responseContentType: application/json
response: eyJtZXNzYWdlIjogIkEgQmlnIFN1Y2Nlc3MhIn0=
- name: TestCase2
requestHttpMethod: GET
requestHash: e2c319e4ef41706e2c0c1b266c62cad607a014c59597ba662bef6d10a0b64a32
responseHttpStatus: '200'
responseContentType: application/json
response: eyJtZXNzYWdlIjogIkFub3RoZXIgU3VjY2VzcyEifQ==
plugin: kong-service-virtualization
" | kubectl apply -f -
Prerequisite: Configure your Personal Access Token
terraform {
required_providers {
konnect = {
source = "kong/konnect"
}
}
}
provider "konnect" {
personal_access_token = "$KONNECT_TOKEN"
server_url = "https://us.api.konghq.com/"
}
Add the following to your Terraform configuration to create a Konnect Gateway Plugin:
resource "konnect_gateway_plugin_kong_service_virtualization" "my_kong_service_virtualization" {
enabled = true
config = {
virtual_tests = [
{
name = "TestCase1"
requestHttpMethod = "POST"
requestHash = "0296217561490155228da9c17fc555cf9db82d159732f3206638c25f04a285c4"
responseHttpStatus = "200"
responseContentType = "application/json"
response = "eyJtZXNzYWdlIjogIkEgQmlnIFN1Y2Nlc3MhIn0="
},
{
name = "TestCase2"
requestHttpMethod = "GET"
requestHash = "e2c319e4ef41706e2c0c1b266c62cad607a014c59597ba662bef6d10a0b64a32"
responseHttpStatus = "200"
responseContentType = "application/json"
response = "eyJtZXNzYWdlIjogIkFub3RoZXIgU3VjY2VzcyEifQ=="
} ]
}
control_plane_id = konnect_gateway_control_plane.my_konnect_cp.id
}
Add this section to your declarative configuration file:
_format_version: "3.0"
plugins:
- name: kong-service-virtualization
service: serviceName|Id
config:
virtual_tests:
- name: TestCase1
requestHttpMethod: POST
requestHash: '0296217561490155228da9c17fc555cf9db82d159732f3206638c25f04a285c4'
responseHttpStatus: '200'
responseContentType: application/json
response: eyJtZXNzYWdlIjogIkEgQmlnIFN1Y2Nlc3MhIn0=
- name: TestCase2
requestHttpMethod: GET
requestHash: e2c319e4ef41706e2c0c1b266c62cad607a014c59597ba662bef6d10a0b64a32
responseHttpStatus: '200'
responseContentType: application/json
response: eyJtZXNzYWdlIjogIkFub3RoZXIgU3VjY2VzcyEifQ==
Make sure to replace the following placeholders with your own values:
-
serviceName|Id
: Theid
orname
of the service the plugin configuration will target.
Make the following request:
curl -i -X POST http://localhost:8001/services/{serviceName|Id}/plugins/ \
--header "Accept: application/json" \
--header "Content-Type: application/json" \
--data '
{
"name": "kong-service-virtualization",
"config": {
"virtual_tests": [
{
"name": "TestCase1",
"requestHttpMethod": "POST",
"requestHash": "0296217561490155228da9c17fc555cf9db82d159732f3206638c25f04a285c4",
"responseHttpStatus": "200",
"responseContentType": "application/json",
"response": "eyJtZXNzYWdlIjogIkEgQmlnIFN1Y2Nlc3MhIn0="
},
{
"name": "TestCase2",
"requestHttpMethod": "GET",
"requestHash": "e2c319e4ef41706e2c0c1b266c62cad607a014c59597ba662bef6d10a0b64a32",
"responseHttpStatus": "200",
"responseContentType": "application/json",
"response": "eyJtZXNzYWdlIjogIkFub3RoZXIgU3VjY2VzcyEifQ=="
}
]
}
}
'
Make sure to replace the following placeholders with your own values:
-
serviceName|Id
: Theid
orname
of the service the plugin configuration will target.
echo "
apiVersion: configuration.konghq.com/v1
kind: KongPlugin
metadata:
name: kong-service-virtualization
namespace: kong
annotations:
kubernetes.io/ingress.class: kong
config:
virtual_tests:
- name: TestCase1
requestHttpMethod: POST
requestHash: '0296217561490155228da9c17fc555cf9db82d159732f3206638c25f04a285c4'
responseHttpStatus: '200'
responseContentType: application/json
response: eyJtZXNzYWdlIjogIkEgQmlnIFN1Y2Nlc3MhIn0=
- name: TestCase2
requestHttpMethod: GET
requestHash: e2c319e4ef41706e2c0c1b266c62cad607a014c59597ba662bef6d10a0b64a32
responseHttpStatus: '200'
responseContentType: application/json
response: eyJtZXNzYWdlIjogIkFub3RoZXIgU3VjY2VzcyEifQ==
plugin: kong-service-virtualization
" | kubectl apply -f -
Next, apply the KongPlugin
resource by annotating the service
resource:
kubectl annotate -n kong service SERVICE_NAME konghq.com/plugins=kong-service-virtualization
Prerequisite: Configure your Personal Access Token
terraform {
required_providers {
konnect = {
source = "kong/konnect"
}
}
}
provider "konnect" {
personal_access_token = "$KONNECT_TOKEN"
server_url = "https://us.api.konghq.com/"
}
Add the following to your Terraform configuration to create a Konnect Gateway Plugin:
resource "konnect_gateway_plugin_kong_service_virtualization" "my_kong_service_virtualization" {
enabled = true
config = {
virtual_tests = [
{
name = "TestCase1"
requestHttpMethod = "POST"
requestHash = "0296217561490155228da9c17fc555cf9db82d159732f3206638c25f04a285c4"
responseHttpStatus = "200"
responseContentType = "application/json"
response = "eyJtZXNzYWdlIjogIkEgQmlnIFN1Y2Nlc3MhIn0="
},
{
name = "TestCase2"
requestHttpMethod = "GET"
requestHash = "e2c319e4ef41706e2c0c1b266c62cad607a014c59597ba662bef6d10a0b64a32"
responseHttpStatus = "200"
responseContentType = "application/json"
response = "eyJtZXNzYWdlIjogIkFub3RoZXIgU3VjY2VzcyEifQ=="
} ]
}
control_plane_id = konnect_gateway_control_plane.my_konnect_cp.id
service = {
id = konnect_gateway_service.my_service.id
}
}
Add this section to your declarative configuration file:
_format_version: "3.0"
plugins:
- name: kong-service-virtualization
route: routeName|Id
config:
virtual_tests:
- name: TestCase1
requestHttpMethod: POST
requestHash: '0296217561490155228da9c17fc555cf9db82d159732f3206638c25f04a285c4'
responseHttpStatus: '200'
responseContentType: application/json
response: eyJtZXNzYWdlIjogIkEgQmlnIFN1Y2Nlc3MhIn0=
- name: TestCase2
requestHttpMethod: GET
requestHash: e2c319e4ef41706e2c0c1b266c62cad607a014c59597ba662bef6d10a0b64a32
responseHttpStatus: '200'
responseContentType: application/json
response: eyJtZXNzYWdlIjogIkFub3RoZXIgU3VjY2VzcyEifQ==
Make sure to replace the following placeholders with your own values:
-
routeName|Id
: Theid
orname
of the route the plugin configuration will target.
Make the following request:
curl -i -X POST http://localhost:8001/routes/{routeName|Id}/plugins/ \
--header "Accept: application/json" \
--header "Content-Type: application/json" \
--data '
{
"name": "kong-service-virtualization",
"config": {
"virtual_tests": [
{
"name": "TestCase1",
"requestHttpMethod": "POST",
"requestHash": "0296217561490155228da9c17fc555cf9db82d159732f3206638c25f04a285c4",
"responseHttpStatus": "200",
"responseContentType": "application/json",
"response": "eyJtZXNzYWdlIjogIkEgQmlnIFN1Y2Nlc3MhIn0="
},
{
"name": "TestCase2",
"requestHttpMethod": "GET",
"requestHash": "e2c319e4ef41706e2c0c1b266c62cad607a014c59597ba662bef6d10a0b64a32",
"responseHttpStatus": "200",
"responseContentType": "application/json",
"response": "eyJtZXNzYWdlIjogIkFub3RoZXIgU3VjY2VzcyEifQ=="
}
]
}
}
'
Make sure to replace the following placeholders with your own values:
-
routeName|Id
: Theid
orname
of the route the plugin configuration will target.
echo "
apiVersion: configuration.konghq.com/v1
kind: KongPlugin
metadata:
name: kong-service-virtualization
namespace: kong
annotations:
kubernetes.io/ingress.class: kong
config:
virtual_tests:
- name: TestCase1
requestHttpMethod: POST
requestHash: '0296217561490155228da9c17fc555cf9db82d159732f3206638c25f04a285c4'
responseHttpStatus: '200'
responseContentType: application/json
response: eyJtZXNzYWdlIjogIkEgQmlnIFN1Y2Nlc3MhIn0=
- name: TestCase2
requestHttpMethod: GET
requestHash: e2c319e4ef41706e2c0c1b266c62cad607a014c59597ba662bef6d10a0b64a32
responseHttpStatus: '200'
responseContentType: application/json
response: eyJtZXNzYWdlIjogIkFub3RoZXIgU3VjY2VzcyEifQ==
plugin: kong-service-virtualization
" | kubectl apply -f -
Next, apply the KongPlugin
resource by annotating the httproute
or ingress
resource:
kubectl annotate -n kong httproute konghq.com/plugins=kong-service-virtualization
kubectl annotate -n kong ingress konghq.com/plugins=kong-service-virtualization
Prerequisite: Configure your Personal Access Token
terraform {
required_providers {
konnect = {
source = "kong/konnect"
}
}
}
provider "konnect" {
personal_access_token = "$KONNECT_TOKEN"
server_url = "https://us.api.konghq.com/"
}
Add the following to your Terraform configuration to create a Konnect Gateway Plugin:
resource "konnect_gateway_plugin_kong_service_virtualization" "my_kong_service_virtualization" {
enabled = true
config = {
virtual_tests = [
{
name = "TestCase1"
requestHttpMethod = "POST"
requestHash = "0296217561490155228da9c17fc555cf9db82d159732f3206638c25f04a285c4"
responseHttpStatus = "200"
responseContentType = "application/json"
response = "eyJtZXNzYWdlIjogIkEgQmlnIFN1Y2Nlc3MhIn0="
},
{
name = "TestCase2"
requestHttpMethod = "GET"
requestHash = "e2c319e4ef41706e2c0c1b266c62cad607a014c59597ba662bef6d10a0b64a32"
responseHttpStatus = "200"
responseContentType = "application/json"
response = "eyJtZXNzYWdlIjogIkFub3RoZXIgU3VjY2VzcyEifQ=="
} ]
}
control_plane_id = konnect_gateway_control_plane.my_konnect_cp.id
route = {
id = konnect_gateway_route.my_route.id
}
}