Last active
January 23, 2022 16:30
-
-
Save rponte/710d65dc3beb28d97655 to your computer and use it in GitHub Desktop.
Simple jUnit Rule to create a HttpServer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import static org.junit.Assert.assertEquals; | |
import org.junit.Rule; | |
import org.junit.Test; | |
import base.junitrules.HttpServerRule; | |
import base.junitrules.SuccessHandler; | |
public class _HowToUseTest { | |
@Rule | |
public HttpServerRule httpServer = new HttpServerRule(); | |
@Test | |
public void shouldRespondASimpleHttpGet() { | |
String uri = "/triadworks/instructors/2020/description"; | |
httpServer.registerHandler(uri, new SuccessHandler("@rponte is cute body!")); | |
HttpClient httpClient = new HttpClient(); | |
String response = httpClient.doGet(completeUri(uri)); | |
assertEquals("body content", "@rponte is cute body!", body); | |
} | |
public String completeUri(String uriPath) { | |
return httpServer.getUriFor(uriPath); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package base.junitrules; | |
import java.io.File; | |
import java.io.IOException; | |
import org.apache.commons.io.FileUtils; | |
import java.net.HttpURLConnection; | |
import com.sun.net.httpserver.HttpExchange; | |
import com.sun.net.httpserver.HttpHandler; | |
public class DownloadFileHttpHandler implements HttpHandler { | |
private final File file; | |
private final String contentType = "application/zip"; | |
public DownloadFileHttpHandler(File fileToDownload) { | |
this.file = fileToDownload; | |
} | |
@Override | |
public void handle(HttpExchange exchange) throws IOException { | |
exchange.getResponseHeaders().add("Content-Type", contentType); | |
exchange.sendResponseHeaders(HttpURLConnection.HTTP_OK, file.length()); | |
FileUtils.copyFile(this.file, exchange.getResponseBody()); | |
exchange.close(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package base.junitrules; | |
import java.net.InetSocketAddress; | |
import org.junit.rules.ExternalResource; | |
import com.sun.net.httpserver.HttpHandler; | |
import com.sun.net.httpserver.HttpServer; | |
/** | |
* Simulates a HTTP Server | |
*/ | |
public class HttpServerRule extends ExternalResource { | |
private static final int PORT = 6991; | |
private HttpServer server; | |
@Override | |
protected void before() throws Throwable { | |
server = HttpServer.create(new InetSocketAddress(PORT), 0); // localhost:6991 | |
server.setExecutor(null); // creates a default executor | |
server.start(); | |
} | |
@Override | |
protected void after() { | |
if (server != null) { | |
server.stop(0); // doesn't wait all current exchange handlers complete | |
} | |
} | |
public String getUriFor(String path) { | |
if (!path.startsWith("/")) { | |
path = "/" + path; | |
} | |
String host = "http://localhost:" + PORT; | |
return host + path; | |
} | |
public void registerHandler(String uriToHandle, HttpHandler httpHandler) { | |
server.createContext(uriToHandle, httpHandler); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package base.http.handler; | |
import java.io.IOException; | |
import org.apache.commons.io.IOUtils; | |
import java.net.HttpURLConnection; | |
import com.sun.net.httpserver.HttpExchange; | |
import com.sun.net.httpserver.HttpHandler; | |
public class InternalServerErrorHandler implements HttpHandler { | |
private String responseBody = ""; | |
private String contentType = "text/plain"; | |
public InternalServerErrorHandler() {} | |
public InternalServerErrorHandler(String responseBody) { | |
this.responseBody = responseBody; | |
} | |
@Override | |
public void handle(HttpExchange exchange) throws IOException { | |
exchange.getResponseHeaders().add("Content-Type", contentType); | |
exchange.sendResponseHeaders(HttpURLConnection.HTTP_INTERNAL_ERROR, responseBody.length()); | |
IOUtils.write(responseBody, exchange.getResponseBody()); | |
exchange.close(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package base.http.handler; | |
import java.io.IOException; | |
import org.apache.commons.io.IOUtils; | |
import java.net.HttpURLConnection; | |
import com.sun.net.httpserver.HttpExchange; | |
import com.sun.net.httpserver.HttpHandler; | |
public class SuccessHandler implements HttpHandler { | |
private String responseBody = ""; | |
private String contentType = "text/plain"; | |
public SuccessHandler() {} | |
public SuccessHandler(String responseBody) { | |
this.responseBody = responseBody; | |
} | |
@Override | |
public void handle(HttpExchange exchange) throws IOException { | |
exchange.getResponseHeaders().add("Content-Type", contentType); | |
exchange.sendResponseHeaders(HttpURLConnection.HTTP_OK, responseBody.length()); | |
IOUtils.write(responseBody, exchange.getResponseBody()); | |
exchange.close(); | |
} | |
} |
This is awesome!! thank you!
I have converted it to a junit5 extension here: https://gist.github.com/ahmed-musallam/f6238afb3895a23d03b5a09899392108
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice!