File: utils_test.go

package info (click to toggle)
golang-github-rackspace-gophercloud 1.0.0%2Bgit20161013.1012.e00690e8-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 5,148 kB
  • ctags: 6,414
  • sloc: sh: 16; makefile: 6
file content (94 lines) | stat: -rw-r--r-- 2,432 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package stacks

import (
	"fmt"
	"net/http"
	"net/url"
	"strings"
	"testing"

	th "github.com/rackspace/gophercloud/testhelper"
)

func TestTEFixFileRefs(t *testing.T) {
	te := TE{
		Bin: []byte(`string_to_replace: my fair lady`),
		fileMaps: map[string]string{
			"string_to_replace": "london bridge is falling down",
		},
	}
	te.fixFileRefs()
	th.AssertEquals(t, string(te.Bin), `london bridge is falling down: my fair lady`)
}

func TesttoStringKeys(t *testing.T) {
	var test1 interface{} = map[interface{}]interface{}{
		"Adam":  "Smith",
		"Isaac": "Newton",
	}
	result1, err := toStringKeys(test1)
	th.AssertNoErr(t, err)

	expected := map[string]interface{}{
		"Adam":  "Smith",
		"Isaac": "Newton",
	}
	th.AssertDeepEquals(t, result1, expected)
}

func TestGetBasePath(t *testing.T) {
	_, err := getBasePath()
	th.AssertNoErr(t, err)
}

// test if HTTP client can read file type URLS. Read the URL of this file
// because if this test is running, it means this file _must_ exist
func TestGetHTTPClient(t *testing.T) {
	client := getHTTPClient()
	baseurl, err := getBasePath()
	th.AssertNoErr(t, err)
	resp, err := client.Get(baseurl)
	th.AssertNoErr(t, err)
	th.AssertEquals(t, resp.StatusCode, 200)
}

// Implement a fakeclient that can be used to mock out HTTP requests
type fakeClient struct {
	BaseClient Client
}

// this client's Get method first changes the URL given to point to
// testhelper's (th) endpoints. This is done because the http Mux does not seem
// to work for fqdns with the `file` scheme
func (c fakeClient) Get(url string) (*http.Response, error) {
	newurl := strings.Replace(url, "file://", th.Endpoint(), 1)
	return c.BaseClient.Get(newurl)
}

// test the fetch function
func TestFetch(t *testing.T) {
	th.SetupHTTP()
	defer th.TeardownHTTP()
	baseurl, err := getBasePath()
	th.AssertNoErr(t, err)
	fakeURL := strings.Join([]string{baseurl, "file.yaml"}, "/")
	urlparsed, err := url.Parse(fakeURL)
	th.AssertNoErr(t, err)

	th.Mux.HandleFunc(urlparsed.Path, func(w http.ResponseWriter, r *http.Request) {
		th.TestMethod(t, r, "GET")
		w.Header().Set("Content-Type", "application/jason")
		w.WriteHeader(http.StatusOK)
		fmt.Fprintf(w, "Fee-fi-fo-fum")
	})

	client := fakeClient{BaseClient: getHTTPClient()}
	te := TE{
		URL:    "file.yaml",
		client: client,
	}
	err = te.Fetch()
	th.AssertNoErr(t, err)
	th.AssertEquals(t, fakeURL, te.URL)
	th.AssertEquals(t, "Fee-fi-fo-fum", string(te.Bin))
}