~juju-qa/ubuntu/trusty/juju/juju-1.25.8

« back to all changes in this revision

Viewing changes to src/gopkg.in/juju/charmstore.v4/internal/charmstore/server_test.go

  • Committer: Nicholas Skaggs
  • Date: 2016-12-02 18:01:10 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161202180110-dl1helep8qfebmhx
ImportĀ upstreamĀ 1.25.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2014 Canonical Ltd.
 
2
// Licensed under the AGPLv3, see LICENCE file for details.
 
3
 
 
4
package charmstore
 
5
 
 
6
import (
 
7
        "net/http"
 
8
 
 
9
        "github.com/juju/testing/httptesting"
 
10
        gc "gopkg.in/check.v1"
 
11
 
 
12
        "gopkg.in/juju/charmstore.v4/internal/router"
 
13
        "gopkg.in/juju/charmstore.v4/internal/storetesting"
 
14
)
 
15
 
 
16
var serverParams = ServerParams{
 
17
        AuthUsername: "test-user",
 
18
        AuthPassword: "test-password",
 
19
}
 
20
 
 
21
type ServerSuite struct {
 
22
        storetesting.IsolatedMgoESSuite
 
23
}
 
24
 
 
25
var _ = gc.Suite(&ServerSuite{})
 
26
 
 
27
func (s *ServerSuite) TestNewServerWithNoVersions(c *gc.C) {
 
28
        h, err := NewServer(s.Session.DB("foo"), nil, serverParams, nil)
 
29
        c.Assert(err, gc.ErrorMatches, `charm store server must serve at least one version of the API`)
 
30
        c.Assert(h, gc.IsNil)
 
31
}
 
32
 
 
33
type versionResponse struct {
 
34
        Version string
 
35
        Path    string
 
36
}
 
37
 
 
38
func (s *ServerSuite) TestNewServerWithVersions(c *gc.C) {
 
39
        db := s.Session.DB("foo")
 
40
        serveVersion := func(vers string) NewAPIHandlerFunc {
 
41
                return func(p *Pool, config ServerParams) http.Handler {
 
42
                        return router.HandleJSON(func(_ http.Header, req *http.Request) (interface{}, error) {
 
43
                                return versionResponse{
 
44
                                        Version: vers,
 
45
                                        Path:    req.URL.Path,
 
46
                                }, nil
 
47
                        })
 
48
                }
 
49
        }
 
50
 
 
51
        h, err := NewServer(db, nil, serverParams, map[string]NewAPIHandlerFunc{
 
52
                "version1": serveVersion("version1"),
 
53
        })
 
54
        c.Assert(err, gc.IsNil)
 
55
        assertServesVersion(c, h, "version1")
 
56
        assertDoesNotServeVersion(c, h, "version2")
 
57
        assertDoesNotServeVersion(c, h, "version3")
 
58
 
 
59
        h, err = NewServer(db, nil, serverParams, map[string]NewAPIHandlerFunc{
 
60
                "version1": serveVersion("version1"),
 
61
                "version2": serveVersion("version2"),
 
62
        })
 
63
        c.Assert(err, gc.IsNil)
 
64
        assertServesVersion(c, h, "version1")
 
65
        assertServesVersion(c, h, "version2")
 
66
        assertDoesNotServeVersion(c, h, "version3")
 
67
 
 
68
        h, err = NewServer(db, nil, serverParams, map[string]NewAPIHandlerFunc{
 
69
                "version1": serveVersion("version1"),
 
70
                "version2": serveVersion("version2"),
 
71
                "version3": serveVersion("version3"),
 
72
        })
 
73
        c.Assert(err, gc.IsNil)
 
74
        assertServesVersion(c, h, "version1")
 
75
        assertServesVersion(c, h, "version2")
 
76
        assertServesVersion(c, h, "version3")
 
77
 
 
78
        h, err = NewServer(db, nil, serverParams, map[string]NewAPIHandlerFunc{
 
79
                "version1": serveVersion("version1"),
 
80
                "":         serveVersion(""),
 
81
        })
 
82
        c.Assert(err, gc.IsNil)
 
83
        assertServesVersion(c, h, "")
 
84
        assertServesVersion(c, h, "version1")
 
85
}
 
86
 
 
87
func (s *ServerSuite) TestNewServerWithConfig(c *gc.C) {
 
88
        serveConfig := func(p *Pool, config ServerParams) http.Handler {
 
89
                return router.HandleJSON(func(_ http.Header, req *http.Request) (interface{}, error) {
 
90
                        return config, nil
 
91
                })
 
92
        }
 
93
        h, err := NewServer(s.Session.DB("foo"), nil, serverParams, map[string]NewAPIHandlerFunc{
 
94
                "version1": serveConfig,
 
95
        })
 
96
        c.Assert(err, gc.IsNil)
 
97
        httptesting.AssertJSONCall(c, httptesting.JSONCallParams{
 
98
                Handler:    h,
 
99
                URL:        "/version1/some/path",
 
100
                ExpectBody: serverParams,
 
101
        })
 
102
}
 
103
 
 
104
func (s *ServerSuite) TestNewServerWithElasticSearch(c *gc.C) {
 
105
        serveConfig := func(p *Pool, config ServerParams) http.Handler {
 
106
                return router.HandleJSON(func(_ http.Header, req *http.Request) (interface{}, error) {
 
107
                        return config, nil
 
108
                })
 
109
        }
 
110
        h, err := NewServer(s.Session.DB("foo"), &SearchIndex{s.ES, s.TestIndex}, serverParams,
 
111
                map[string]NewAPIHandlerFunc{
 
112
                        "version1": serveConfig,
 
113
                })
 
114
        c.Assert(err, gc.IsNil)
 
115
        httptesting.AssertJSONCall(c, httptesting.JSONCallParams{
 
116
                Handler:    h,
 
117
                URL:        "/version1/some/path",
 
118
                ExpectBody: serverParams,
 
119
        })
 
120
}
 
121
 
 
122
func assertServesVersion(c *gc.C, h http.Handler, vers string) {
 
123
        path := vers
 
124
        if path != "" {
 
125
                path = "/" + path
 
126
        }
 
127
        httptesting.AssertJSONCall(c, httptesting.JSONCallParams{
 
128
                Handler: h,
 
129
                URL:     path + "/some/path",
 
130
                ExpectBody: versionResponse{
 
131
                        Version: vers,
 
132
                        Path:    "/some/path",
 
133
                },
 
134
        })
 
135
}
 
136
 
 
137
func assertDoesNotServeVersion(c *gc.C, h http.Handler, vers string) {
 
138
        rec := httptesting.DoRequest(c, httptesting.DoRequestParams{
 
139
                Handler: h,
 
140
                URL:     "/" + vers + "/some/path",
 
141
        })
 
142
        c.Assert(rec.Code, gc.Equals, http.StatusNotFound)
 
143
}