Tag related to Mock Server

Quick Mock Server

Are you in a need of a quick mock server? One simple solution for Linux/Mac/BSD users is to employ an existing tool, nc. It exists in most distros and it’s very easy to use.

#!/bin/bash

if [ "$#" -ne "1" ]; then
    echo "usage: ./mock_server.sh <port>"
    exit 1
fi

trap "{ exit 0; }" SIGINT SIGTERM SIGKILL

PORT="$1"

function make_response() {
	read -r response
	echo -e "HTTP/1.1 200 OK\r\n\r\n${response}"
}

while true ; do
	dd if=/dev/zero count=10000
	make_response | nc -l "$PORT"
done

Save this in a file called mock_server.sh and run it like this: ./mock_server.sh 8000, or on any port you like. After running this script you can call the server using curl specifying any path you like.