github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/orchestration/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/vnpaycloud-console/gophercloud/v2/testhelper" 11 ) 12 13 func TestToStringKeys(t *testing.T) { 14 var test1 any = map[any]any{ 15 "Adam": "Smith", 16 "Isaac": "Newton", 17 } 18 result1, err := toStringKeys(test1) 19 th.AssertNoErr(t, err) 20 21 expected := map[string]any{ 22 "Adam": "Smith", 23 "Isaac": "Newton", 24 } 25 th.AssertDeepEquals(t, result1, expected) 26 } 27 28 func TestGetBasePath(t *testing.T) { 29 _, err := getBasePath() 30 th.AssertNoErr(t, err) 31 } 32 33 // test if HTTP client can read file type URLS. Read the URL of this file 34 // because if this test is running, it means this file _must_ exist 35 func TestGetHTTPClient(t *testing.T) { 36 client := getHTTPClient() 37 baseurl, err := getBasePath() 38 th.AssertNoErr(t, err) 39 resp, err := client.Get(baseurl) 40 th.AssertNoErr(t, err) 41 th.AssertEquals(t, resp.StatusCode, 200) 42 } 43 44 // Implement a fakeclient that can be used to mock out HTTP requests 45 type fakeClient struct { 46 BaseClient Client 47 } 48 49 // this client's Get method first changes the URL given to point to 50 // testhelper's (th) endpoints. This is done because the http Mux does not seem 51 // to work for fqdns with the `file` scheme 52 func (c fakeClient) Get(url string) (*http.Response, error) { 53 newurl := strings.Replace(url, "file://", th.Endpoint(), 1) 54 return c.BaseClient.Get(newurl) 55 } 56 57 // test the fetch function 58 func TestFetch(t *testing.T) { 59 th.SetupHTTP() 60 defer th.TeardownHTTP() 61 baseurl, err := getBasePath() 62 th.AssertNoErr(t, err) 63 fakeURL := strings.Join([]string{baseurl, "file.yaml"}, "/") 64 urlparsed, err := url.Parse(fakeURL) 65 th.AssertNoErr(t, err) 66 67 th.Mux.HandleFunc(urlparsed.Path, func(w http.ResponseWriter, r *http.Request) { 68 th.TestMethod(t, r, "GET") 69 w.Header().Set("Content-Type", "application/jason") 70 w.WriteHeader(http.StatusOK) 71 fmt.Fprint(w, "Fee-fi-fo-fum") 72 }) 73 74 client := fakeClient{BaseClient: getHTTPClient()} 75 te := TE{ 76 URL: "file.yaml", 77 client: client, 78 } 79 err = te.Fetch() 80 th.AssertNoErr(t, err) 81 th.AssertEquals(t, fakeURL, te.URL) 82 th.AssertEquals(t, "Fee-fi-fo-fum", string(te.Bin)) 83 }