https://github.com/moclojer/moclojer
Simple and efficient HTTP and Webscoket mock server with specification in yaml, edn or OpenAPI
https://github.com/moclojer/moclojer
api api-mock api-mocker api-rest clojure edn hacktoberfest mock mock-server openapi openapi3 swagger swagger-api swagger2 yaml
Last synced: 2 months ago
JSON representation
Simple and efficient HTTP and Webscoket mock server with specification in yaml, edn or OpenAPI
- Host: GitHub
- URL: https://github.com/moclojer/moclojer
- Owner: moclojer
- License: mit
- Created: 2022-02-23T10:49:57.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2025-10-13T15:20:51.000Z (3 months ago)
- Last Synced: 2025-10-22T01:39:26.040Z (2 months ago)
- Topics: api, api-mock, api-mocker, api-rest, clojure, edn, hacktoberfest, mock, mock-server, openapi, openapi3, swagger, swagger-api, swagger2, yaml
- Language: Clojure
- Homepage: https://docs.moclojer.com
- Size: 771 KB
- Stars: 127
- Watchers: 2
- Forks: 13
- Open Issues: 13
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
- Citation: CITATION.cff
- Cla: CLA.md
Awesome Lists containing this project
README

Simple and efficient HTTP mock server with specification written in `yaml`, `edn` or `OpenAPI`.
> 💾 Download the `.jar` file with the latest version of moclojer to test on your computer [here](https://github.com/moclojer/moclojer/releases/latest).
[**📖 See the complete documentation for moclojer here**](https://docs.moclojer.com/), if you want to contribute (or complement) the documentation, it is [here](https://github.com/moclojer/moclojer/tree/main/docs).
**`YAML` example**
```yaml
# This mock register route: GET /hello/:username
- endpoint:
# Note: the method could be omitted because GET is the default
method: GET
path: /hello/:username
response:
# Note: the status could be omitted because 200 is the default
status: 200
headers:
Content-Type: application/json
# Note: the body will receive the value passed in the url using the
# :username placeholder
body: >
{
"hello": "{{path-params.username}}!"
}
```
**WebSocket Support**
Moclojer also supports WebSocket connections with a simple configuration approach:
```yaml
# WebSocket echo server
- websocket:
path: /ws/echo
on-connect:
# Message sent when client connects
response: '{"status": "connected", "message": "Welcome to WebSocket Echo!"}'
on-message:
# Simple echo for "ping" message
- pattern: "ping"
response: "pong"
# Echo back any JSON content with an "echo" field
- pattern: '{"echo": "{{json-params.echo}}"}'
response: '{"echoed": "{{json-params.echo}}"}'
```
You can test the WebSocket connection using tools like `websocat`:
```sh
websocat "ws://localhost:8000/ws/echo" --text
```
## docker
* **image:** `ghcr.io/moclojer/moclojer:latest`
* **port _(default)_:** `8000`_, if you want to change the port set the environment variable `PORT`_
```sh
docker run -it \
-p 8000:8000 -v $(pwd)/moclojer.yml:/app/moclojer.yml \
ghcr.io/moclojer/moclojer:latest
```
**We have two versions available:**
* `dev`: main branch docker image
* `latest`: latest stable version image
## manual installation
We distribute via the `.jar` file, you need to have Java installed on your operating system.
```sh
bash < <(curl -s https://raw.githubusercontent.com/moclojer/moclojer/main/install.sh)
```
> If you are using Linux you maybe need `sudo`.
## CLI Usage
* `clj -M:run [OPTIONS]`
* `java -jar moclojer.jar [OPTIONS]`
* `moclojer_Linux [OPTIONS]`
### Options
parameter | description
--- | ---
`-c, --config` | Config path or the CONFIG environment variable. \[**default:** `~/.config/moclojer.yml`\]
`-m, --mocks` | OpenAPI v3 mocks path or the MOCKS environment variable.
`-f, --format` | Output and logging format. Either `println` or `json`.
`-h, --help` | Show help information
`-v, --version` | Show version information
**sentry:** set environment var `SENTRY_DSN` ([sentry doc](https://docs.sentry.io/platforms/node/guides/azure-functions/configuration/options/#dsn)), automatic send backtrace to
> **Config** uses `XDG_CONFIG_HOME` to fetch the default moclojer configuration file, if you want to set a different directory you must use the `-c` or environment variable `CONFIG`
## 💻 dev environment
moclojer is written in **Clojure**, to run the commands below we assume you have clojure _installed_ on your operating system.
**run:**
```sh
clj -M:run
```
**test:**
```sh
clj -M:test
```
> _if you want to run a specific test:_ `clj -M:test -n com.moclojer.external-body.excel-test`
**`moclojer.jar` generate:**
```sh
clj -A:dev -M --report stderr -m com.moclojer.build
```
## framework integrations
We distribute the library via [Clojars](https://clojars.org/com.moclojer/moclojer).
### Clojure CLI/deps.edn
```clojure
com.moclojer/moclojer {:mvn/version "0.3.1"}
```
### Leiningen/Boot
```clojure
[com.moclojer/moclojer "0.3.1"]
```
### [`git`](https://clojure.org/guides/deps_and_cli#_using_git_libraries) in `deps.edn`
```edn
{:deps
{com.moclojer/moclojer {:git/url "https://github.com/moclojer/moclojer.git"
:git/tag "v0.3.1"
:git/sha "c4ca0f2cfcfbe47de6eb0c601b26106190e20793"}}}
```
### example of use
```clj
(ns my-app.core
(:require [com.moclojer.adapters :as adapters]
[com.moclojer.server :as server]))
(def *router
"create a router from a config map"
(adapters/generate-routes
[{:endpoint
{:method "GET"
:path "/example"
:response {:status 200
:headers {:Content-Type "application/json"}
:body {:id 123}}}}]))
(defn -main
"start the server"
[& args]
(server/start-server! *router))
```