github.com/gophercloud/gophercloud@v1.11.0/openstack/messaging/v2/queues/testing/fixtures_test.go (about)

     1  package testing
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"testing"
     7  
     8  	"github.com/gophercloud/gophercloud/openstack/messaging/v2/queues"
     9  	th "github.com/gophercloud/gophercloud/testhelper"
    10  	fake "github.com/gophercloud/gophercloud/testhelper/client"
    11  )
    12  
    13  // QueueName is the name of the queue
    14  var QueueName = "FakeTestQueue"
    15  
    16  // CreateQueueRequest is a sample request to create a queue.
    17  const CreateQueueRequest = `
    18  {
    19      "_max_messages_post_size": 262144,
    20      "_default_message_ttl": 3600,
    21      "_default_message_delay": 30,
    22      "_dead_letter_queue": "dead_letter",
    23      "_dead_letter_queue_messages_ttl": 3600,
    24      "_max_claim_count": 10,
    25      "_enable_encrypt_messages": false,
    26      "description": "Queue for unit testing."
    27  }`
    28  
    29  // CreateShareRequest is a sample request to a share.
    30  const CreateShareRequest = `
    31  {
    32    "paths": ["messages", "claims", "subscriptions"],
    33    "methods": ["GET", "POST", "PUT", "PATCH"],
    34    "expires": "2016-09-01T00:00:00"
    35  }`
    36  
    37  // CreatePurgeRequest is a sample request to a purge.
    38  const CreatePurgeRequest = `
    39  {
    40      "resource_types": ["messages", "subscriptions"]
    41  }`
    42  
    43  // ListQueuesResponse1 is a sample response to a List queues.
    44  const ListQueuesResponse1 = `
    45  {
    46      "queues":[
    47          {
    48              "href":"/v2/queues/london",
    49              "name":"london",
    50              "metadata":{
    51                  "_dead_letter_queue":"fake_queue",
    52                  "_dead_letter_queue_messages_ttl":3500,
    53                  "_default_message_delay":25,
    54                  "_default_message_ttl":3700,
    55                  "_max_claim_count":10,
    56                  "_max_messages_post_size":262143,
    57                  "_enable_encrypt_messages":true,
    58                  "description":"Test queue."
    59              }
    60          }
    61      ],
    62      "links":[
    63          {
    64              "href":"/v2/queues?marker=london",
    65              "rel":"next"
    66          }
    67  	],
    68  	"count": 2
    69  }`
    70  
    71  // ListQueuesResponse2 is a sample response to a List queues.
    72  const ListQueuesResponse2 = `
    73  {
    74      "queues":[
    75  		{
    76              "href":"/v2/queues/beijing",
    77              "name":"beijing",
    78              "metadata":{
    79                  "_dead_letter_queue":"fake_queue",
    80                  "_dead_letter_queue_messages_ttl":3500,
    81                  "_default_message_delay":25,
    82                  "_default_message_ttl":3700,
    83                  "_max_claim_count":10,
    84                  "_max_messages_post_size":262143,
    85                  "description":"Test queue."
    86              }
    87          }
    88      ],
    89      "links":[
    90          {
    91              "href":"/v2/queues?marker=beijing",
    92              "rel":"next"
    93          }
    94  	],
    95  	"count": 2
    96  }`
    97  
    98  // UpdateQueueRequest is a sample request to update a queue.
    99  const UpdateQueueRequest = `
   100  [
   101      {
   102          "op": "replace",
   103          "path": "/metadata/description",
   104          "value": "Update queue description"
   105      }
   106  ]`
   107  
   108  // UpdateQueueResponse is a sample response to a update queue.
   109  const UpdateQueueResponse = `
   110  {
   111  	"description": "Update queue description"
   112  }`
   113  
   114  // GetQueueResponse is a sample response to a get queue.
   115  const GetQueueResponse = `
   116  {
   117  	"_max_messages_post_size": 262144,
   118  	"_default_message_ttl": 3600,
   119  	"description": "Queue used for unit testing."
   120  }`
   121  
   122  // GetStatsResponse is a sample response to a stats request.
   123  const GetStatsResponse = `
   124  {
   125      "messages":{
   126           "claimed": 10,
   127           "total": 20,
   128           "free": 10
   129      }
   130  }`
   131  
   132  // CreateShareResponse is a sample response to a share request.
   133  const CreateShareResponse = `
   134  {
   135      "project": "2887aabf368046a3bb0070f1c0413470",
   136      "paths": [
   137          "/v2/queues/test/messages",
   138          "/v2/queues/test/claims",
   139          "/v2/queues/test/subscriptions"
   140      ],
   141      "expires": "2016-09-01T00:00:00",
   142      "methods": [
   143          "GET",
   144          "PATCH",
   145          "POST",
   146          "PUT"
   147      ],
   148      "signature": "6a63d63242ebd18c3518871dda6fdcb6273db2672c599bf985469241e9a1c799"
   149  }`
   150  
   151  // FirstQueue is the first result in a List.
   152  var FirstQueue = queues.Queue{
   153  	Href: "/v2/queues/london",
   154  	Name: "london",
   155  	Metadata: queues.QueueDetails{
   156  		DeadLetterQueue:           "fake_queue",
   157  		DeadLetterQueueMessageTTL: 3500,
   158  		DefaultMessageDelay:       25,
   159  		DefaultMessageTTL:         3700,
   160  		MaxClaimCount:             10,
   161  		MaxMessagesPostSize:       262143,
   162  		EnableEncryptMessages:     true,
   163  		Extra:                     map[string]interface{}{"description": "Test queue."},
   164  	},
   165  }
   166  
   167  // SecondQueue is the second result in a List.
   168  var SecondQueue = queues.Queue{
   169  	Href: "/v2/queues/beijing",
   170  	Name: "beijing",
   171  	Metadata: queues.QueueDetails{
   172  		DeadLetterQueue:           "fake_queue",
   173  		DeadLetterQueueMessageTTL: 3500,
   174  		DefaultMessageDelay:       25,
   175  		DefaultMessageTTL:         3700,
   176  		MaxClaimCount:             10,
   177  		MaxMessagesPostSize:       262143,
   178  		Extra:                     map[string]interface{}{"description": "Test queue."},
   179  	},
   180  }
   181  
   182  // ExpectedQueueSlice is the expected result in a List.
   183  var ExpectedQueueSlice = [][]queues.Queue{{FirstQueue}, {SecondQueue}}
   184  
   185  // QueueDetails is the expected result in a Get.
   186  var QueueDetails = queues.QueueDetails{
   187  	DefaultMessageTTL:   3600,
   188  	MaxMessagesPostSize: 262144,
   189  	Extra:               map[string]interface{}{"description": "Queue used for unit testing."},
   190  }
   191  
   192  // ExpectedStats is the expected result in a GetStats.
   193  var ExpectedStats = queues.Stats{
   194  	Claimed: 10,
   195  	Total:   20,
   196  	Free:    10,
   197  }
   198  
   199  // ExpectedShare is the expected result in Share.
   200  var ExpectedShare = queues.QueueShare{
   201  	Project: "2887aabf368046a3bb0070f1c0413470",
   202  	Paths: []string{
   203  		"/v2/queues/test/messages",
   204  		"/v2/queues/test/claims",
   205  		"/v2/queues/test/subscriptions",
   206  	},
   207  	Expires: "2016-09-01T00:00:00",
   208  	Methods: []string{
   209  		"GET",
   210  		"PATCH",
   211  		"POST",
   212  		"PUT",
   213  	},
   214  	Signature: "6a63d63242ebd18c3518871dda6fdcb6273db2672c599bf985469241e9a1c799",
   215  }
   216  
   217  // HandleListSuccessfully configures the test server to respond to a List request.
   218  func HandleListSuccessfully(t *testing.T) {
   219  	th.Mux.HandleFunc("/v2/queues",
   220  		func(w http.ResponseWriter, r *http.Request) {
   221  			th.TestMethod(t, r, "GET")
   222  			th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   223  
   224  			w.Header().Add("Content-Type", "application/json")
   225  			next := r.RequestURI
   226  
   227  			switch next {
   228  			case "/v2/queues?limit=1&with_count=true":
   229  				fmt.Fprintf(w, ListQueuesResponse1)
   230  			case "/v2/queues?marker=london":
   231  				fmt.Fprint(w, ListQueuesResponse2)
   232  			case "/v2/queues?marker=beijing":
   233  				fmt.Fprint(w, `{ "queues": [] }`)
   234  			}
   235  		})
   236  }
   237  
   238  // HandleCreateSuccessfully configures the test server to respond to a Create request.
   239  func HandleCreateSuccessfully(t *testing.T) {
   240  	th.Mux.HandleFunc(fmt.Sprintf("/v2/queues/%s", QueueName),
   241  		func(w http.ResponseWriter, r *http.Request) {
   242  			th.TestMethod(t, r, "PUT")
   243  			th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   244  			th.TestJSONRequest(t, r, CreateQueueRequest)
   245  
   246  			w.WriteHeader(http.StatusNoContent)
   247  		})
   248  }
   249  
   250  // HandleUpdateSuccessfully configures the test server to respond to an Update request.
   251  func HandleUpdateSuccessfully(t *testing.T) {
   252  	th.Mux.HandleFunc(fmt.Sprintf("/v2/queues/%s", QueueName),
   253  		func(w http.ResponseWriter, r *http.Request) {
   254  			th.TestMethod(t, r, "PATCH")
   255  			th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   256  			th.TestJSONRequest(t, r, UpdateQueueRequest)
   257  
   258  			w.Header().Add("Content-Type", "application/json")
   259  			fmt.Fprintf(w, UpdateQueueResponse)
   260  		})
   261  }
   262  
   263  // HandleGetSuccessfully configures the test server to respond to a Get request.
   264  func HandleGetSuccessfully(t *testing.T) {
   265  	th.Mux.HandleFunc(fmt.Sprintf("/v2/queues/%s", QueueName),
   266  		func(w http.ResponseWriter, r *http.Request) {
   267  			th.TestMethod(t, r, "GET")
   268  			th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   269  
   270  			w.Header().Add("Content-Type", "application/json")
   271  			fmt.Fprintf(w, GetQueueResponse)
   272  		})
   273  }
   274  
   275  // HandleDeleteSuccessfully configures the test server to respond to a Delete request.
   276  func HandleDeleteSuccessfully(t *testing.T) {
   277  	th.Mux.HandleFunc(fmt.Sprintf("/v2/queues/%s", QueueName),
   278  		func(w http.ResponseWriter, r *http.Request) {
   279  			th.TestMethod(t, r, "DELETE")
   280  			th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   281  			w.WriteHeader(http.StatusNoContent)
   282  		})
   283  }
   284  
   285  // HandleGetSuccessfully configures the test server to respond to a Get request.
   286  func HandleGetStatsSuccessfully(t *testing.T) {
   287  	th.Mux.HandleFunc(fmt.Sprintf("/v2/queues/%s/stats", QueueName),
   288  		func(w http.ResponseWriter, r *http.Request) {
   289  			th.TestMethod(t, r, "GET")
   290  			th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   291  
   292  			w.Header().Add("Content-Type", "application/json")
   293  			fmt.Fprintf(w, GetStatsResponse)
   294  		})
   295  }
   296  
   297  // HandleShareSuccessfully configures the test server to respond to a Share request.
   298  func HandleShareSuccessfully(t *testing.T) {
   299  	th.Mux.HandleFunc(fmt.Sprintf("/v2/queues/%s/share", QueueName),
   300  		func(w http.ResponseWriter, r *http.Request) {
   301  			th.TestMethod(t, r, "POST")
   302  			th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   303  			th.TestJSONRequest(t, r, CreateShareRequest)
   304  
   305  			w.Header().Add("Content-Type", "application/json")
   306  			fmt.Fprintf(w, CreateShareResponse)
   307  		})
   308  }
   309  
   310  // HandlePurgeSuccessfully configures the test server to respond to a Purge request.
   311  func HandlePurgeSuccessfully(t *testing.T) {
   312  	th.Mux.HandleFunc(fmt.Sprintf("/v2/queues/%s/purge", QueueName),
   313  		func(w http.ResponseWriter, r *http.Request) {
   314  			th.TestMethod(t, r, "POST")
   315  			th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   316  			th.TestJSONRequest(t, r, CreatePurgeRequest)
   317  
   318  			w.WriteHeader(http.StatusNoContent)
   319  		})
   320  }