github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/common/extensions/testing/fixtures.go (about)

     1  package testing
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"testing"
     7  
     8  	"github.com/huaweicloud/golangsdk/openstack/common/extensions"
     9  	th "github.com/huaweicloud/golangsdk/testhelper"
    10  	"github.com/huaweicloud/golangsdk/testhelper/client"
    11  )
    12  
    13  // ListOutput provides a single page of Extension results.
    14  const ListOutput = `
    15  {
    16  	"extensions": [
    17  		{
    18  			"updated": "2013-01-20T00:00:00-00:00",
    19  			"name": "Neutron Service Type Management",
    20  			"links": [],
    21  			"namespace": "http://docs.openstack.org/ext/neutron/service-type/api/v1.0",
    22  			"alias": "service-type",
    23  			"description": "API for retrieving service providers for Neutron advanced services"
    24  		}
    25  	]
    26  }`
    27  
    28  // GetOutput provides a single Extension result.
    29  const GetOutput = `
    30  {
    31  	"extension": {
    32  		"updated": "2013-02-03T10:00:00-00:00",
    33  		"name": "agent",
    34  		"links": [],
    35  		"namespace": "http://docs.openstack.org/ext/agent/api/v2.0",
    36  		"alias": "agent",
    37  		"description": "The agent management extension."
    38  	}
    39  }
    40  `
    41  
    42  // ListedExtension is the Extension that should be parsed from ListOutput.
    43  var ListedExtension = extensions.Extension{
    44  	Updated:     "2013-01-20T00:00:00-00:00",
    45  	Name:        "Neutron Service Type Management",
    46  	Links:       []interface{}{},
    47  	Namespace:   "http://docs.openstack.org/ext/neutron/service-type/api/v1.0",
    48  	Alias:       "service-type",
    49  	Description: "API for retrieving service providers for Neutron advanced services",
    50  }
    51  
    52  // ExpectedExtensions is a slice containing the Extension that should be parsed from ListOutput.
    53  var ExpectedExtensions = []extensions.Extension{ListedExtension}
    54  
    55  // SingleExtension is the Extension that should be parsed from GetOutput.
    56  var SingleExtension = &extensions.Extension{
    57  	Updated:     "2013-02-03T10:00:00-00:00",
    58  	Name:        "agent",
    59  	Links:       []interface{}{},
    60  	Namespace:   "http://docs.openstack.org/ext/agent/api/v2.0",
    61  	Alias:       "agent",
    62  	Description: "The agent management extension.",
    63  }
    64  
    65  // HandleListExtensionsSuccessfully creates an HTTP handler at `/extensions` on the test handler
    66  // mux that response with a list containing a single tenant.
    67  func HandleListExtensionsSuccessfully(t *testing.T) {
    68  	th.Mux.HandleFunc("/extensions", func(w http.ResponseWriter, r *http.Request) {
    69  		th.TestMethod(t, r, "GET")
    70  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
    71  
    72  		w.Header().Add("Content-Type", "application/json")
    73  
    74  		fmt.Fprintf(w, ListOutput)
    75  	})
    76  }
    77  
    78  // HandleGetExtensionSuccessfully creates an HTTP handler at `/extensions/agent` that responds with
    79  // a JSON payload corresponding to SingleExtension.
    80  func HandleGetExtensionSuccessfully(t *testing.T) {
    81  	th.Mux.HandleFunc("/extensions/agent", func(w http.ResponseWriter, r *http.Request) {
    82  		th.TestMethod(t, r, "GET")
    83  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
    84  
    85  		w.Header().Add("Content-Type", "application/json")
    86  		w.WriteHeader(http.StatusOK)
    87  
    88  		fmt.Fprintf(w, GetOutput)
    89  	})
    90  }