Example usage for io.netty.util NetUtil LOCALHOST

List of usage examples for io.netty.util NetUtil LOCALHOST

Introduction

In this page you can find the example usage for io.netty.util NetUtil LOCALHOST.

Prototype

InetAddress LOCALHOST

To view the source code for io.netty.util NetUtil LOCALHOST.

Click Source Link

Document

The InetAddress that represents the loopback address.

Usage

From source file:com.andrewkroh.cisco.common.TestUtils.java

License:Apache License

/**
 * Return a free port which can be used to bind to
 *
 * @return port/*from   w  w  w. j  av a 2  s. c  o m*/
 */
public static int getFreePort() {
    for (int i = 0; i < NUM_CANDIDATES; i++) {
        int port = nextCandidatePort();
        try {
            // Ensure it is possible to bind on both wildcard and loopback.
            ServerSocket ss;
            ss = new ServerSocket();
            ss.setReuseAddress(false);
            ss.bind(new InetSocketAddress(port));
            ss.close();

            ss = new ServerSocket();
            ss.setReuseAddress(false);
            ss.bind(new InetSocketAddress(NetUtil.LOCALHOST, port));
            ss.close();

            return port;
        } catch (IOException e) {
            // ignore
        }
    }

    throw new RuntimeException("unable to find a free port");
}

From source file:com.andrewkroh.cisco.xmlservices.DefaultXmlPushServiceTest.java

License:Apache License

private IpPhone mockPhoneWithWrongPort() {
    IpPhone phone = mock(IpPhone.class);
    when(phone.getUsername()).thenReturn("admin");
    when(phone.getPassword()).thenReturn("pass");
    when(phone.getHostname()).thenReturn(NetUtil.LOCALHOST.getHostAddress());
    when(phone.getPort()).thenReturn(TestUtils.getFreePort());
    return phone;
}

From source file:com.github.sinsinpub.pero.manual.proxyhandler.ProxyServer.java

License:Apache License

/**
 * Starts a new proxy server with disabled authentication for testing purpose.
 * //from w  ww.  j a  va2  s  .  c o  m
 * @param useSsl {@code true} if and only if implicit SSL is enabled
 * @param testMode the test mode
 * @param username the expected username. If the client tries to authenticate with a different
 *            username, this server will fail the authentication request.
 * @param password the expected password. If the client tries to authenticate with a different
 *            password, this server will fail the authentication request.
 * @param destination the expected destination. If the client requests proxying to a different
 *            destination, this server will reject the connection request.
 */
protected ProxyServer(final boolean useSsl, TestMode testMode, InetSocketAddress destination, String username,
        String password, int bindPort, boolean logging) {

    this.testMode = testMode;
    this.destination = destination;
    this.username = username;
    this.password = password;

    ServerBootstrap b = new ServerBootstrap();
    b.channel(NioServerSocketChannel.class);
    if (logging) {
        b.handler(new LoggingHandler(LogLevel.INFO));
    }
    b.group(StaticContextProvider.group);
    b.childHandler(new ChannelInitializer<SocketChannel>() {
        @Override
        protected void initChannel(SocketChannel ch) throws Exception {
            ChannelPipeline p = ch.pipeline();
            if (useSsl) {
                p.addLast(StaticContextProvider.serverSslCtx.newHandler(ch.alloc()));
            }

            configure(ch);
        }
    });

    ch = (ServerSocketChannel) b.bind(NetUtil.LOCALHOST, bindPort).syncUninterruptibly().channel();
}

From source file:com.github.sinsinpub.pero.manual.proxyhandler.ProxyServer.java

License:Apache License

public final InetSocketAddress address() {
    return new InetSocketAddress(NetUtil.LOCALHOST, ch.localAddress().getPort());
}

From source file:com.linecorp.armeria.client.endpoint.dns.TestDnsServer.java

License:Apache License

TestDnsServer(Map<DnsQuestion, DnsResponse> responses) {
    this.responses = ImmutableMap.copyOf(responses);

    final Bootstrap b = new Bootstrap();
    b.channel(TransportType.datagramChannelType(CommonPools.workerGroup()));
    b.group(CommonPools.workerGroup());//w  ww .  ja v  a  2 s .  c  o m
    b.handler(new ChannelInitializer() {
        @Override
        protected void initChannel(Channel ch) throws Exception {
            final ChannelPipeline p = ch.pipeline();
            p.addLast(new DatagramDnsQueryDecoder());
            p.addLast(new DatagramDnsResponseEncoder());
            p.addLast(new DnsServerHandler());
        }
    });

    channel = b.bind(NetUtil.LOCALHOST, 0).syncUninterruptibly().channel();
}

From source file:com.linecorp.armeria.server.healthcheck.HttpHealthCheckServiceTest.java

License:Apache License

@Test
public void testGet() throws Exception {
    final ServerBuilder builder = new ServerBuilder();
    builder.service("/l7check", new HttpHealthCheckService());
    final Server server = builder.build();
    try {//from  w  w  w  . j av  a 2s  . c  o m
        server.start().join();

        final int port = server.activePort().get().localAddress().getPort();
        try (Socket s = new Socket(NetUtil.LOCALHOST, port)) {
            s.setSoTimeout(10000);
            final InputStream in = s.getInputStream();
            final OutputStream out = s.getOutputStream();
            out.write("GET /l7check HTTP/1.0\r\n\r\n".getBytes(StandardCharsets.US_ASCII));

            // Should not be chunked.
            assertThat(new String(ByteStreams.toByteArray(in))).isEqualTo("HTTP/1.1 200 OK\r\n"
                    + "content-type: text/plain; charset=utf-8\r\n" + "content-length: 2\r\n\r\n" + "ok");
        }
    } finally {
        server.stop();
    }
}

From source file:com.linecorp.armeria.server.healthcheck.HttpHealthCheckServiceTest.java

License:Apache License

@Test
public void testHead() throws Exception {
    final ServerBuilder builder = new ServerBuilder();
    builder.service("/l7check", new HttpHealthCheckService());
    final Server server = builder.build();
    try {/*from w  w w .  ja v a2s . c o m*/
        server.start().join();

        final int port = server.activePort().get().localAddress().getPort();
        try (Socket s = new Socket(NetUtil.LOCALHOST, port)) {
            s.setSoTimeout(10000);
            final InputStream in = s.getInputStream();
            final OutputStream out = s.getOutputStream();
            out.write("HEAD /l7check HTTP/1.0\r\n\r\n".getBytes(StandardCharsets.US_ASCII));

            // Should neither be chunked nor have content.
            assertThat(new String(ByteStreams.toByteArray(in))).isEqualTo("HTTP/1.1 200 OK\r\n"
                    + "content-type: text/plain; charset=utf-8\r\n" + "content-length: 2\r\n\r\n");
        }
    } finally {
        server.stop();
    }
}

From source file:com.linecorp.armeria.server.http.HttpServerPathTest.java

License:Apache License

@Test(timeout = 10000)
public void testDoubleSlashPath() throws Exception {
    try (Socket s = new Socket(NetUtil.LOCALHOST, httpPort())) {
        s.setSoTimeout(10000);//from   w w  w  .  ja  v a2s  .com
        s.getOutputStream().write("GET /service//foo HTTP/1.0\r\n\r\n".getBytes(StandardCharsets.US_ASCII));
        assertThat(new String(ByteStreams.toByteArray(s.getInputStream()), StandardCharsets.US_ASCII))
                .startsWith("HTTP/1.1 200 OK\r\n");
    }
}

From source file:com.linecorp.armeria.server.HttpServerPathTest.java

License:Apache License

private static void urlPathAssertion(HttpStatus expected, String path) throws Exception {
    final String requestString = "GET " + path + " HTTP/1.0\r\n\r\n";

    try (Socket s = new Socket(NetUtil.LOCALHOST, server.httpPort())) {
        s.setSoTimeout(10000);//  w w w  . j  a v  a  2 s.c  om
        s.getOutputStream().write(requestString.getBytes(StandardCharsets.US_ASCII));
        assertThat(new String(ByteStreams.toByteArray(s.getInputStream()), StandardCharsets.US_ASCII)).as(path)
                .startsWith("HTTP/1.1 " + expected);
    }
}

From source file:com.linecorp.armeria.server.HttpServerTest.java

License:Apache License

@Test
public void testHeadHeadersOnly() throws Exception {
    final int port = server.httpPort();
    try (Socket s = new Socket(NetUtil.LOCALHOST, port)) {
        s.setSoTimeout(10000);/*from  ww w . j  a v a 2 s. c  o m*/
        final InputStream in = s.getInputStream();
        final OutputStream out = s.getOutputStream();
        out.write("HEAD /head-headers-only HTTP/1.0\r\n\r\n".getBytes(StandardCharsets.US_ASCII));

        // Should neither be chunked nor have content.
        assertThat(new String(ByteStreams.toByteArray(in)))
                .isEqualTo("HTTP/1.1 200 OK\r\ncontent-length: 0\r\n\r\n");
    }
}