1
// Copyright 2014 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
9
"github.com/juju/testing/httptesting"
10
gc "gopkg.in/check.v1"
12
"gopkg.in/juju/charmstore.v4/internal/router"
13
"gopkg.in/juju/charmstore.v4/internal/storetesting"
16
var serverParams = ServerParams{
17
AuthUsername: "test-user",
18
AuthPassword: "test-password",
21
type ServerSuite struct {
22
storetesting.IsolatedMgoESSuite
25
var _ = gc.Suite(&ServerSuite{})
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`)
33
type versionResponse struct {
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{
51
h, err := NewServer(db, nil, serverParams, map[string]NewAPIHandlerFunc{
52
"version1": serveVersion("version1"),
54
c.Assert(err, gc.IsNil)
55
assertServesVersion(c, h, "version1")
56
assertDoesNotServeVersion(c, h, "version2")
57
assertDoesNotServeVersion(c, h, "version3")
59
h, err = NewServer(db, nil, serverParams, map[string]NewAPIHandlerFunc{
60
"version1": serveVersion("version1"),
61
"version2": serveVersion("version2"),
63
c.Assert(err, gc.IsNil)
64
assertServesVersion(c, h, "version1")
65
assertServesVersion(c, h, "version2")
66
assertDoesNotServeVersion(c, h, "version3")
68
h, err = NewServer(db, nil, serverParams, map[string]NewAPIHandlerFunc{
69
"version1": serveVersion("version1"),
70
"version2": serveVersion("version2"),
71
"version3": serveVersion("version3"),
73
c.Assert(err, gc.IsNil)
74
assertServesVersion(c, h, "version1")
75
assertServesVersion(c, h, "version2")
76
assertServesVersion(c, h, "version3")
78
h, err = NewServer(db, nil, serverParams, map[string]NewAPIHandlerFunc{
79
"version1": serveVersion("version1"),
82
c.Assert(err, gc.IsNil)
83
assertServesVersion(c, h, "")
84
assertServesVersion(c, h, "version1")
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) {
93
h, err := NewServer(s.Session.DB("foo"), nil, serverParams, map[string]NewAPIHandlerFunc{
94
"version1": serveConfig,
96
c.Assert(err, gc.IsNil)
97
httptesting.AssertJSONCall(c, httptesting.JSONCallParams{
99
URL: "/version1/some/path",
100
ExpectBody: serverParams,
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) {
110
h, err := NewServer(s.Session.DB("foo"), &SearchIndex{s.ES, s.TestIndex}, serverParams,
111
map[string]NewAPIHandlerFunc{
112
"version1": serveConfig,
114
c.Assert(err, gc.IsNil)
115
httptesting.AssertJSONCall(c, httptesting.JSONCallParams{
117
URL: "/version1/some/path",
118
ExpectBody: serverParams,
122
func assertServesVersion(c *gc.C, h http.Handler, vers string) {
127
httptesting.AssertJSONCall(c, httptesting.JSONCallParams{
129
URL: path + "/some/path",
130
ExpectBody: versionResponse{
137
func assertDoesNotServeVersion(c *gc.C, h http.Handler, vers string) {
138
rec := httptesting.DoRequest(c, httptesting.DoRequestParams{
140
URL: "/" + vers + "/some/path",
142
c.Assert(rec.Code, gc.Equals, http.StatusNotFound)