Fix tests for scopeStore.
Update all our tests to use the PG_TEST_DB environment variable if set, and use
that to control whether or not the postgres tests get run. testing.Short() just
wasn't working.
Update ErrScopeNotFound and ErrScopeAlreadyExists to be variables instead of
types, because PostgreSQL (annoyingly) offers no way to determine which specific
row insertion caused the problem, and I anticipate this being a problem that is
ongoing. So it's really just not worth it.
Stop getScopes from returning an ErrScopeNotFound. Let's return what we find,
and let the absence of what we didn't find speak for itself.
Fix an error with generating the SQL for the postgres.createScopes call. We used
to generate it in a way that was invalid (not joining values with ",") when more
than one set of values was supplied. Hooray, testing!
Update the postgres scopeStore to return ErrScopeNotFound and
ErrScopeAlreadyExists errors, as appropriate.
Update our tests to reflect that ErrScopeNotFound and ErrScopeAlreadyExists are
now variables, not types.
8 "bitbucket.org/ww/goautoneg"
12 requestErrAccessDenied = "access_denied"
13 requestErrInsufficient = "insufficient"
14 requestErrOverflow = "overflow"
15 requestErrInvalidValue = "invalid_value"
16 requestErrInvalidFormat = "invalid_format"
17 requestErrMissing = "missing"
18 requestErrNotFound = "not_found"
19 requestErrConflict = "conflict"
20 requestErrActOfGod = "act_of_god"
24 actOfGodResponse = response{Errors: []requestError{requestError{Slug: requestErrActOfGod}}}
25 invalidFormatResponse = response{Errors: []requestError{requestError{Slug: requestErrInvalidFormat, Field: "/"}}}
27 encoders = []string{"application/json"}
30 type response struct {
31 Errors []requestError `json:"errors,omitempty"`
32 Logins []Login `json:"logins,omitempty"`
33 Profiles []Profile `json:"profiles,omitempty"`
34 Clients []Client `json:"clients,omitempty"`
35 Endpoints []Endpoint `json:"endpoints,omitempty"`
38 type requestError struct {
39 Slug string `json:"error,omitempty"`
40 Field string `json:"field,omitempty"`
41 Param string `json:"param,omitempty"`
42 Header string `json:"header,omitempty"`
45 func negotiate(h http.Handler) http.Handler {
46 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
47 if r.Header.Get("Accept") != "" {
48 contentType := goautoneg.Negotiate(r.Header.Get("Accept"), encoders)
49 if contentType == "" {
50 w.WriteHeader(http.StatusNotAcceptable)
51 w.Write([]byte("Unsupported content type requested: " + r.Header.Get("Accept")))
59 func encode(w http.ResponseWriter, r *http.Request, status int, resp response) {
60 contentType := goautoneg.Negotiate(r.Header.Get("Accept"), encoders)
61 w.Header().Set("content-type", contentType)
65 case "application/json":
66 enc := json.NewEncoder(w)
67 err = enc.Encode(resp)
69 enc := json.NewEncoder(w)
70 err = enc.Encode(resp)
77 func decode(r *http.Request, target interface{}) error {
79 switch r.Header.Get("Content-Type") {
80 case "application/json":
81 dec := json.NewDecoder(r.Body)
82 return dec.Decode(target)
84 dec := json.NewDecoder(r.Body)
85 return dec.Decode(target)
89 func wrap(context Context, f func(w http.ResponseWriter, r *http.Request, context Context)) http.Handler {
90 return negotiate(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {