Skip to content
A Java-based HTTP server that lets you test your HTTP client, retry logic, streaming behavior, timeouts etc. with the endpoints of httpbin.org locally.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src
.gitignore
.travis.yml
LICENSE
README.md
pom.xml

README.md

Java httpbin

Build Status Maven Central

A Java-based HTTP server that lets you test your HTTP client, retry logic, streaming behavior, timeouts etc. with the endpoints of httpbin.org locally.

This way, you can write tests without relying on an external dependency like httpbin.org.

Endpoints

Java httpbin supports a subset of httpbin endpoints:

  • /ip Returns Origin IP.
  • /user-agent Returns user-agent.
  • /headers Returns headers.
  • /get Returns GET data.
  • /post Returns POST data.
  • /put Returns PUT data.
  • /status/:code Returns given HTTP Status code.
  • /redirect/:n 302 Redirects n times.
  • /absolute-redirect/:n 302 Absolute redirects n times.
  • /redirect-to?url=foo 302 Redirects to the foo URL.
  • /cookies Returns the cookies.
  • /cookies/set?name=value Sets one or more simple cookies.
  • /cookies/delete?name Deletes one or more simple cookies.
  • /robots.txt Returns some robots.txt rules.
  • /deny Denied by robots.txt file.
  • /basic-auth/:user/:passwd Challenges HTTP Basic Auth.
  • /hidden-basic-auth/:user/:passwd Challenges HTTP Basic Auth and returns 404 on failure.
  • /html Returns some HTML.
  • /xml Returns some XML.
  • /image/png Returns page containing a PNG image.
  • /image/jpeg Returns page containing a JPEG image.

Usage

First add dependency to pom.xml:

<dependency>
  <groupId>org.gaul</groupId>
  <artifactId>httpbin</artifactId>
  <version>1.1.0</version>
</dependency>

Then add to your test code:

private URI httpBinEndpoint = URI.create("http://127.0.0.1:0");
private final HttpBin httpBin = new HttpBin(httpBinEndpoint);

@Before
public setUp() {
    httpBin.start();

    // reset endpoint to handle zero port
    httpBinEndpoint = new URI(httpBinEndpoint.getScheme(),
            httpBinEndpoint.getUserInfo(), httpBinEndpoint.getHost(),
            httpBin.getPort(), httpBinEndpoint.getPath(),
            httpBinEndpoint.getQuery(), httpBinEndpoint.getFragment());
}

@After
public void tearDown() throws Exception {
    httpBin.stop();
}

@Test
public void test() throws Exception {
    URI uri = URI.create(httpBinEndpoint + "/status/200");
    HttpURLConnection conn = (HttpURLConnection) uri.toURL().openConnection();
    assert conn.getResponseCode() == 200;
}

References

License

Copyright (C) 2018 Andrew Gaul
Copyright (C) 2015-2016 Bounce Storage

Licensed under the Apache License, Version 2.0

You can’t perform that action at this time.