Skip to content

Instantly share code, notes, and snippets.

@pditommaso
Last active May 2, 2019 10:14
Show Gist options
  • Save pditommaso/847cac01446dc3bc57d7fe5d7a0227d1 to your computer and use it in GitHub Desktop.
Save pditommaso/847cac01446dc3bc57d7fe5d7a0227d1 to your computer and use it in GitHub Desktop.
Minimal web server dumping request body to stdout
import com.sun.net.httpserver.HttpExchange
import com.sun.net.httpserver.HttpHandler
import com.sun.net.httpserver.HttpServer
import groovy.json.JsonOutput
class TowerMockServer implements HttpHandler {
private int count
@Override
void handle(HttpExchange exchange) throws IOException {
final text = exchange.requestBody.text
final json = JsonOutput.prettyPrint(text)
println "== request ${++count} === \n${json}\n"
String response = '{"status":"OK", "workflowId": "12345"}'
exchange.sendResponseHeaders(200, response.getBytes().length)
OutputStream os = exchange.getResponseBody();
os.write(response.getBytes());
os.close();
}
static void main(String...args) {
def server = HttpServer.create(new InetSocketAddress(8500), 0);
def context = server.createContext("/");
context.setHandler( new TowerMockServer() );
server.start();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment