~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/juju/gomaasapi/util_test.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2012-2016 Canonical Ltd.
 
2
// Licensed under the LGPLv3, see LICENCE file for details.
 
3
 
 
4
package gomaasapi
 
5
 
 
6
import (
 
7
        "encoding/json"
 
8
 
 
9
        jc "github.com/juju/testing/checkers"
 
10
        gc "gopkg.in/check.v1"
 
11
)
 
12
 
 
13
func (suite *GomaasapiTestSuite) TestJoinURLsAppendsPathToBaseURL(c *gc.C) {
 
14
        c.Check(JoinURLs("http://example.com/", "foo"), gc.Equals, "http://example.com/foo")
 
15
}
 
16
 
 
17
func (suite *GomaasapiTestSuite) TestJoinURLsAddsSlashIfNeeded(c *gc.C) {
 
18
        c.Check(JoinURLs("http://example.com/foo", "bar"), gc.Equals, "http://example.com/foo/bar")
 
19
}
 
20
 
 
21
func (suite *GomaasapiTestSuite) TestJoinURLsNormalizesDoubleSlash(c *gc.C) {
 
22
        c.Check(JoinURLs("http://example.com/base/", "/szot"), gc.Equals, "http://example.com/base/szot")
 
23
}
 
24
 
 
25
func (suite *GomaasapiTestSuite) TestEnsureTrailingSlashAppendsSlashIfMissing(c *gc.C) {
 
26
        c.Check(EnsureTrailingSlash("test"), gc.Equals, "test/")
 
27
}
 
28
 
 
29
func (suite *GomaasapiTestSuite) TestEnsureTrailingSlashDoesNotAppendIfPresent(c *gc.C) {
 
30
        c.Check(EnsureTrailingSlash("test/"), gc.Equals, "test/")
 
31
}
 
32
 
 
33
func (suite *GomaasapiTestSuite) TestEnsureTrailingSlashReturnsSlashIfEmpty(c *gc.C) {
 
34
        c.Check(EnsureTrailingSlash(""), gc.Equals, "/")
 
35
}
 
36
 
 
37
func parseJSON(c *gc.C, source string) interface{} {
 
38
        var parsed interface{}
 
39
        err := json.Unmarshal([]byte(source), &parsed)
 
40
        c.Assert(err, jc.ErrorIsNil)
 
41
        return parsed
 
42
}
 
43
 
 
44
func updateJSONMap(c *gc.C, source string, changes map[string]interface{}) string {
 
45
        var parsed map[string]interface{}
 
46
        err := json.Unmarshal([]byte(source), &parsed)
 
47
        c.Assert(err, jc.ErrorIsNil)
 
48
        for key, value := range changes {
 
49
                parsed[key] = value
 
50
        }
 
51
        bytes, err := json.Marshal(parsed)
 
52
        c.Assert(err, jc.ErrorIsNil)
 
53
        return string(bytes)
 
54
}