|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2025 Kichwa Coders Canada, Inc. |
| 3 | + * |
| 4 | + * This program and the accompanying materials |
| 5 | + * are made available under the terms of the Eclipse Public License 2.0 |
| 6 | + * which accompanies this distribution, and is available at |
| 7 | + * https://www.eclipse.org/legal/epl-2.0/ |
| 8 | + * |
| 9 | + * SPDX-License-Identifier: EPL-2.0 |
| 10 | + *******************************************************************************/ |
| 11 | +package org.eclipse.swt.tests.junit; |
| 12 | + |
| 13 | +import java.io.BufferedReader; |
| 14 | +import java.io.Closeable; |
| 15 | +import java.io.IOException; |
| 16 | +import java.io.InputStreamReader; |
| 17 | +import java.io.OutputStream; |
| 18 | +import java.net.InetAddress; |
| 19 | +import java.net.InetSocketAddress; |
| 20 | +import java.net.URLDecoder; |
| 21 | +import java.net.URLEncoder; |
| 22 | +import java.nio.charset.StandardCharsets; |
| 23 | + |
| 24 | +import com.sun.net.httpserver.HttpExchange; |
| 25 | +import com.sun.net.httpserver.HttpServer; |
| 26 | + |
| 27 | +public class EchoHttpServer implements Closeable { |
| 28 | + |
| 29 | + private HttpServer server; |
| 30 | + |
| 31 | + public EchoHttpServer() throws IOException { |
| 32 | + InetSocketAddress addr = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0); |
| 33 | + server = HttpServer.create(addr, 0); |
| 34 | + createHandlers(); |
| 35 | + server.start(); |
| 36 | + } |
| 37 | + |
| 38 | + protected void createHandlers() { |
| 39 | + server.createContext("/get/echo", this::handleGetEcho); |
| 40 | + server.createContext("/post/echo", this::handlePostEcho); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public void close() { |
| 45 | + server.stop(0); |
| 46 | + } |
| 47 | + |
| 48 | + public int port() { |
| 49 | + return server.getAddress().getPort(); |
| 50 | + } |
| 51 | + |
| 52 | + public String getEchoUrl(String msg) { |
| 53 | + return "http://localhost:" + port() + "/get/echo?msg=" + URLEncoder.encode(msg, StandardCharsets.UTF_8); |
| 54 | + } |
| 55 | + |
| 56 | + public String postEchoUrl() { |
| 57 | + return "http://localhost:" + port() + "/post/echo"; |
| 58 | + } |
| 59 | + |
| 60 | + protected void handleGetEcho(HttpExchange exchange) throws IOException { |
| 61 | + if (!"GET".equalsIgnoreCase(exchange.getRequestMethod())) { |
| 62 | + exchange.sendResponseHeaders(405, -1); |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + String msg = extractMessageFromQuery(exchange.getRequestURI().getQuery()); |
| 67 | + respond(exchange, msg); |
| 68 | + } |
| 69 | + |
| 70 | + protected void handlePostEcho(HttpExchange exchange) throws IOException { |
| 71 | + if (!"POST".equalsIgnoreCase(exchange.getRequestMethod())) { |
| 72 | + exchange.sendResponseHeaders(405, -1); |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + byte[] bodyBytes = exchange.getRequestBody().readAllBytes(); |
| 77 | + String body = new String(bodyBytes, StandardCharsets.UTF_8); |
| 78 | + |
| 79 | + respond(exchange, body); |
| 80 | + } |
| 81 | + |
| 82 | + private String extractMessageFromQuery(String query) { |
| 83 | + if (query == null) |
| 84 | + return ""; |
| 85 | + // XXX: This is not real/complete query decoding, add more |
| 86 | + // as needed if tests require it |
| 87 | + for (String part : query.split("&")) { |
| 88 | + if (part.startsWith("msg=")) { |
| 89 | + return URLDecoder.decode(part.substring(4), StandardCharsets.UTF_8); |
| 90 | + } |
| 91 | + } |
| 92 | + return ""; |
| 93 | + } |
| 94 | + |
| 95 | + private void respond(HttpExchange exchange, String text) throws IOException { |
| 96 | + var html = expectedResponse(text); |
| 97 | + byte[] bytes = html.getBytes(StandardCharsets.UTF_8); |
| 98 | + |
| 99 | + exchange.sendResponseHeaders(200, bytes.length); |
| 100 | + try (OutputStream os = exchange.getResponseBody()) { |
| 101 | + os.write(bytes); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + public String expectedResponse(String text) { |
| 106 | + var html = String.format(""" |
| 107 | + <!DOCTYPE html> |
| 108 | + <html> |
| 109 | + <head> |
| 110 | + <title>%s</title> |
| 111 | + </head> |
| 112 | + <body> |
| 113 | + <h1>This is the SWT Test Echo Http Server</h1> |
| 114 | + <p>This is the echo message (also in title): %s</p> |
| 115 | + </body> |
| 116 | + </html> |
| 117 | + """, text, text); |
| 118 | + return html; |
| 119 | + } |
| 120 | + |
| 121 | + public static void main(String[] args) throws IOException { |
| 122 | + try (var server = new EchoHttpServer()) { |
| 123 | + System.out.println("started on port " + server.port()); |
| 124 | + System.out.println("Try visiting " + server.getEchoUrl("Hello SWT")); |
| 125 | + System.out.println("Will shutdown by pressing newline"); |
| 126 | + new BufferedReader(new InputStreamReader(System.in)).readLine(); |
| 127 | + } |
| 128 | + System.out.println("shutdown"); |
| 129 | + } |
| 130 | + |
| 131 | +} |
0 commit comments