blob: 421cbf4487402d0b73caffafcf88ce4f5edfffa1 [file] [log] [blame]
Sreekanth Pothanis07400f32015-09-08 00:26:14 -07001package testing
Jamie Hannaford449c4ac2014-10-21 09:58:02 +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 Hannaford449c4ac2014-10-21 09:58:02 +020010)
11
12func MockListResponse(t *testing.T) {
13 th.Mux.HandleFunc("/volumes", 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 "volumes": [
23 {
24 "id": "289da7f8-6440-407c-9fb4-7db01ec49164",
25 "display_name": "vol-001"
26 },
27 {
28 "id": "96c3bda7-c82a-4f50-be73-ca7621794835",
29 "display_name": "vol-002"
30 }
31 ]
32 }
33 `)
34 })
35}
36
37func MockGetResponse(t *testing.T) {
38 th.Mux.HandleFunc("/volumes/d32019d3-bc6e-4319-9c1d-6722fc136a22", func(w http.ResponseWriter, r *http.Request) {
39 th.TestMethod(t, r, "GET")
40 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
41
42 w.Header().Add("Content-Type", "application/json")
43 w.WriteHeader(http.StatusOK)
44 fmt.Fprintf(w, `
Jon Perritt12395212016-02-24 10:41:17 -060045 {
46 "volume": {
47 "id": "521752a6-acf6-4b2d-bc7a-119f9148cd8c",
48 "display_name": "vol-001",
49 "display_description": "Another volume.",
50 "status": "active",
51 "size": 30,
52 "volume_type": "289da7f8-6440-407c-9fb4-7db01ec49164",
53 "metadata": {
54 "contents": "junk"
55 },
56 "availability_zone": "us-east1",
57 "bootable": "false",
58 "snapshot_id": null,
59 "attachments": [
60 {
61 "attachment_id": "03987cd1-0ad5-40d1-9b2a-7cc48295d4fa",
62 "id": "47e9ecc5-4045-4ee3-9a4b-d859d546a0cf",
63 "volume_id": "6c80f8ac-e3e2-480c-8e6e-f1db92fe4bfe",
64 "server_id": "d1c4788b-9435-42e2-9b81-29f3be1cd01f",
65 "host_name": "mitaka",
66 "device": "/"
67 }
68 ],
69 "created_at": "2012-02-14T20:53:07Z"
70 }
71 }
Jamie Hannaford449c4ac2014-10-21 09:58:02 +020072 `)
73 })
74}
75
76func MockCreateResponse(t *testing.T) {
77 th.Mux.HandleFunc("/volumes", func(w http.ResponseWriter, r *http.Request) {
78 th.TestMethod(t, r, "POST")
79 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
80 th.TestHeader(t, r, "Content-Type", "application/json")
81 th.TestHeader(t, r, "Accept", "application/json")
82 th.TestJSONRequest(t, r, `
83{
84 "volume": {
Jamie Hannaford59f22072014-10-23 17:00:59 +020085 "size": 75
Jamie Hannaford449c4ac2014-10-21 09:58:02 +020086 }
87}
88 `)
89
90 w.Header().Add("Content-Type", "application/json")
91 w.WriteHeader(http.StatusCreated)
92
93 fmt.Fprintf(w, `
94{
95 "volume": {
96 "size": 4,
97 "id": "d32019d3-bc6e-4319-9c1d-6722fc136a22"
98 }
99}
100 `)
101 })
102}
103
104func MockDeleteResponse(t *testing.T) {
105 th.Mux.HandleFunc("/volumes/d32019d3-bc6e-4319-9c1d-6722fc136a22", func(w http.ResponseWriter, r *http.Request) {
106 th.TestMethod(t, r, "DELETE")
107 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
108 w.WriteHeader(http.StatusNoContent)
109 })
110}
111
112func MockUpdateResponse(t *testing.T) {
113 th.Mux.HandleFunc("/volumes/d32019d3-bc6e-4319-9c1d-6722fc136a22", func(w http.ResponseWriter, r *http.Request) {
114 th.TestMethod(t, r, "PUT")
115 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
116 w.WriteHeader(http.StatusOK)
117 fmt.Fprintf(w, `
118 {
119 "volume": {
120 "display_name": "vol-002",
121 "id": "d32019d3-bc6e-4319-9c1d-6722fc136a22"
122 }
123 }
124 `)
125 })
126}