https://github.com/andriykalashnykov/go-httpbin
go-httpbin
https://github.com/andriykalashnykov/go-httpbin
Last synced: 8 months ago
JSON representation
go-httpbin
- Host: GitHub
- URL: https://github.com/andriykalashnykov/go-httpbin
- Owner: AndriyKalashnykov
- License: apache-2.0
- Created: 2023-05-25T03:23:42.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2025-02-20T03:01:24.000Z (8 months ago)
- Last Synced: 2025-02-21T03:44:04.419Z (8 months ago)
- Language: Go
- Size: 23.4 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://github.com/AndriyKalashnykov/go-httpbin/actions/workflows/ci.yml)
[](https://hits.seeyoufarm.com)
[](https://opensource.org/licenses/Apache-2.0)
[](https://app.renovatebot.com/dashboard#github/AndriyKalashnykov/go-httpbin)
[](https://godoc.org/github.com/AndriyKalashnykov/go-httpbin)
# go-httpbinA Go handler that lets you test your HTTP client, retry logic, streaming behavior, timeouts etc.
with the endpoints of [httpbin.org](http://httpbin.org) locally in a [net/http/httptest.Server](https://pkg.go.dev/net/http/httptest).This way, you can write tests without relying on an external dependency like [httpbin.org].
## Endpoints
- `/ip` Returns Origin IP.
- `/user-agent` Returns user-agent.
- `/headers` Returns headers.
- `/get` Returns GET 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.
- `/stream/:n` Streams _n_ lines of JSON objects.
- `/delay/:n` Delays responding for _min(n, 10)_ seconds.
- `/bytes/:n` Generates _n_ random bytes of binary data, accepts optional _seed_ integer parameter.
- `/cookies` Returns the cookies.
- `/cookies/set?name=value` Sets one or more simple cookies.
- `/cookies/delete?name` Deletes one or more simple cookies.
- `/drip?numbytes=n&duration=s&delay=s&code=code` Drips data over a duration after
an optional initial _delay_, then optionally returns with the given status _code_.
- `/cache` Returns 200 unless an If-Modified-Since or If-None-Match header is provided, when it returns a 304.
- `/cache/:n` Sets a Cache-Control header for _n_ seconds.
- `/gzip` Returns gzip-encoded data.
- `/deflate` Returns deflate-encoded data.
- `/brotli` Returns brotli-encoded data.
- `/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/gif` Returns page containing an animated GIF image.
- `/image/png` Returns page containing a PNG image.
- `/image/jpeg` Returns page containing a JPEG image.## How to use
Standing up a Go server running httpbin endpoints is just 1 line:
```go
package mainimport (
"log"
"net/http"
"github.com/AndriyKalashnykov/go-httpbin"
)func main() {
log.Fatal(http.ListenAndServe(":8080", httpbin.GetMux()))
}
```Let's say you do not want a server running all the time because you just want to
test your HTTP logic after all. Integrating `httpbin` to your tests is very simple:```go
package testimport (
"testing"
"net/http"
"net/http/httptest""github.com/AndriyKalashnykov/go-httpbin"
)func TestDownload(t *testing.T) {
srv := httptest.NewServer(httpbin.GetMux())
defer srv.Close()resp, err := http.Get(srv.URL + "/bytes/65536")
if err != nil {
t.Fatal(err)
}
// read from an actual HTTP server hosted locally
// test whatever you are going to test...
}
```go-httpbin works from the command line as well:
```
$ go install github.com/AndriyKalashnykov/go-httpbin/cmd/httpbin
$ $GOPATH/bin/httpbin -host :8080
```