github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/evs/v2/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 MockCreateResponse(t *testing.T) {
    13  	th.Mux.HandleFunc("/cloudsnapshots", func(w http.ResponseWriter, r *http.Request) {
    14  		th.TestMethod(t, r, "POST")
    15  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
    16  		th.TestHeader(t, r, "Content-Type", "application/json")
    17  		th.TestHeader(t, r, "Accept", "application/json")
    18  		th.TestJSONRequest(t, r, `
    19  {
    20      "snapshot": {
    21          "volume_id": "1234",
    22          "name": "snapshot-001"
    23      }
    24  }
    25        `)
    26  
    27  		w.Header().Add("Content-Type", "application/json")
    28  		w.WriteHeader(http.StatusAccepted)
    29  
    30  		fmt.Fprintf(w, `
    31  {
    32      "snapshot": {
    33          "volume_id": "1234",
    34          "name": "snapshot-001",
    35          "id": "d32019d3-bc6e-4319-9c1d-6722fc136a22",
    36          "description": "Daily backup",
    37          "status": "available",
    38          "size": 30,
    39  		    "created_at": "2020-03-27T15:35:03.000000"
    40      }
    41  }
    42      `)
    43  	})
    44  }
    45  
    46  func MockGetResponse(t *testing.T) {
    47  	th.Mux.HandleFunc("/cloudsnapshots/d32019d3-bc6e-4319-9c1d-6722fc136a22", func(w http.ResponseWriter, r *http.Request) {
    48  		th.TestMethod(t, r, "GET")
    49  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
    50  
    51  		w.Header().Add("Content-Type", "application/json")
    52  		w.WriteHeader(http.StatusOK)
    53  		fmt.Fprintf(w, `
    54  {
    55      "snapshot": {
    56          "id": "d32019d3-bc6e-4319-9c1d-6722fc136a22",
    57          "name": "snapshot-001",
    58          "description": "Daily backup",
    59          "volume_id": "521752a6-acf6-4b2d-bc7a-119f9148cd8c",
    60          "status": "available",
    61          "size": 30,
    62  		    "created_at": "2020-03-27T15:35:03.000000"
    63      }
    64  }
    65        `)
    66  	})
    67  }
    68  
    69  func MockUpdateResponse(t *testing.T) {
    70  	th.Mux.HandleFunc("/cloudsnapshots/d32019d3-bc6e-4319-9c1d-6722fc136a22", func(w http.ResponseWriter, r *http.Request) {
    71  		th.TestMethod(t, r, "PUT")
    72  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
    73  		th.TestHeader(t, r, "Content-Type", "application/json")
    74  		th.TestJSONRequest(t, r, `
    75  {
    76      "snapshot": {
    77          "name": "snapshot-001-update",
    78          "description": "Weekly backup"
    79      }
    80  }
    81      `)
    82  
    83  		fmt.Fprintf(w, `
    84  {
    85      "snapshot": {
    86          "id": "d32019d3-bc6e-4319-9c1d-6722fc136a22",
    87          "name": "snapshot-001-update",
    88          "description": "Weekly backup",
    89          "volume_id": "521752a6-acf6-4b2d-bc7a-119f9148cd8c",
    90          "status": "available",
    91          "size": 30,
    92          "created_at": "2020-03-27T15:35:03.000000",
    93          "updated_at": "2020-03-27T15:55:03.000000"
    94      }
    95  }
    96      `)
    97  	})
    98  }
    99  
   100  func MockListResponse(t *testing.T) {
   101  	th.Mux.HandleFunc("/cloudsnapshots/detail", func(w http.ResponseWriter, r *http.Request) {
   102  		th.TestMethod(t, r, "GET")
   103  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   104  
   105  		w.Header().Add("Content-Type", "application/json")
   106  		w.WriteHeader(http.StatusOK)
   107  
   108  		fmt.Fprintf(w, `
   109      {
   110        "snapshots": [
   111          {
   112            "id": "289da7f8-6440-407c-9fb4-7db01ec49164",
   113            "name": "snapshot-001",
   114            "volume_id": "521752a6-acf6-4b2d-bc7a-119f9148cd8c",
   115            "description": "Daily Backup",
   116            "status": "available",
   117            "size": 30,
   118  		  "created_at": "2020-03-27T15:35:03.000000"
   119          },
   120          {
   121            "id": "96c3bda7-c82a-4f50-be73-ca7621794835",
   122            "name": "snapshot-002",
   123            "volume_id": "76b8950a-8594-4e5b-8dce-0dfa9c696358",
   124            "description": "Weekly Backup",
   125            "status": "available",
   126            "size": 25,
   127  		  "created_at": "2020-03-27T15:35:03.000000"
   128          }
   129        ]
   130      }
   131      `)
   132  	})
   133  }
   134  
   135  func MockDeleteResponse(t *testing.T) {
   136  	th.Mux.HandleFunc("/cloudsnapshots/d32019d3-bc6e-4319-9c1d-6722fc136a22", func(w http.ResponseWriter, r *http.Request) {
   137  		th.TestMethod(t, r, "DELETE")
   138  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   139  		w.WriteHeader(http.StatusNoContent)
   140  	})
   141  }