blob: 21be6f90a0c27153321a392689a74b9b6d9951b2 [file] [log] [blame]
jrperritt3d966162016-06-06 14:08:54 -05001package testing
Jamie Hannaford96c666d2014-10-20 16:09:10 +02002
3import (
4 "fmt"
5 "net/http"
6 "testing"
7
Jon Perritt27249f42016-02-18 10:35:59 -06008 th "github.com/gophercloud/gophercloud/testhelper"
9 fake "github.com/gophercloud/gophercloud/testhelper/client"
Jamie Hannaford96c666d2014-10-20 16:09:10 +020010)
11
12func 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",
Joe Topjian614b51d2016-08-03 12:19:30 -060025 "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"
Jamie Hannaford96c666d2014-10-20 16:09:10 +020031 },
32 {
33 "id": "96c3bda7-c82a-4f50-be73-ca7621794835",
Joe Topjian614b51d2016-08-03 12:19:30 -060034 "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"
Jamie Hannaford96c666d2014-10-20 16:09:10 +020040 }
41 ]
42 }
43 `)
44 })
45}
46
47func 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": {
Joe Topjian614b51d2016-08-03 12:19:30 -060057 "id": "d32019d3-bc6e-4319-9c1d-6722fc136a22",
Jamie Hannaford96c666d2014-10-20 16:09:10 +020058 "display_name": "snapshot-001",
Joe Topjian614b51d2016-08-03 12:19:30 -060059 "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"
Jamie Hannaford96c666d2014-10-20 16:09:10 +020064 }
65}
66 `)
67 })
68}
69
70func 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",
Joe Topjian614b51d2016-08-03 12:19:30 -060093 "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 }
Jamie Hannaford96c666d2014-10-20 16:09:10 +0200100}
101 `)
102 })
103}
104
105func 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
128func 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}