1
// Copyright 2015 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
13
gc "gopkg.in/check.v1"
15
"gopkg.in/juju/charmstore.v5-unstable/internal/identity"
18
type discharge struct {
23
// idM is a mock identity manager that can be used to test the client.
29
func newIdM(c *gc.C) *idM {
31
ServeMux: http.NewServeMux(),
33
i.Server = httptest.NewServer(i)
34
i.Handle("/", http.HandlerFunc(i.notFound))
35
i.Handle("/test", http.HandlerFunc(i.serveTest))
36
i.Handle("/v1/u/user1/groups", i.serveGroups("g1", "g2"))
37
i.Handle("/v1/u/user2/groups", i.serveGroups())
41
func (i *idM) notFound(w http.ResponseWriter, req *http.Request) {
42
i.error(w, http.StatusNotFound, "not found", "%s not found", req.URL.Path)
45
// serveTest serves a /test endpoint that can return a number of things
46
// depending on the query parameters:
47
// ct = Content-Type to use (application/json)
48
// s = Status code to use (200)
49
// b = body content ({"method": method used})
50
func (i *idM) serveTest(w http.ResponseWriter, req *http.Request) {
52
if req.Form.Get("ct") != "" {
53
w.Header().Set("Content-Type", req.Form.Get("ct"))
55
w.Header().Set("Content-Type", "application/json")
57
if req.Form.Get("s") != "" {
58
s, err := strconv.Atoi(req.Form.Get("s"))
60
i.error(w, http.StatusBadRequest, "ERROR", "cannot read status: %s", err)
65
if req.Form.Get("b") != "" {
66
w.Write([]byte(req.Form.Get("b")))
68
data := map[string]interface{}{
71
resp, err := json.Marshal(data)
73
i.error(w, http.StatusInternalServerError, "ERROR", "cannot marshal response: %s", err)
80
func (i *idM) write(w http.ResponseWriter, v interface{}) {
81
body, err := json.Marshal(v)
83
i.error(w, http.StatusInternalServerError, "ERROR", "cannot marshal response: %s", err)
86
w.Header().Set("Content-Type", "application/json")
90
func (i *idM) error(w http.ResponseWriter, status int, code, format string, a ...interface{}) {
91
w.Header().Set("Content-Type", "application/json")
93
body, err := json.Marshal(&identity.IdmError{
94
Message: fmt.Sprintf(format, a...),
103
func (i *idM) serveGroups(groups ...string) http.HandlerFunc {
104
return func(w http.ResponseWriter, _ *http.Request) {