modulepackage
0.0.0-20240315150752-da45896c98cb
Repository: https://github.com/ahmetb/go-httpbin.git
Documentation: pkg.go.dev
# README
go-httpbin
A Go handler that lets you test your HTTP client, retry logic, streaming behavior, timeouts etc.
with the endpoints of httpbin.org locally in a net/http/httptest.Server
.
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:
package main
import (
"log"
"net/http"
"github.com/ahmetb/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:
package test
import (
"testing"
"net/http"
"net/http/httptest"
"github.com/ahmetb/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/ahmetb/go-httpbin/cmd/httpbin@latest
$ $GOPATH/bin/httpbin -host :8080
Development
You must have the following tools installed on your system:
To get started, simply run glide install
to install all the dependencies of this package.
Then, run go test $(glide nv)
to test it.
License
Copyright 2016 Ahmet Alp Balkan
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Authors
- Ahmet Alp Balkan (@ahmetb)
# Packages
No description provided by the author
# Functions
AbsoluteRedirectHandler returns a 302 Found response if n=1 pointing to /host/get, otherwise to /host/absolute-redirect/(n-1).
BasicAuthHandler challenges with given username and password.
BrotliHandler returns a Brotli-encoded response.
BytesHandler returns n random bytes of binary data and accepts an optional 'seed' integer query parameter.
CacheHandler returns 200 with the response of /get unless an If-Modified-Sinceor If-None-Match header is provided, when it returns a 304.
CookiesHandler returns the cookies provided in the request.
DeflateHandler returns a DEFLATE-encoded response.
DelayHandler delays responding for min(n, 10) seconds and responds with /get endpoint.
DeleteCookiesHandler deletes cookies with provided query value keys in the response by settings a Unix epoch expiration date and returns a 302 redirect to /cookies.
DenyHandler returns a plain-text response.
DripHandler drips data over a duration after an optional initial delay, then optionally returns with the given status code.
GetHandler returns user agent.
GetMux returns the mux with handlers for httpbin endpoints registered.
GIFHandler returns an animated GIF image.
GZIPHandler returns a GZIP-encoded response.
HeadersHandler returns user agent.
HiddenBasicAuthHandler challenges with given username and password and returns 404 if authentication fails.
HomeHandler serves static HTML content for the index page.
HTMLHandler returns some HTML response.
IPHandler returns Origin IP.
JPEGHandler returns a JPEG image.
PNGHandler returns a PNG image.
PostHandler accept a post and echo its data back.
RedirectHandler returns a 302 Found response if n=1 pointing to /get, otherwise to /redirect/(n-1).
RedirectToHandler returns a 302 Found response pointing to the url query parameter.
RobotsTXTHandler returns a robots.txt response.
SetCacheHandler sets a Cache-Control header for n seconds and returns with the /get response.
SetCookiesHandler sets the query key/value pairs as cookies in the response and returns a 302 redirect to /cookies.
StatusHandler returns a proper response for provided status code.
StreamHandler writes a json object to a new line every second.
UserAgentHandler returns user agent.
XMLHandler returns some XML response.
# Variables
BinaryChunkSize is buffer length used for stuff like generating large blobs.
DelayMax is the maximum execution time for /delay endpoint.
StreamInterval is the default interval between writing objects to the stream.