A Java-based HTTP server that lets you locally test your HTTP client, retry logic, streaming behavior, timeouts, etc. with the endpoints of httpbin.org.
This way, you can write tests without relying on an external dependency like httpbin.org.
Java httpbin supports a subset of httpbin endpoints:
/ipReturns Origin IP./user-agentReturns user-agent./headersReturns headers./deleteReturns DELETE data./getReturns GET data./patchReturns PATCH data./postReturns POST data./putReturns PUT data./anythingReturns anything passed in request data./status/:codeReturns given HTTP Status code./redirect/:n302 Redirects n times./relative-redirect/:n302 Redirects n times./absolute-redirect/:n302 Absolute redirects n times./redirect-to?url=foo302 Redirects to the foo URL./stream/:nStreams n lines of JSON objects./stream-bytes/:n?chunkSize=c&seed=sStreams n bytes./delay/:nDelays responding for min(n, 10) seconds./bytes/:nGenerates n random bytes of binary data, accepts optional seed integer parameter./base64/:sReturns a base64 decoded :s input/range/:sReturn a subset of data based on Content-range header./cookiesReturns the cookies./cookies/set?name=valueSets one or more simple cookies./cookies/delete?nameDeletes one or more simple cookies./drip?numbytes=n&duration=s&delay=s&code=codeDrips data over a duration after an optional initial delay, then optionally returns with the given status code./cacheReturns 200 unless an If-Modified-Since or If-None-Match header is provided, when it returns a 304./cache/:nSets a Cache-Control header for n seconds./etagReturn 200 when If-Match or If-None-Match succeed./gzipReturns gzip-encoded data./deflateReturns deflate-encoded data./robots.txtReturns some robots.txt rules./denyDenied by robots.txt file./basic-auth/:user/:passwdChallenges HTTP Basic Auth./hidden-basic-auth/:user/:passwdChallenges HTTP Basic Auth and returns 404 on failure./htmlReturns some HTML./xmlReturns some XML./image/pngReturns page containing a PNG image./image/jpegReturns page containing a JPEG image.
First add dependency to pom.xml:
<dependency> <groupId>org.gaul</groupId> <artifactId>httpbin</artifactId> <version>1.4.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 void setUp() throws Exception { 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; }
- httpbin - original Python implementation
- go-httpbin - Go reimplementation
Copyright (C) 2018-2023 Andrew Gaul
Copyright (C) 2015-2016 Bounce Storage
Licensed under the Apache License, Version 2.0