github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/blockstorage/v1/snapshots/testing/fixtures.go (about)

     1  package testing
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"testing"
     7  
     8  	th "github.com/huaweicloud/golangsdk/testhelper"
     9  	fake "github.com/huaweicloud/golangsdk/testhelper/client"
    10  )
    11  
    12  func MockListResponse(t *testing.T) {
    13  	th.Mux.HandleFunc("/snapshots", func(w http.ResponseWriter, r *http.Request) {
    14  		th.TestMethod(t, r, "GET")
    15  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
    16  
    17  		w.Header().Add("Content-Type", "application/json")
    18  		w.WriteHeader(http.StatusOK)
    19  
    20  		fmt.Fprintf(w, `
    21      {
    22        "snapshots": [
    23          {
    24            "id": "289da7f8-6440-407c-9fb4-7db01ec49164",
    25            "display_name": "snapshot-001",
    26            "volume_id": "521752a6-acf6-4b2d-bc7a-119f9148cd8c",
    27            "display_description": "Daily Backup",
    28            "status": "available",
    29            "size": 30,
    30            "created_at": "2012-02-14T20:53:07"
    31          },
    32          {
    33            "id": "96c3bda7-c82a-4f50-be73-ca7621794835",
    34            "display_name": "snapshot-002",
    35            "volume_id": "76b8950a-8594-4e5b-8dce-0dfa9c696358",
    36            "display_description": "Weekly Backup",
    37            "status": "available",
    38            "size": 25,
    39            "created_at": "2012-02-14T20:53:08"
    40          }
    41        ]
    42      }
    43      `)
    44  	})
    45  }
    46  
    47  func MockGetResponse(t *testing.T) {
    48  	th.Mux.HandleFunc("/snapshots/d32019d3-bc6e-4319-9c1d-6722fc136a22", func(w http.ResponseWriter, r *http.Request) {
    49  		th.TestMethod(t, r, "GET")
    50  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
    51  
    52  		w.Header().Add("Content-Type", "application/json")
    53  		w.WriteHeader(http.StatusOK)
    54  		fmt.Fprintf(w, `
    55  {
    56      "snapshot": {
    57          "id": "d32019d3-bc6e-4319-9c1d-6722fc136a22",
    58          "display_name": "snapshot-001",
    59          "display_description": "Daily backup",
    60          "volume_id": "521752a6-acf6-4b2d-bc7a-119f9148cd8c",
    61          "status": "available",
    62          "size": 30,
    63          "created_at": "2012-02-29T03:50:07"
    64      }
    65  }
    66        `)
    67  	})
    68  }
    69  
    70  func MockCreateResponse(t *testing.T) {
    71  	th.Mux.HandleFunc("/snapshots", func(w http.ResponseWriter, r *http.Request) {
    72  		th.TestMethod(t, r, "POST")
    73  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
    74  		th.TestHeader(t, r, "Content-Type", "application/json")
    75  		th.TestHeader(t, r, "Accept", "application/json")
    76  		th.TestJSONRequest(t, r, `
    77  {
    78      "snapshot": {
    79          "volume_id": "1234",
    80          "display_name": "snapshot-001"
    81      }
    82  }
    83        `)
    84  
    85  		w.Header().Add("Content-Type", "application/json")
    86  		w.WriteHeader(http.StatusCreated)
    87  
    88  		fmt.Fprintf(w, `
    89  {
    90      "snapshot": {
    91          "volume_id": "1234",
    92          "display_name": "snapshot-001",
    93          "id": "d32019d3-bc6e-4319-9c1d-6722fc136a22",
    94          "display_description": "Daily backup",
    95          "volume_id": "1234",
    96          "status": "available",
    97          "size": 30,
    98          "created_at": "2012-02-29T03:50:07"
    99    }
   100  }
   101      `)
   102  	})
   103  }
   104  
   105  func MockUpdateMetadataResponse(t *testing.T) {
   106  	th.Mux.HandleFunc("/snapshots/123/metadata", func(w http.ResponseWriter, r *http.Request) {
   107  		th.TestMethod(t, r, "PUT")
   108  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   109  		th.TestHeader(t, r, "Content-Type", "application/json")
   110  		th.TestJSONRequest(t, r, `
   111      {
   112        "metadata": {
   113          "key": "v1"
   114        }
   115      }
   116      `)
   117  
   118  		fmt.Fprintf(w, `
   119        {
   120          "metadata": {
   121            "key": "v1"
   122          }
   123        }
   124      `)
   125  	})
   126  }
   127  
   128  func MockDeleteResponse(t *testing.T) {
   129  	th.Mux.HandleFunc("/snapshots/d32019d3-bc6e-4319-9c1d-6722fc136a22", func(w http.ResponseWriter, r *http.Request) {
   130  		th.TestMethod(t, r, "DELETE")
   131  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   132  		w.WriteHeader(http.StatusNoContent)
   133  	})
   134  }