Package ghidra.test
Class MockHttpServerUtils
java.lang.Object
ghidra.test.MockHttpServerUtils
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidassertContentType(String expectedType, HttpExchange httpExchange) Asserts that the specifiedHttpExchangehas a specific content type header.static HttpServerCreates an HttpServer, listening on localhost and a unique unused port number.static HttpHandlercreateStaticResponseHandler(int resultCode, String contentType, byte[] resultBody) Creates a HttpHandler that returns a specified body and result code.static HttpHandlercreateStaticResponseHandler(String contentType, byte[] resultBody) Creates a HttpHandler that returns a specified bodystatic URIgetURI(InetSocketAddress addr) Convert a mock http server's address to a URLstatic voidlogMockHttp(HttpExchange httpExchange, String msg) Logs (using Msg.info) a message using information from the http connection as a prefixstatic voidmock404Handler(HttpExchange httpExchange) A handler that always returns a 404.static InetSocketAddressReturns the next hopefully unused localhost socket addr.static HttpHandlerwrapHandlerWithDelay(HttpHandler delegate, int delayMS) Adds a delay to a handler.static HttpHandlerwrapHandlerWithRetryError(HttpHandler delegate, int errorCount, int errorStatus)
-
Field Details
-
CONTENT_TYPE_HEADER
- See Also:
-
-
Constructor Details
-
MockHttpServerUtils
public MockHttpServerUtils()
-
-
Method Details
-
getURI
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
Returns the next hopefully unused localhost socket addr.- Returns:
- the next hopefully unused localhost socket addr
-
createMockHttpServer
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
Asserts that the specifiedHttpExchangehas a specific content type header.- Parameters:
expectedType- example: "application/json"httpExchange-HttpExchange
-
wrapHandlerWithDelay
Adds a delay to a handler.- Parameters:
delegate-HttpHandlerto wrapdelayMS- 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
A handler that always returns a 404. Use this as the target of a lambda. This matches theHttpHandler.handle(HttpExchange)method signature.- Parameters:
httpExchange-HttpExchange- Throws:
IOException- if error
-
createStaticResponseHandler
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
Logs (using Msg.info) a message using information from the http connection as a prefix- Parameters:
httpExchange-HttpExchangemsg- string message
-