Skip to content

alechenninger/httpserverrule

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

httpserverrule

JUnit4 rule for serving files from classpath or filesystem over HTTP

Using it

public class HttpServerRuleTest {

  @Rule
  public HttpServerRule server = HttpServerRule.builder()
      .stopAfterStatement()
      .build();

  @Test
  public void shouldServeClassPathByDefault() throws IOException {
    HttpTransport client = new NetHttpTransport();

    String footxt = client.createRequestFactory()
        .buildGetRequest(new GenericUrl(server.urlForPath("foo.txt")))
        .execute()
        .parseAsString();

    assertEquals("Hi", footxt.trim());
  }
}

By default, the server is a long running process which shuts down on JVM exit, though to take advantage of this you will need to reuse a rule instance, like:

public abstract class HttpServers {
  public static HttpServerRule longRunning() {
    return HttpServerRule.builder()
        .servingClasspathResources("/pages/")
        .build();
  }
}

public class MyHtmlTest {
  @ClassRule
  public static HttpServerRule server = HttpServers.longRunning();
  
  @Test
  public void shouldBeCool() {
    // use server...
  }

About

JUnit4 rule for serving files from classpath or filesystem over HTTP

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages