github.com/gophercloud/gophercloud@v1.11.0/openstack/identity/v3/extensions/projectendpoints/testing/requests_test.go (about)

     1  package testing
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"testing"
     7  
     8  	"github.com/gophercloud/gophercloud"
     9  	"github.com/gophercloud/gophercloud/openstack/identity/v3/extensions/projectendpoints"
    10  	"github.com/gophercloud/gophercloud/pagination"
    11  	th "github.com/gophercloud/gophercloud/testhelper"
    12  	"github.com/gophercloud/gophercloud/testhelper/client"
    13  )
    14  
    15  func TestCreateSuccessful(t *testing.T) {
    16  	th.SetupHTTP()
    17  	defer th.TeardownHTTP()
    18  
    19  	th.Mux.HandleFunc("/OS-EP-FILTER/projects/project-id/endpoints/endpoint-id", func(w http.ResponseWriter, r *http.Request) {
    20  		th.TestMethod(t, r, "PUT")
    21  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
    22  
    23  		w.WriteHeader(http.StatusNoContent)
    24  	})
    25  
    26  	err := projectendpoints.Create(client.ServiceClient(), "project-id", "endpoint-id").Err
    27  	th.AssertNoErr(t, err)
    28  }
    29  
    30  func TestListEndpoints(t *testing.T) {
    31  	th.SetupHTTP()
    32  	defer th.TeardownHTTP()
    33  
    34  	th.Mux.HandleFunc("/OS-EP-FILTER/projects/project-id/endpoints", func(w http.ResponseWriter, r *http.Request) {
    35  		th.TestMethod(t, r, "GET")
    36  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
    37  
    38  		w.Header().Add("Content-Type", "application/json")
    39  		fmt.Fprintf(w, `
    40  		{
    41  			"endpoints": [
    42  				{
    43  					"id": "6fedc0",
    44  					"interface": "public",
    45  					"url": "http://example.com/identity/",
    46  					"region": "north",
    47  					"links": {
    48  						"self": "http://example.com/identity/v3/endpoints/6fedc0"
    49  					},
    50  					"service_id": "1b501a"
    51  				},
    52  				{
    53  					"id": "6fedc0",
    54  					"interface": "internal",
    55  					"region": "south",
    56  					"url": "http://example.com/identity/",
    57  					"links": {
    58  						"self": "http://example.com/identity/v3/endpoints/6fedc0"
    59  					},
    60  					"service_id": "1b501a"
    61  				}
    62  			],
    63  			"links": {
    64  				"self": "http://example.com/identity/v3/OS-EP-FILTER/projects/263fd9/endpoints",
    65  				"previous": null,
    66  				"next": null
    67  			}
    68  		}
    69  		`)
    70  	})
    71  
    72  	count := 0
    73  	projectendpoints.List(client.ServiceClient(), "project-id").EachPage(func(page pagination.Page) (bool, error) {
    74  		count++
    75  		actual, err := projectendpoints.ExtractEndpoints(page)
    76  		if err != nil {
    77  			t.Errorf("Failed to extract endpoints: %v", err)
    78  			return false, err
    79  		}
    80  
    81  		expected := []projectendpoints.Endpoint{
    82  			{
    83  				ID:           "6fedc0",
    84  				Availability: gophercloud.AvailabilityPublic,
    85  				Region:       "north",
    86  				ServiceID:    "1b501a",
    87  				URL:          "http://example.com/identity/",
    88  			},
    89  			{
    90  				ID:           "6fedc0",
    91  				Availability: gophercloud.AvailabilityInternal,
    92  				Region:       "south",
    93  				ServiceID:    "1b501a",
    94  				URL:          "http://example.com/identity/",
    95  			},
    96  		}
    97  		th.AssertDeepEquals(t, expected, actual)
    98  		return true, nil
    99  	})
   100  	th.AssertEquals(t, 1, count)
   101  }
   102  
   103  func TestDeleteEndpoint(t *testing.T) {
   104  	th.SetupHTTP()
   105  	defer th.TeardownHTTP()
   106  
   107  	th.Mux.HandleFunc("/OS-EP-FILTER/projects/project-id/endpoints/endpoint-id", func(w http.ResponseWriter, r *http.Request) {
   108  		th.TestMethod(t, r, "DELETE")
   109  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   110  
   111  		w.WriteHeader(http.StatusNoContent)
   112  	})
   113  
   114  	res := projectendpoints.Delete(client.ServiceClient(), "project-id", "endpoint-id")
   115  	th.AssertNoErr(t, res.Err)
   116  }