Package ghidra.test

Class MockHttpServerUtils

java.lang.Object
ghidra.test.MockHttpServerUtils

public class MockHttpServerUtils extends Object
  • Field Details

  • Constructor Details

    • MockHttpServerUtils

      public MockHttpServerUtils()
  • Method Details

    • getURI

      public static URI getURI(InetSocketAddress addr)
      Convert a mock http server's address to a URL
      Parameters:
      addr - InetSocketAddress
      Returns:
      http connection URI, example "http://127.0.0.1:9999"
    • nextLoopbackServerAddr

      public static InetSocketAddress nextLoopbackServerAddr()
      Returns the next hopefully unused localhost socket addr.
      Returns:
      the next hopefully unused localhost socket addr
    • createMockHttpServer

      public static HttpServer createMockHttpServer() throws IOException
      Creates an HttpServer, listening on localhost and a unique unused port number.

      Use HttpServer.createContext(String, HttpHandler) to add handlers for specific paths.

      Returns:
      new HttpServer
      Throws:
      IOException - if unused port is not found
    • assertContentType

      public static void assertContentType(String expectedType, HttpExchange httpExchange)
      Asserts that the specified HttpExchange has a specific content type header.
      Parameters:
      expectedType - example: "application/json"
      httpExchange - HttpExchange
    • wrapHandlerWithDelay

      public static HttpHandler wrapHandlerWithDelay(HttpHandler delegate, int delayMS)
      Adds a delay to a handler.
      Parameters:
      delegate - HttpHandler to wrap
      delayMS - milliseconds to delay before allowing the delegate to process the request
      Returns:
      new HttpHandler that wraps the specified delegate
    • wrapHandlerWithRetryError

      public static HttpHandler wrapHandlerWithRetryError(HttpHandler delegate, int errorCount, int errorStatus)
    • mock404Handler

      public static void mock404Handler(HttpExchange httpExchange) throws IOException
      A handler that always returns a 404. Use this as the target of a lambda. This matches the HttpHandler.handle(HttpExchange) method signature.
      Parameters:
      httpExchange - HttpExchange
      Throws:
      IOException - if error
    • createStaticResponseHandler

      public static HttpHandler createStaticResponseHandler(String contentType, byte[] resultBody)
      Creates a HttpHandler that returns a specified body
      Parameters:
      contentType - http content type header value (eg. "text/plain")
      resultBody - bytes to send as body
      Returns:
      new HttpHandler
    • createStaticResponseHandler

      public static HttpHandler createStaticResponseHandler(int resultCode, String contentType, byte[] resultBody)
      Creates a HttpHandler that returns a specified body and result code.
      Parameters:
      resultCode - http result code to return (eg. HTTP_OK / 200 )
      contentType - http content type header value (eg. "text/plain")
      resultBody - bytes to send as body
      Returns:
      new HttpHandler
    • logMockHttp

      public static void logMockHttp(HttpExchange httpExchange, String msg)
      Logs (using Msg.info) a message using information from the http connection as a prefix
      Parameters:
      httpExchange - HttpExchange
      msg - string message