httpbin package - github.com/ahmetb/go-httpbin - Go Packages (original) (raw)

Package httpbin providers HTTP handlers for httpbin.org endpoints and a multiplexer to directly hook it up to any http.Server or httptest.Server.

This section is empty.

func AbsoluteRedirectHandler

AbsoluteRedirectHandler returns a 302 Found response if n=1 pointing to /host/get, otherwise to /host/absolute-redirect/(n-1)

func BasicAuthHandler

BasicAuthHandler challenges with given username and password.

func BrotliHandler

BrotliHandler returns a Brotli-encoded response

func BytesHandler

BytesHandler returns n random bytes of binary data and accepts an optional 'seed' integer query parameter.

func CacheHandler

CacheHandler returns 200 with the response of /get unless an If-Modified-Since or If-None-Match header is provided, when it returns a 304.

func CookiesHandler

CookiesHandler returns the cookies provided in the request.

func DeflateHandler

DeflateHandler returns a DEFLATE-encoded response.

func DelayHandler

DelayHandler delays responding for min(n, 10) seconds and responds with /get endpoint

func DeleteCookiesHandler

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.

func DenyHandler

DenyHandler returns a plain-text response.

func DripHandler

DripHandler drips data over a duration after an optional initial delay, then optionally returns with the given status code.

func GZIPHandler

GZIPHandler returns a GZIP-encoded response

GetMux returns the mux with handlers for httpbin endpoints registered.

package main

import ( "fmt" "io/ioutil" "log" "net/http" "net/http/httptest"

httpbin "github.com/ahmetb/go-httpbin"

)

func main() { srv := httptest.NewServer(httpbin.GetMux()) defer srv.Close()

resp, err := http.Get(srv.URL + "/bytes/65536")
if err != nil {
    log.Fatal(err)
}
defer resp.Body.Close()

// read from an actual HTTP server hosted locally
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
    log.Fatal(err)
}
fmt.Printf("Retrieved %d bytes.\n", len(b))

}

Output:

Retrieved 65536 bytes.

package main

import ( "log" "net/http"

httpbin "github.com/ahmetb/go-httpbin"

)

func main() { log.Fatal(http.ListenAndServe(":8080", httpbin.GetMux())) }

func HTMLHandler

HTMLHandler returns some HTML response.

func HiddenBasicAuthHandler

HiddenBasicAuthHandler challenges with given username and password and returns 404 if authentication fails.

func HomeHandler

HomeHandler serves static HTML content for the index page.

func PostHandler

PostHandler accept a post and echo its data back

func RedirectHandler

RedirectHandler returns a 302 Found response if n=1 pointing to /get, otherwise to /redirect/(n-1)

func RedirectToHandler

RedirectToHandler returns a 302 Found response pointing to the url query parameter

func SetCacheHandler

SetCacheHandler sets a Cache-Control header for n seconds and returns with the /get response.

func SetCookiesHandler

SetCookiesHandler sets the query key/value pairs as cookies in the response and returns a 302 redirect to /cookies.

func StatusHandler

StatusHandler returns a proper response for provided status code

func StreamHandler

StreamHandler writes a json object to a new line every second.

func XMLHandler

XMLHandler returns some XML response.

This section is empty.