Class ServerExtension

java.lang.Object
com.linecorp.armeria.testing.junit5.common.AbstractAllOrEachExtension
com.linecorp.armeria.testing.junit5.server.ServerExtension
All Implemented Interfaces:
AfterAllCallback, AfterEachCallback, BeforeAllCallback, BeforeEachCallback, Extension
Direct Known Subclasses:
MockWebServerExtension

public abstract class ServerExtension extends AbstractAllOrEachExtension
An Extension that allows easy set-up and tear-down of a Server.
  • Constructor Details

    • ServerExtension

      protected ServerExtension()
      Creates a new instance with auto-start enabled.
    • ServerExtension

      protected ServerExtension(boolean autoStart)
      Creates a new instance.
      Parameters:
      autoStart - true if the Server should start automatically. false if the Server should start when a user calls start().
  • Method Details

    • before

      public void before(ExtensionContext context) throws Exception
      Calls start() if auto-start is enabled.
      Specified by:
      before in class AbstractAllOrEachExtension
      Throws:
      Exception
    • beforeEach

      public void beforeEach(ExtensionContext context) throws Exception
      Specified by:
      beforeEach in interface BeforeEachCallback
      Overrides:
      beforeEach in class AbstractAllOrEachExtension
      Throws:
      Exception
    • after

      public void after(ExtensionContext context) throws Exception
      Calls stop(), without waiting until the Server is stopped completely.
      Specified by:
      after in class AbstractAllOrEachExtension
      Throws:
      Exception
    • start

      public com.linecorp.armeria.server.Server start()
      Starts the Server configured by configure(ServerBuilder). If the Server has been started up already, the existing Server is returned. Note that this operation blocks until the Server finished the start-up.
      Returns:
      the started Server
    • configure

      protected abstract void configure(com.linecorp.armeria.server.ServerBuilder sb) throws Exception
      Configures the Server with the given ServerBuilder.
      Throws:
      Exception
    • configureWebClient

      protected void configureWebClient(com.linecorp.armeria.client.WebClientBuilder webClientBuilder) throws Exception
      Configures the WebClient with the given WebClientBuilder. You can get the configured WebClient using webClient().
      Throws:
      Exception
    • stop

      public CompletableFuture<Void> stop()
      Stops the Server asynchronously.
      Returns:
      the CompletableFuture that will complete when the Server is stopped.
    • server

      public com.linecorp.armeria.server.Server server()
      Returns the started Server.
      Throws:
      IllegalStateException - if the Server is not started
    • httpPort

      public int httpPort()
      Returns the HTTP port number of the Server.
      Throws:
      IllegalStateException - if the Server is not started or it did not open an HTTP port
    • httpsPort

      public int httpsPort()
      Returns the HTTPS port number of the Server.
      Throws:
      IllegalStateException - if the Server is not started or it did not open an HTTPS port
    • port

      public int port(com.linecorp.armeria.common.SessionProtocol protocol)
      Returns the port number of the Server for the specified SessionProtocol.
      Throws:
      IllegalStateException - if the Server is not started or it did not open a port of the specified protocol.
    • hasHttp

      public boolean hasHttp()
      Returns true if the Server is started and it has an HTTP port open.
    • hasHttps

      public boolean hasHttps()
      Returns true if the Server is started and it has an HTTPS port open.
    • endpoint

      public com.linecorp.armeria.client.Endpoint endpoint(com.linecorp.armeria.common.SessionProtocol protocol)
      Returns the Endpoint of the specified SessionProtocol for the Server.
      Throws:
      IllegalStateException - if the Server is not started or it did not open the port for the specified SessionProtocol.
    • httpEndpoint

      public com.linecorp.armeria.client.Endpoint httpEndpoint()
      Returns the HTTP Endpoint for the Server.
      Throws:
      IllegalStateException - if the Server is not started or it did not open an HTTP port.
    • httpsEndpoint

      public com.linecorp.armeria.client.Endpoint httpsEndpoint()
      Returns the HTTPS Endpoint for the Server.
      Throws:
      IllegalStateException - if the Server is not started or it did not open an HTTPS port.
    • uri

      public URI uri(com.linecorp.armeria.common.SessionProtocol protocol)
      Returns the URI for the Server of the specified SessionProtocol.
      Returns:
      the absolute URI without a path.
      Throws:
      IllegalStateException - if the Server is not started or it did not open the port for the specified SessionProtocol.
    • uri

      public URI uri(com.linecorp.armeria.common.SessionProtocol protocol, com.linecorp.armeria.common.SerializationFormat format)
      Returns the URI for the Server of the specified SessionProtocol and SerializationFormat.
      Throws:
      IllegalStateException - if the Server is not started or it did not open the port for the specified SessionProtocol.
    • httpUri

      public URI httpUri()
      Returns the HTTP URI for the Server.
      Returns:
      the absolute URI without a path.
      Throws:
      IllegalStateException - if the Server is not started or it did not open an HTTP port.
    • httpUri

      public URI httpUri(com.linecorp.armeria.common.SerializationFormat format)
      Returns the HTTP URI for the Server.
      Returns:
      the absolute URI without a path.
      Throws:
      IllegalStateException - if the Server is not started or it did not open an HTTP port.
    • httpsUri

      public URI httpsUri()
      Returns the HTTPS URI for the Server.
      Returns:
      the absolute URI without a path.
      Throws:
      IllegalStateException - if the Server is not started or it did not open an HTTPS port.
    • httpsUri

      public URI httpsUri(com.linecorp.armeria.common.SerializationFormat format)
      Returns the HTTPS URI for the Server.
      Returns:
      the absolute URI without a path.
      Throws:
      IllegalStateException - if the Server is not started or it did not open an HTTPS port.
    • socketAddress

      public InetSocketAddress socketAddress(com.linecorp.armeria.common.SessionProtocol protocol)
      Returns the InetSocketAddress of the specified SessionProtocol for the Server.
      Throws:
      IllegalStateException - if the Server is not started or it did not open a port for the specified SessionProtocol.
    • httpSocketAddress

      public InetSocketAddress httpSocketAddress()
      Returns the HTTP InetSocketAddress of the Server.
      Throws:
      IllegalStateException - if the Server is not started or it did not open an HTTP port
    • httpsSocketAddress

      public InetSocketAddress httpsSocketAddress()
      Returns the HTTPS InetSocketAddress of the Server.
      Throws:
      IllegalStateException - if the Server is not started or it did not open an HTTPS port
    • requestContextCaptor

      public final ServiceRequestContextCaptor requestContextCaptor()
      Returns the ServiceRequestContextCaptor that captures all the ServiceRequestContexts in this Server.
    • webClient

      public com.linecorp.armeria.client.WebClient webClient()
      Returns the WebClient configured by configureWebClient(WebClientBuilder).
    • webClient

      public com.linecorp.armeria.client.WebClient webClient(Consumer<com.linecorp.armeria.client.WebClientBuilder> webClientCustomizer)
      Returns a newly created WebClient configured by configureWebClient(WebClientBuilder) and then the specified customizer.
    • blockingWebClient

      @UnstableApi public com.linecorp.armeria.client.BlockingWebClient blockingWebClient()
      Returns the BlockingWebClient configured by configureWebClient(WebClientBuilder).
    • blockingWebClient

      @UnstableApi public com.linecorp.armeria.client.BlockingWebClient blockingWebClient(Consumer<com.linecorp.armeria.client.WebClientBuilder> webClientCustomizer)
      Returns a newly created BlockingWebClient configured by configureWebClient(WebClientBuilder) and then the specified customizer.
    • restClient

      @UnstableApi public com.linecorp.armeria.client.RestClient restClient()
      Returns the RestClient configured by configureWebClient(WebClientBuilder).
    • restClient

      @UnstableApi public com.linecorp.armeria.client.RestClient restClient(Consumer<com.linecorp.armeria.client.WebClientBuilder> webClientCustomizer)
      Returns a newly created RestClient configured by configureWebClient(WebClientBuilder) and then the specified customizer.
    • shouldCapture

      @UnstableApi protected boolean shouldCapture(com.linecorp.armeria.server.ServiceRequestContext ctx)
      Determines whether the ServiceRequestContext should be captured or not. This method returns true by default. Override it to capture the contexts selectively.