github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/image/v2/imageimport/testing/requests_test.go (about)

     1  package testing
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"net/http"
     7  	"testing"
     8  
     9  	"github.com/vnpaycloud-console/gophercloud/v2/openstack/image/v2/imageimport"
    10  	th "github.com/vnpaycloud-console/gophercloud/v2/testhelper"
    11  	fakeclient "github.com/vnpaycloud-console/gophercloud/v2/testhelper/client"
    12  )
    13  
    14  func TestGet(t *testing.T) {
    15  	th.SetupHTTP()
    16  	defer th.TeardownHTTP()
    17  
    18  	th.Mux.HandleFunc("/info/import", func(w http.ResponseWriter, r *http.Request) {
    19  		th.TestMethod(t, r, "GET")
    20  		th.TestHeader(t, r, "X-Auth-Token", fakeclient.TokenID)
    21  
    22  		w.Header().Add("Content-Type", "application/json")
    23  		w.WriteHeader(http.StatusOK)
    24  
    25  		fmt.Fprint(w, ImportGetResult)
    26  	})
    27  
    28  	validImportMethods := []string{
    29  		string(imageimport.GlanceDirectMethod),
    30  		string(imageimport.WebDownloadMethod),
    31  	}
    32  
    33  	s, err := imageimport.Get(context.TODO(), fakeclient.ServiceClient()).Extract()
    34  	th.AssertNoErr(t, err)
    35  
    36  	th.AssertEquals(t, s.ImportMethods.Description, "Import methods available.")
    37  	th.AssertEquals(t, s.ImportMethods.Type, "array")
    38  	th.AssertDeepEquals(t, s.ImportMethods.Value, validImportMethods)
    39  }
    40  
    41  func TestCreate(t *testing.T) {
    42  	th.SetupHTTP()
    43  	defer th.TeardownHTTP()
    44  
    45  	th.Mux.HandleFunc("/images/da3b75d9-3f4a-40e7-8a2c-bfab23927dea/import", func(w http.ResponseWriter, r *http.Request) {
    46  		th.TestMethod(t, r, "POST")
    47  		th.TestHeader(t, r, "X-Auth-Token", fakeclient.TokenID)
    48  		th.TestJSONRequest(t, r, ImportCreateRequest)
    49  
    50  		w.Header().Add("Content-Type", "application/json")
    51  		w.WriteHeader(http.StatusAccepted)
    52  		fmt.Fprint(w, `{}`)
    53  	})
    54  
    55  	opts := imageimport.CreateOpts{
    56  		Name: imageimport.WebDownloadMethod,
    57  		URI:  "http://download.cirros-cloud.net/0.4.0/cirros-0.4.0-x86_64-disk.img",
    58  	}
    59  	err := imageimport.Create(context.TODO(), fakeclient.ServiceClient(), "da3b75d9-3f4a-40e7-8a2c-bfab23927dea", opts).ExtractErr()
    60  	th.AssertNoErr(t, err)
    61  }