~juju-qa/ubuntu/xenial/juju/xenial-2.0-beta3

« back to all changes in this revision

Viewing changes to src/gopkg.in/juju/charmstore.v5-unstable/internal/identity/idm_test.go

  • Committer: Martin Packman
  • Date: 2016-03-30 19:31:08 UTC
  • mfrom: (1.1.41)
  • Revision ID: martin.packman@canonical.com-20160330193108-h9iz3ak334uk0z5r
Merge new upstream source 2.0~beta3

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// Copyright 2015 Canonical Ltd.
2
 
// Licensed under the AGPLv3, see LICENCE file for details.
3
 
 
4
 
package identity_test
5
 
 
6
 
import (
7
 
        "encoding/json"
8
 
        "fmt"
9
 
        "net/http"
10
 
        "net/http/httptest"
11
 
        "strconv"
12
 
 
13
 
        gc "gopkg.in/check.v1"
14
 
 
15
 
        "gopkg.in/juju/charmstore.v5-unstable/internal/identity"
16
 
)
17
 
 
18
 
type discharge struct {
19
 
        id string
20
 
        c  chan error
21
 
}
22
 
 
23
 
// idM is a mock identity manager that can be used to test the client.
24
 
type idM struct {
25
 
        *httptest.Server
26
 
        *http.ServeMux
27
 
}
28
 
 
29
 
func newIdM(c *gc.C) *idM {
30
 
        i := &idM{
31
 
                ServeMux: http.NewServeMux(),
32
 
        }
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())
38
 
        return i
39
 
}
40
 
 
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)
43
 
}
44
 
 
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) {
51
 
        req.ParseForm()
52
 
        if req.Form.Get("ct") != "" {
53
 
                w.Header().Set("Content-Type", req.Form.Get("ct"))
54
 
        } else {
55
 
                w.Header().Set("Content-Type", "application/json")
56
 
        }
57
 
        if req.Form.Get("s") != "" {
58
 
                s, err := strconv.Atoi(req.Form.Get("s"))
59
 
                if err != nil {
60
 
                        i.error(w, http.StatusBadRequest, "ERROR", "cannot read status: %s", err)
61
 
                        return
62
 
                }
63
 
                w.WriteHeader(s)
64
 
        }
65
 
        if req.Form.Get("b") != "" {
66
 
                w.Write([]byte(req.Form.Get("b")))
67
 
        } else {
68
 
                data := map[string]interface{}{
69
 
                        "method": req.Method,
70
 
                }
71
 
                resp, err := json.Marshal(data)
72
 
                if err != nil {
73
 
                        i.error(w, http.StatusInternalServerError, "ERROR", "cannot marshal response: %s", err)
74
 
                        return
75
 
                }
76
 
                w.Write(resp)
77
 
        }
78
 
}
79
 
 
80
 
func (i *idM) write(w http.ResponseWriter, v interface{}) {
81
 
        body, err := json.Marshal(v)
82
 
        if err != nil {
83
 
                i.error(w, http.StatusInternalServerError, "ERROR", "cannot marshal response: %s", err)
84
 
                return
85
 
        }
86
 
        w.Header().Set("Content-Type", "application/json")
87
 
        w.Write(body)
88
 
}
89
 
 
90
 
func (i *idM) error(w http.ResponseWriter, status int, code, format string, a ...interface{}) {
91
 
        w.Header().Set("Content-Type", "application/json")
92
 
        w.WriteHeader(status)
93
 
        body, err := json.Marshal(&identity.IdmError{
94
 
                Message: fmt.Sprintf(format, a...),
95
 
                Code:    code,
96
 
        })
97
 
        if err != nil {
98
 
                panic(err)
99
 
        }
100
 
        w.Write(body)
101
 
}
102
 
 
103
 
func (i *idM) serveGroups(groups ...string) http.HandlerFunc {
104
 
        return func(w http.ResponseWriter, _ *http.Request) {
105
 
                i.write(w, groups)
106
 
        }
107
 
}