Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a test for the UnixConnector in the simple mode
- Loading branch information
Showing
5 changed files
with
87 additions
and
3 deletions.
There are no files selected for viewing
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
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
2 changes: 1 addition & 1 deletion
2
...zard-unixsocket/src/main/resources/META-INF/services/io.dropwizard.jetty.ConnectorFactory
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
io.dropwizard.unixsocket.UnixSocketFactory | ||
io.dropwizard.unixsocket.UnixSocketConnectorFactory |
72 changes: 72 additions & 0 deletions
72
...ard-unixsocket/src/test/java/io/dropwizard/unixsocket/UnixSocketConnectorFactoryTest.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package io.dropwizard.unixsocket; | ||
|
||
import com.codahale.metrics.health.HealthCheck; | ||
import io.dropwizard.Application; | ||
import io.dropwizard.Configuration; | ||
import io.dropwizard.setup.Environment; | ||
import io.dropwizard.testing.ResourceHelpers; | ||
import io.dropwizard.testing.junit.DropwizardAppRule; | ||
import jnr.unixsocket.UnixSocketAddress; | ||
import jnr.unixsocket.UnixSocketChannel; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
|
||
import javax.ws.rs.GET; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.Produces; | ||
import javax.ws.rs.core.MediaType; | ||
import java.io.File; | ||
import java.io.InputStreamReader; | ||
import java.io.PrintWriter; | ||
import java.io.Reader; | ||
import java.nio.channels.Channels; | ||
|
||
public class UnixSocketConnectorFactoryTest { | ||
|
||
@Rule | ||
public DropwizardAppRule<Configuration> rule = new DropwizardAppRule<>(HelloWorldApplication.class, | ||
ResourceHelpers.resourceFilePath("yaml/usock-server.yml")); | ||
|
||
private File socket = new File("/tmp/dropwizard.sock"); | ||
|
||
@Test | ||
public void testClient() throws Exception { | ||
String httpRequest = "GET /app/hello HTTP/1.1\r\n" + | ||
"Host: dropwizard-unixsock\r\n" + | ||
"\r\n"; | ||
try (UnixSocketChannel channel = UnixSocketChannel.open(new UnixSocketAddress(socket)); | ||
PrintWriter writer = new PrintWriter(Channels.newOutputStream(channel)); | ||
Reader reader = new InputStreamReader(Channels.newInputStream(channel))) { | ||
writer.print(httpRequest); | ||
writer.flush(); | ||
|
||
char[] buf = new char[4096]; | ||
int read = reader.read(buf); | ||
System.out.println("Read from the server: " + new String(buf, 0, read)); | ||
} | ||
} | ||
|
||
public static class HelloWorldApplication extends Application<Configuration> { | ||
|
||
@Override | ||
public void run(Configuration configuration, Environment environment) throws Exception { | ||
environment.jersey().register(new FakeResource()); | ||
environment.healthChecks().register("hello-check", new HealthCheck() { | ||
@Override | ||
protected Result check() throws Exception { | ||
return Result.healthy(); | ||
} | ||
}); | ||
} | ||
|
||
@Path("/hello") | ||
@Produces(MediaType.APPLICATION_JSON) | ||
public static class FakeResource { | ||
|
||
@GET | ||
public String get() throws Exception { | ||
return "{\"hello\": \"World\"}"; | ||
} | ||
} | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
dropwizard-unixsocket/src/test/resources/yaml/usock-server.yml
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
server: | ||
type: simple | ||
connector: | ||
type: unixsocket | ||
applicationContextPath: /app | ||
adminContextPath: /admin |