返回排行榜

smocker-dev/smocker

TypeScriptsmocker.dev

Smocker is a simple and efficient HTTP mock server and proxy

mock-servergointegration-testingtestmockapireacttypescriptfunctional-testingproxywiremock
Star 增长趋势
Star
1.3k
Forks
71
周增长
Issues
13
5001k
2019年9月2021年12月2024年4月2026年7月
制品库npmnpm install smocker
README

Smocker

CI Docker Repository Github Release Go Report Card License

Smocker (server mock) is a simple and efficient HTTP mock server.

The documentation is available on smocker.dev.

Table of contents

Installation

With Docker

docker run -d \
  --restart=always \
  -p 8080:8080 \
  -p 8081:8081 \
  --name smocker \
  ghcr.io/smocker-dev/smocker

Manual Deployment

# This will be the deployment folder for the Smocker instance
mkdir -p /opt/smocker && cd /opt/smocker
wget -P /tmp https://github.com/smocker-dev/smocker/releases/latest/download/smocker.tar.gz
tar xf /tmp/smocker.tar.gz
nohup ./smocker -mock-server-listen-port=8080 -config-listen-port=8081 &

Healthcheck

curl localhost:8081/version

User Interface

Smocker exposes a configuration user interface. You can access it in your web browser on http://localhost:8081/.

History

Mocks

Usage

Smocker exposes two ports:

  • 8080 is the mock server port. It will expose the routes you register through the configuration port
  • 8081 is the configuration port. It's the port you will use to register new mocks. This port also exposes a user interface.

Hello, World!

To register a mock, you can use the YAML and the JSON formats. A basic mock might look like this:

# helloworld.yml
# This mock register two routes: GET /hello/world and GET /foo/bar
- request:
    # Note: the method could be omitted because GET is the default
    method: GET
    path: /hello/world
  response:
    # Note: the status could be omitted because 200 is the default
    status: 200
    headers:
      Content-Type: application/json
    body: >
      {
        "hello": "Hello, World!"
      }

- request:
    method: GET
    path: /foo/bar
  response:
    status: 204

You can then register it to the configuration server with the following command:

curl -XPOST \
  --header "Content-Type: application/x-yaml" \
  --data-binary "@helloworld.yml" \
  localhost:8081/mocks

After your mock is registered, you can query the mock server on the specified route, so that it returns the expected response to you:

$ curl -i localhost:8080/hello/world
HTTP/1.1 200 OK
Content-Type: application/json
Date: Thu, 05 Sep 2019 15:49:32 GMT
Content-Length: 31

{
  "hello": "Hello, World!"
}

To cleanup the mock server without restarting it, you can execute the following command:

curl -XPOST localhost:8081/reset

For more advanced usage, please read the project's documentation.

Mock format schema

A JSON Schema describing the mock format lives at docs/mock.schema.json. It is generated from and kept in sync with the Go types (server/types): the example mocks under tests/data are validated against it in CI, so it stays accurate. The documentation references this file as the canonical schema.

Editors can use it for autocompletion and validation. With the YAML language server (VS Code, Neovim, …), add this line at the top of a mocks file:

# yaml-language-server: $schema=https://raw.githubusercontent.com/smocker-dev/smocker/main/docs/mock.schema.json
- request:
    path: /hello
  response:
    body: '{"message": "Hello, World!"}'

Development

Backend

The backend is written in Go. You can use the following commands to manage the development lifecycle:

  • make start: start the backend in development mode, with live reload
  • make build, make VERSION=xxx build: compile the code and generate a binary
  • make lint: run static analysis on the code
  • make format: automatically format the backend code
  • make test: execute unit tests
  • make test-integration: execute integration tests

Frontend

The frontend is written with TypeScript and React, bundled with Vite. You can use the following commands to manage the development lifecycle:

  • npm install: install the dependencies
  • npm run dev: start the Vite dev server with hot reload. It proxies the admin API to the backend, so run make start alongside it (override the target with SMOCKER_DEV_PROXY if the backend is elsewhere)
  • npm run build: generate the transpiled and minified files and assets
  • npm run lint: run static analysis on the code
  • npm run format: automatically format the frontend code
  • npm test: execute unit tests
  • npm run test:watch: execute unit tests, with live reload

Docker

The application can be packaged as a standalone Docker image. You can use the following commands to manage the development lifecycle:

  • make build-docker, make VERSION=xxx build-docker: build the application as a Docker image
  • make start-docker, make VERSION=xxx start-docker: run a Smocker Docker image

Caddy

If you need to test Smocker with a base path, you can use the Caddyfile provided in the repository (Caddy v2):

  • make start-release, make VERSION=xxx start-release: create a released version of Smocker and launch it with /smocker/ as base path
  • make start-caddy: start Caddy to make Smocker accessible at http://localhost:8082/smocker/

HTTPS

If you need to test Smocker with HTTPS enabled, the easiest way is to generate a locally signed certificate with mkcert:

# Install the local certificate authority
mkcert -install

# Create a certificate for localhost
mkcert -cert-file /tmp/cert.pem -key-file /tmp/key.pem localhost

Then, start Smocker with TLS enabled, using your generated certificate:

./smocker -mock-server-listen-port=44300 -config-listen-port=44301 -tls-enable -tls-cert-file=/tmp/cert.pem -tls-private-key-file=/tmp/key.pem

Authors

Contributors

相关仓库
karatelabs/karate

Test Automation Made Simple

JavaMavenMIT Licensetest-automationassertions
docs.karatelabs.io
8.9k2k
mockoon/mockoon

Mockoon is the easiest and quickest way to run mock APIs locally. No remote deployment, no account required, open source.

TypeScriptnpmMIT Licensemockserver
mockoon.com
8.3k461
wiremock/wiremock

A tool for mocking HTTP services

JavaMavenApache License 2.0wiremockjson
wiremock.org
7.3k1.5k
AliSoftware/OHHTTPStubs

Stub your network requests easily! Test your apps with fake network data and custom response time, response code and headers!

Objective-CMIT Licenseiosstubbing
5.1k596
stoplightio/prism

Turn any OpenAPI2/3 and Postman Collection file into an API server with mocking, transformations and validations.

TypeScriptnpmApache License 2.0oasprism
stoplight.io/open-source/prism
5k413
mock-server/mockserver-monorepo

MockServer is an HTTP(S) mock server and proxy for testing that lets you mock APIs, inspect and modify live traffic, and inject failures. It supports HTTP/1.1, HTTP/2, gRPC, WebSockets, TCP and more on a single port, with additional support for HTTP/3, message brokers, and AI/LLM APIs.

JavaMavenApache License 2.0mock-serverproxy
mock-server.com
4.9k1.1k
mock-server/mockserver

MockServer enables easy mocking of any system you integrate with via HTTP or HTTPS with clients written in Java, JavaScript and Ruby. MockServer also includes a proxy that introspects all proxied traffic including encrypted SSL traffic and supports Port Forwarding, Web Proxying (i.e. HTTP proxy), HTTPS Tunneling Proxying (using HTTP CONNECT) and SOCKS Proxying (i.e. dynamic port forwarding).

JavaMavenApache License 2.0mock-serverproxy
mock-server.com
4.7k1.1k
fabric8io/kubernetes-client

Java client for Kubernetes & OpenShift

JavaMavenApache License 2.0kubernetesopenshift
fabric8.io
3.7k1.5k
didi/sharingan

Sharingan(写轮眼)是一个基于golang的流量录制回放工具,适合项目重构、回归测试等。

GoGo ModulesApache License 2.0golangmock-server
2.4k352
microcks/microcks

The open source, cloud native tool for API Mocking and Testing. Microcks is a Cloud Native Computing Foundation incubating project 🚀

JavaMavenApache License 2.0apitesting
microcks.io
2k349
wiremock/WireMock.Net

WireMock.Net is a flexible product for stubbing and mocking web HTTP responses using advanced request matching and response templating. Based on WireMock Java, but extended and different functionality. Full documentation can be found at https://wiremock.org/dotnet/.

C#Apache License 2.0wiremockmock-server
1.7k240
WireMock-Net/WireMock.Net

WireMock.Net is a flexible product for stubbing and mocking web HTTP responses using advanced request matching and response templating. Based on the functionality from http://WireMock.org, but extended with more functionality.

C#Apache License 2.0wiremockmock-server
1.5k213