-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract option funcs and response types into separate files for clari…
…ty (#102)
- Loading branch information
Showing
4 changed files
with
163 additions
and
155 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package httpbin | ||
|
||
import "time" | ||
|
||
// OptionFunc uses the "functional options" pattern to customize an HTTPBin | ||
// instance | ||
type OptionFunc func(*HTTPBin) | ||
|
||
// WithDefaultParams sets the default params handlers will use | ||
func WithDefaultParams(defaultParams DefaultParams) OptionFunc { | ||
return func(h *HTTPBin) { | ||
h.DefaultParams = defaultParams | ||
} | ||
} | ||
|
||
// WithMaxBodySize sets the maximum amount of memory | ||
func WithMaxBodySize(m int64) OptionFunc { | ||
return func(h *HTTPBin) { | ||
h.MaxBodySize = m | ||
} | ||
} | ||
|
||
// WithMaxDuration sets the maximum amount of time httpbin may take to respond | ||
func WithMaxDuration(d time.Duration) OptionFunc { | ||
return func(h *HTTPBin) { | ||
h.MaxDuration = d | ||
} | ||
} | ||
|
||
// WithHostname sets the hostname to return via the /hostname endpoint. | ||
func WithHostname(s string) OptionFunc { | ||
return func(h *HTTPBin) { | ||
h.hostname = s | ||
} | ||
} | ||
|
||
// WithObserver sets the request observer callback | ||
func WithObserver(o Observer) OptionFunc { | ||
return func(h *HTTPBin) { | ||
h.Observer = o | ||
} | ||
} | ||
|
||
// WithAllowedRedirectDomains limits the domains to which the /redirect-to | ||
// endpoint will redirect traffic. | ||
func WithAllowedRedirectDomains(hosts []string) OptionFunc { | ||
return func(h *HTTPBin) { | ||
hostSet := make(map[string]struct{}, len(hosts)) | ||
for _, host := range hosts { | ||
hostSet[host] = struct{}{} | ||
} | ||
h.AllowedRedirectDomains = hostSet | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package httpbin | ||
|
||
import ( | ||
"net/http" | ||
"net/url" | ||
) | ||
|
||
const ( | ||
jsonContentType = "application/json; encoding=utf-8" | ||
htmlContentType = "text/html; charset=utf-8" | ||
) | ||
|
||
type headersResponse struct { | ||
Headers http.Header `json:"headers"` | ||
} | ||
|
||
type ipResponse struct { | ||
Origin string `json:"origin"` | ||
} | ||
|
||
type userAgentResponse struct { | ||
UserAgent string `json:"user-agent"` | ||
} | ||
|
||
// A generic response for any incoming request that should not contain a body | ||
// (GET, HEAD, OPTIONS, etc). | ||
type noBodyResponse struct { | ||
Args url.Values `json:"args"` | ||
Headers http.Header `json:"headers"` | ||
Origin string `json:"origin"` | ||
URL string `json:"url"` | ||
|
||
Deflated bool `json:"deflated,omitempty"` | ||
Gzipped bool `json:"gzipped,omitempty"` | ||
} | ||
|
||
// A generic response for any incoming request that might contain a body (POST, | ||
// PUT, PATCH, etc). | ||
type bodyResponse struct { | ||
Args url.Values `json:"args"` | ||
Headers http.Header `json:"headers"` | ||
Origin string `json:"origin"` | ||
URL string `json:"url"` | ||
|
||
Data string `json:"data"` | ||
Files map[string][]string `json:"files"` | ||
Form map[string][]string `json:"form"` | ||
JSON interface{} `json:"json"` | ||
} | ||
|
||
type cookiesResponse map[string]string | ||
|
||
type authResponse struct { | ||
Authorized bool `json:"authorized"` | ||
User string `json:"user"` | ||
} | ||
|
||
// An actual stream response body will be made up of one or more of these | ||
// structs, encoded as JSON and separated by newlines | ||
type streamResponse struct { | ||
ID int `json:"id"` | ||
Args url.Values `json:"args"` | ||
Headers http.Header `json:"headers"` | ||
Origin string `json:"origin"` | ||
URL string `json:"url"` | ||
} | ||
|
||
type uuidResponse struct { | ||
UUID string `json:"uuid"` | ||
} | ||
|
||
type bearerResponse struct { | ||
Authenticated bool `json:"authenticated"` | ||
Token string `json:"token"` | ||
} | ||
|
||
type hostnameResponse struct { | ||
Hostname string `json:"hostname"` | ||
} |