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