Skip to content

Instantly share code, notes, and snippets.

@ru-rocker
Last active April 3, 2022 11:18
Show Gist options
  • Save ru-rocker/2eff871d9de798e4f7defb5fb4d39309 to your computer and use it in GitHub Desktop.
Save ru-rocker/2eff871d9de798e4f7defb5fb4d39309 to your computer and use it in GitHub Desktop.
apiVersion: apps/v1
kind: Deployment
metadata:
name: mock-endpoint
namespace: ns1
spec:
selector:
matchLabels:
name: mock-endpoint
template:
metadata:
labels:
name: mock-endpoint
spec:
volumes:
- name: mock-data
configMap:
name: mock-data
containers:
- name: mock-endpoint
image: rurocker/mock-rest-api:latest
env:
- name: MOCK_CONFIG_FILE
value: /app/mock-response.yaml
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 3000
protocol: TCP
resources:
limits:
cpu: 100m
memory: 64Mi
volumeMounts:
- name: mock-data
mountPath: /app/mock-response.yaml
subPath: mock-response.yaml
---
apiVersion: v1
kind: Service
metadata:
name: mock-endpoint
namespace: ns1
spec:
type: ClusterIP
ports:
- port: 3000
targetPort: 3000
protocol: TCP
selector:
name: mock-endpoint
name: sample-mock
# advertised listening address.
hostname: 0.0.0.0
# http port
port: 3000
# pre-flight options
options:
accessControlAllowOrigin: '*'
accessControlAllowCredentials: 'true'
accessControlAllowHeaders: Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With
accessControlAllowMethods: POST,HEAD,PATCH, OPTIONS, GET, PUT
# list of routes
routes:
- method: GET
endpoint: /product/:productId
responses:
- statusCode: 400
body: |
{
"code": "400",
"message": "Bad Request",
"productId": :productId
}
# Condition for this request.
# GET /product/:productId will return response code 400 if there is no X_REQ_ID in the request header
condition:
# type: request_header, request_param, query_param and request_body
type: request_header
# the key. if the type is request_body, then it is in the form of json path.
key: X_REQ_ID
# request header value
value:
# state for comparison: absent, present or equal.
# For request param, absent state does not make sense. Do not put absent state if you type is request_param.
state: absent
# GET /product/:productId will return response code 200 if there is a X_REQ_ID in the request header with value ABC
- statusCode: 200
body: |
{
"code": "200",
"message": "OK",
"productId": :productId
}
condition:
type: request_header
key: X_REQ_ID
value: ABC
state: equal
# GET /product/:productId will return response code 202 if there is a X_REQ_ID in the request header with value other than ABC
- statusCode: 202
body: |
{
"code": "202",
"message": "Accepted",
"productId": :productId
}
condition:
type: request_header
key: X_REQ_ID
value: ABC
state: present
- method: POST
endpoint: /echo
responses:
# POST /echo will return response code 200 if there is a name field in the JSON request body.
# The request will be delayed between 100 - 1000ms
- statusCode: 200
body: |
{
"code": "200",
"message": "OK"
}
# delay the response in millisecond. Generate random milliseconds time in range. Put min == max for fix time.
delay:
min: 100
max: 1000
condition:
type: request_body
key: $.name
value:
state: present
# POST /echo will return response code 400 if there is no name field in the JSON request body.
# The request will be delayed is 1000ms
- statusCode: 400
body: |
{
"code": "400",
"message": "Bad Request"
}
delay:
min: 1000
max: 1000
condition:
type: request_body
key: $.name
value:
state: absent
# create configmap
kubectl create configmap mock-data --from-file=mock-response.yaml --namespace ns1
# deployment
kubectl apply -f deployment.yaml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment