modulepackage
0.0.0-20191015185716-f38779cea76e
Repository: https://github.com/rubanbydesign/rest.git
Documentation: pkg.go.dev
# README
A simple package for quick bootstrapping of REST API creation in the Google App Engine Go environment. It's currently used in production on several sites.
Documentation forthcoming.
Testing handlers is easy
There are built-in test functions to make testing easy. For example:
import (
"github.com/rubanbydesign/context"
"github.com/rubanbydesign/rest"
)
type Foo struct {
Bar string `json:"bar"`
}
func myHandler(ctx context.Context) error {
return rest.JSON(ctx, http.StatusOK, &Foo{Bar: "foobar"})
}
func TestSomeHandler() {
var f Foo
r := TestPostJSON("/foo/bar", &f)
assert.NoError(t, r.Do(myHandler))
assert.NoError(t, r.Decode(&f))
assert.Equal(t, http.StatusOK, r.Writer.Code)
assert.Equal(t, "foobar", f.Bar)
}
If you need more control over the request, like custom headers, etc., then
you can update it after creation and before calling Do()
:
func TestSomeHandler() {
var f Foo
r := TestPostJSON("/foo/bar", &f)
r.Request.Header.Set("Authorization", "Bearer abc123")
assert.NoError(t, r.Do(myHandler))
assert.NoError(t, r.Decode(&f))
assert.Equal(t, http.StatusOK, r.Writer.Code)
assert.Equal(t, "foobar", f.Bar)
}
# Functions
Body returns the body of the request as a byte slice.
BodyString returns the body of the request as a string.
Bytes writes the bytes to the HTTP response with the given code and content type.
CSS writes the raw CSS to the HTTP connection.
Decode decodes the http request body in JSON format into the dst variable.
Error writes the error string as the HTTP response.
Font serves the byte slice as a truetype font.
FormFile matches the "net/http".Request.FormFile api.
FormValue returns the form value (or mux.Vars value) from the request.
GetCode returns the http response code associated with the request.
Handler is a chainable set of AppHandler middleware funcs.
Header returns the ResponseWriter headers.
Headers returns the http.Request headers.
HTML writes the raw HTML to the HTTP connection.
Init returns a context with the reader, writer, and other context variables set.
JPEG writes the image to the HTTP connection.
JSON writes the encoded data to the HTTP connection.
New creates a new initialized router.
No description provided by the author
NoContent handles responses without any content.
PNG writes the image to the HTTP connection.
Redirect redirects the request to the given location.
Request returns the http.Request asso.
ResponseWriter returns the response writer for the given context.
Status writes the HTTP response code with the default status text for that code.
TestClient creates a http.Client which will return the given response.
TestPost bootstraps a POST request to the given urlStr with the given request body.
TestPostForm bootstraps a POST request to the given urlStr with the form as the request body.
TestPostJSON bootstraps a POST request to the given urlStr with encoded JSON data as the request body.
TestPostXML bootstraps a POST request to the given urlStr with encoded XML data as the request body.
Text writes the string to the HTTP connection as text/plain content type.
Unauthorized returns a 401 error with 401 status text.
No description provided by the author
XML writes the XML encoded data to the HTTP connection.
# Constants
HTTP methods.
HTTP methods.
HTTP methods.
HTTP methods.
Headers.
Headers.
Headers.
Headers.
Headers.
Headers.
Headers.
Headers.
Headers.
Access control.
Headers.
Headers.
Headers.
Headers.
Headers.
Headers.
Headers.
Headers.
Headers.
Headers.
Headers.
Headers.
Headers.
Headers.
Security.
Headers.
Headers.
Headers.
Headers.
Headers.
Headers.
Headers.
Headers.
Headers.
Headers.
Headers.
Headers.
Headers.
Headers.
Headers.
MIME type definitions.
MIME type definitions.
MIME type definitions.
MIME type definitions.
MIME type definitions.
MIME type definitions.
MIME type definitions.
MIME type definitions.
MIME type definitions.
MIME type definitions.
MIME type definitions.
MIME type definitions.
MIME type definitions.
MIME type definitions.
MIME type definitions.
MIME type definitions.
MIME type definitions.
HTTP methods.
HTTP methods.
HTTP methods.
HTTP methods.
RFC 7231, 6.3.3.
RFC 5842, 7.1.
RFC 7231, 6.6.3.
RFC 7231, 6.5.1.
RFC 7231, 6.5.8.
RFC 7231, 6.2.1.
RFC 7231, 6.3.2.
RFC 7231, 6.5.14.
RFC 4918, 11.4.
RFC 7231, 6.5.3.
RFC 7231, 6.4.3.
RFC 7231, 6.6.5.
RFC 7231, 6.5.9.
RFC 7231, 6.6.6.
RFC 3229, 10.4.1.
RFC 4918, 11.5.
RFC 7231, 6.6.1.
RFC 7231, 6.5.10.
RFC 4918, 11.3.
RFC 5842, 7.2.
RFC 7231, 6.5.5.
RFC 7231, 6.4.2.
RFC 7231, 6.4.1.
RFC 4918, 11.1.
RFC 6585, 6.
RFC 7231, 6.3.5.
RFC 7231, 6.3.4.
RFC 7231, 6.5.6.
RFC 2774, 7.
RFC 7231, 6.5.4.
RFC 7231, 6.6.2.
RFC 7232, 4.1.
RFC 7231, 6.3.1.
RFC 7233, 4.1.
RFC 7231, 6.5.2.
RFC 7538, 3.
RFC 7232, 4.2.
RFC 6585, 3.
RFC 2518, 10.1.
RFC 7235, 3.2.
RFC 7233, 4.4.
RFC 7231, 6.5.11.
RFC 6585, 5.
RFC 7231, 6.5.7.
RFC 7231, 6.5.12.
RFC 7231, 6.3.6.
RFC 7231, 6.4.4.
RFC 7231, 6.6.4.
RFC 7231, 6.2.2.
RFC 7168, 2.3.3.
RFC 7231, 6.4.7.
RFC 6585, 4.
RFC 7235, 3.1.
RFC 7725, 3.
RFC 4918, 11.2.
RFC 7231, 6.5.13.
RFC 7231, 6.5.15.
RFC 7231, 6.4.5.
RFC 2295, 8.1.
HTTP methods.
# Variables
Context key standardized definitions.
Context key standardized definitions.
Context key standardized definitions.
Context key standardized definitions.
Context key standardized definitions.
Context key standardized definitions.
Context key standardized definitions.
Context key standardized definitions.
Context key standardized definitions.
Criticalf is shorthand for the appeninge/log func with the same name, with the added advantage that it's a variable and can be overridden if needed.
Debugf is shorthand for the appeninge/log func with the same name, with the added advantage that it's a variable and can be overridden if needed.
No description provided by the author
No description provided by the author
Errorf is shorthand for the appeninge/log func with the same name, with the added advantage that it's a variable and can be overridden if needed.
No description provided by the author
Fatalf is shorthand for the appeninge/log func with the same name, with the added advantage that it's a variable and can be overridden if needed.
GetErrorCode allows customizing of the http.StatusCode for any given error.
Infof is shorthand for the appeninge/log func with the same name, with the added advantage that it's a variable and can be overridden if needed.
Namespace enables setting custom namespace.
OnError is a custom error handler definition.
OnPanic when defined, will handle any panics from resulting funcs.
OnUnauthorized handles the response for 401 responses.
UserFunc is a used defined function which defines which user is associated with the current request.
Warningf is shorthand for the appeninge/log func with the same name, with the added advantage that it's a variable and can be overridden if needed.
# Structs
RoundTripper provides a easy way to implement the http.RoundTripper interface for simulating HTTP responses without having to make an HTTP request.
No description provided by the author
# Interfaces
ResponseBytes defines an interface which returns a Body() of a byte slice.
ResponseCode defines an interface which returns an HTTP response code.
ResponseContentType defines an interface which returns a content-type.
ResponseReader defines an interface with returns a response body of a io.Readoer.
ResponseString defines an interface with returns a response body of a string.
# Type aliases
AppHandler is the wrapper for all HTTP requests.
LogFunc is a custom log function type.
MIME is a string which implements the ContentType interface.
StatusCode is an HTTP status code, with associated error messages.