Skip to content

Commit 0aa7fb2

Browse files
author
chris-hoefgen
committed
simple server for unit tests to validate client
1 parent f1be0a7 commit 0aa7fb2

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.devcycle.sdk.server.helpers;
2+
3+
import com.sun.net.httpserver.HttpExchange;
4+
import com.sun.net.httpserver.HttpServer;
5+
6+
import java.io.*;
7+
import java.net.InetSocketAddress;
8+
9+
public class LocalConfigServer {
10+
private HttpServer server;
11+
private String configData = "";
12+
public LocalConfigServer(String configData) throws IOException{
13+
this.configData = configData;
14+
InetSocketAddress address = new InetSocketAddress(8000);
15+
server = HttpServer.create(address, 0);
16+
server.createContext("/", this::handleConfigRequest);
17+
server.setExecutor(null); // use the default executor
18+
System.out.println("Starting config server on " + address);
19+
}
20+
21+
public void handleConfigRequest(HttpExchange exchange) throws IOException {
22+
exchange.sendResponseHeaders(200, configData.length());
23+
OutputStream outputStream = exchange.getResponseBody();
24+
outputStream.write(configData.getBytes());
25+
outputStream.flush();
26+
outputStream.close();
27+
}
28+
29+
public void setConfigData(String configData) {
30+
this.configData = configData;
31+
}
32+
33+
public void start() {
34+
this.server.start();
35+
}
36+
37+
public void stop() {
38+
this.server.stop(0);
39+
}
40+
}

0 commit comments

Comments
 (0)