This repository provides a Golang implementation of a mock HTTP server that allows you to easily define and manage mock endpoints through a configuration YAML file. It also offers various command-line flags for additional configuration.
- YAML Configuration: Configure mock endpoints, including expected requests, responses, and more, using a user-friendly YAML file.
- Multiple Endpoints: Define multiple mock endpoints with different behaviors and responses.
- Customizable Responses: Set status codes, headers, and body content for each response to simulate various scenarios.
- Command-Line Flags: Override configuration options like port and TLS settings using command-line flags.
- Basic Authentication: Secure your mock server by enabling basic authentication with username and password flags.
- TLS Support: Enable secure communication over HTTPS by providing TLS certificate and key files.
- Metrics: Expose default go runtime metrics like request count, latency etc at
/metrics
endpoint
-
Clone the repository:
git clone https://github.com/wasim-nihal/mock-http-server.git
-
Install dependencies:
go mod download
-
Build the executable:
go build -o mock-server .
-
Run the server:
./mock-server \ -server.config config.yaml \ # Path to your configuration file -port 8081 \ # Optional port (default: 8080) -basicauth.username user # Optional basic auth username -basicauth.password password # Optional basic auth password -tls # Optional flag to enable TLS (requires certificate and key) -tlsCertFile cert.pem # Optional path to TLS certificate file (if -tls is set) -tlsKeyFile key.pem # Optional path to TLS key file (if -tls is set)
The YAML file defines the mock endpoints and their expected behavior. Here's an example structure:
# List of mock endpoints
endpoints:
# endpoint path
/hello:
# http method type
- method: GET
# response content type
content: text/plain
# response body
body: Hello, World! (GET)
# response status
status: 200
- method: POST
content: text/plain
body: Hello, World! (POST)
status: 200
/bye:
- method: GET
content: text/plain
body: Goodbye!
status: 400
# Add more endpoints with their respective configurations
- ...
apiVersion: v1
kind: Pod
metadata:
name: mock-server-pod
labels:
app: mock-server
spec:
containers:
- name: mock-server-container
image: nihalwasim/mock-http-server:latest
imagePullPolicy: IfNotPresent
ports:
- containerPort: 8080
command: ["/mock-server"]
args:
- "--server.config=/etc/config/server.yaml"
volumeMounts:
- name: config-volume
mountPath: /etc/config
readOnly: true
volumes:
- name: config-volume
configMap:
name: my-configmap
---
apiVersion: v1
kind: ConfigMap
metadata:
name: my-configmap
data:
server.yaml: |
endpoints:
/hello:
- method: GET
content: text/plain
body: Hello, World! (GET)
status: 200
- method: POST
content: text/plain
body: Hello, World! (POST)
status: 200
/bye:
- method: GET
content: text/plain
body: Goodbye!
status: 400
---
apiVersion: v1
kind: Service
metadata:
name: mock-server-service
spec:
selector:
app: mock-server
ports:
- protocol: TCP
port: 8080
targetPort: 8080
This project is licensed under the Apache-2.0 license License. See the LICENSE file for details.
- Replace
config.yaml
with the actual name of your configuration file. - Adjust the command-line flags and YAML example as necessary based on your specific implementation.