MCPcopy Create free account
hub / github.com/crate/crate / HttpTestServer

Class HttpTestServer

server/src/test/java/io/crate/http/HttpTestServer.java:71–216  ·  view source on GitHub ↗

Http server with configurable request handler.

Source from the content-addressed store, hash-verified

69 * Http server with configurable request handler.
70 */
71public class HttpTestServer {
72
73 private final int port;
74 private final boolean fail;
75
76 private final BiConsumer<HttpRequest, JsonGenerator> requestHandler;
77 private static final JsonFactory JSON_FACTORY;
78
79 private Channel channel;
80 private MultiThreadIoEventLoopGroup group;
81
82 public List<String> responses = new ArrayList<>();
83
84 static {
85 JSON_FACTORY = new JsonFactory();
86 JSON_FACTORY.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
87 JSON_FACTORY.configure(JsonWriteFeature.QUOTE_FIELD_NAMES.mappedFeature(), true);
88 JSON_FACTORY.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
89 }
90
91 @Nullable
92 private final Map<String, String> headers;
93
94
95
96 /**
97 * @param port the port to listen on
98 * @param fail of set to true, the server will emit error responses
99 * @param requestHandler is function to transform incoming messages to some custom output, written to a pre-configured json generator.
100 * @param headers Http headers which are attached to the response
101 * It MUST propagate any exception to the server so that server can set status to 500.
102 */
103 public HttpTestServer(int port, boolean fail, BiConsumer<HttpRequest, JsonGenerator> requestHandler, @Nullable Map<String, String> headers) {
104 this.port = port;
105 this.fail = fail;
106 this.requestHandler = requestHandler;
107 this.headers = headers;
108 }
109
110 /**
111 * @param port the port to listen on
112 * @param fail of set to true, the server will emit error responses
113 * @param requestHandler is function to transform incoming messages to some custom output, written to a pre-configured json generator.
114 * It MUST propagate any exception to the server so that server can set status to 500.
115 */
116 public HttpTestServer(int port, boolean fail, BiConsumer<HttpRequest, JsonGenerator> requestHandler) {
117 this.port = port;
118 this.fail = fail;
119 this.requestHandler = requestHandler;
120 this.headers = null;
121 }
122
123 public void run() throws InterruptedException {
124 // Configure the server.
125 ServerBootstrap bootstrap = new ServerBootstrap();
126 group = new MultiThreadIoEventLoopGroup(NioIoHandler.newFactory());
127 bootstrap.group(group);
128 bootstrap.channel(NioServerSocketChannel.class);

Callers

nothing calls this directly

Calls 1

configureMethod · 0.65

Tested by

no test coverage detected