blob: 92b9c083b7b672869288fd5e4e032efcbbe53462 [file] [log] [blame]
Jamie Hannaford302c0b62015-02-16 14:12:34 +01001package backups
2
3import (
4 "fmt"
5 "net/http"
6 "testing"
7
8 th "github.com/rackspace/gophercloud/testhelper"
9 "github.com/rackspace/gophercloud/testhelper/client"
10)
11
12func SetupHandler(t *testing.T, url, method, requestBody, responseBody string, status int) {
13 th.Mux.HandleFunc(url, func(w http.ResponseWriter, r *http.Request) {
14 th.TestMethod(t, r, method)
15 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
16
17 if requestBody != "" {
18 th.TestJSONRequest(t, r, requestBody)
19 }
20
21 if responseBody != "" {
22 w.Header().Add("Content-Type", "application/json")
23 }
24
25 w.WriteHeader(status)
26
27 if responseBody != "" {
28 fmt.Fprintf(w, responseBody)
29 }
30 })
31}
32
33func HandleCreateSuccessfully(t *testing.T) {
34 requestJSON := `
35{
36 "backup": {
37 "description": "My Backup",
38 "instance": "d4603f69-ec7e-4e9b-803f-600b9205576f",
39 "name": "snapshot"
40 }
41}
42`
43
44 responseJSON := `
45{
46 "backup": {
47 "created": "2014-02-13T21:47:16",
48 "description": "My Backup",
49 "id": "61f12fef-edb1-4561-8122-e7c00ef26a82",
50 "instance_id": "d4603f69-ec7e-4e9b-803f-600b9205576f",
51 "locationRef": null,
52 "name": "snapshot",
53 "parent_id": null,
54 "size": 100,
55 "status": "NEW",
56 "updated": "2014-02-13T21:47:16"
57 }
58}
59`
60
61 SetupHandler(t, "/backups", "POST", requestJSON, responseJSON, 202)
62}
63
64func HandleListSuccessfully(t *testing.T) {
65 responseJSON := `
66{
67 "backups": [
68 {
69 "status": "COMPLETED",
70 "updated": "2014-06-18T21:24:39",
71 "description": "Backup from Restored Instance",
72
73 "id": "87972694-4be2-40f5-83f8-501656e0032a",
74 "size": 0.141026,
75 "name": "restored_backup",
76 "created": "2014-06-18T21:23:35",
77 "instance_id": "29af2cd9-0674-48ab-b87a-b160f00208e6",
78 "parent_id": null,
79 "locationRef": "http://localhost/path/to/backup"
80 }
81 ]
82}
83`
84
85 SetupHandler(t, "/backups", "GET", "", responseJSON, 200)
86}