github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/rts/v1/stacks/utils_test.go (about) 1 package stacks 2 3 import ( 4 "fmt" 5 "net/http" 6 "net/url" 7 "strings" 8 "testing" 9 10 th "github.com/huaweicloud/golangsdk/testhelper" 11 ) 12 13 func TestTEFixFileRefs(t *testing.T) { 14 te := TE{ 15 Bin: []byte(`string_to_replace: my fair lady`), 16 fileMaps: map[string]string{ 17 "string_to_replace": "london bridge is falling down", 18 }, 19 } 20 te.fixFileRefs() 21 th.AssertEquals(t, string(te.Bin), `london bridge is falling down: my fair lady`) 22 } 23 24 func TestToStringKeys(t *testing.T) { 25 var test1 interface{} = map[interface{}]interface{}{ 26 "Adam": "Smith", 27 "Isaac": "Newton", 28 } 29 result1, err := toStringKeys(test1) 30 th.AssertNoErr(t, err) 31 32 expected := map[string]interface{}{ 33 "Adam": "Smith", 34 "Isaac": "Newton", 35 } 36 th.AssertDeepEquals(t, result1, expected) 37 } 38 39 func TestGetBasePath(t *testing.T) { 40 _, err := getBasePath() 41 th.AssertNoErr(t, err) 42 } 43 44 // test if HTTP client can read file type URLS. Read the URL of this file 45 // because if this test is running, it means this file _must_ exist 46 func TestGetHTTPClient(t *testing.T) { 47 client := getHTTPClient() 48 baseurl, err := getBasePath() 49 th.AssertNoErr(t, err) 50 resp, err := client.Get(baseurl) 51 th.AssertNoErr(t, err) 52 th.AssertEquals(t, resp.StatusCode, 200) 53 } 54 55 // Implement a fakeclient that can be used to mock out HTTP requests 56 type fakeClient struct { 57 BaseClient Client 58 } 59 60 // this client's Get method first changes the URL given to point to 61 // testhelper's (th) endpoints. This is done because the http Mux does not seem 62 // to work for fqdns with the `file` scheme 63 func (c fakeClient) Get(url string) (*http.Response, error) { 64 newurl := strings.Replace(url, "file://", th.Endpoint(), 1) 65 return c.BaseClient.Get(newurl) 66 } 67 68 // test the fetch function 69 func TestFetch(t *testing.T) { 70 th.SetupHTTP() 71 defer th.TeardownHTTP() 72 baseurl, err := getBasePath() 73 th.AssertNoErr(t, err) 74 fakeURL := strings.Join([]string{baseurl, "file.yaml"}, "/") 75 urlparsed, err := url.Parse(fakeURL) 76 th.AssertNoErr(t, err) 77 78 th.Mux.HandleFunc(urlparsed.Path, func(w http.ResponseWriter, r *http.Request) { 79 th.TestMethod(t, r, "GET") 80 w.Header().Set("Content-Type", "application/jason") 81 w.WriteHeader(http.StatusOK) 82 fmt.Fprintf(w, "Fee-fi-fo-fum") 83 }) 84 85 client := fakeClient{BaseClient: getHTTPClient()} 86 te := TE{ 87 URL: "file.yaml", 88 client: client, 89 } 90 err = te.Fetch() 91 th.AssertNoErr(t, err) 92 th.AssertEquals(t, fakeURL, te.URL) 93 th.AssertEquals(t, "Fee-fi-fo-fum", string(te.Bin)) 94 }