github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/imageservice/v2/members/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 fakeclient "github.com/huaweicloud/golangsdk/testhelper/client" 10 ) 11 12 // HandleCreateImageMemberSuccessfully setup 13 func HandleCreateImageMemberSuccessfully(t *testing.T) { 14 th.Mux.HandleFunc("/images/da3b75d9-3f4a-40e7-8a2c-bfab23927dea/members", func(w http.ResponseWriter, r *http.Request) { 15 th.TestMethod(t, r, "POST") 16 th.TestHeader(t, r, "X-Auth-Token", fakeclient.TokenID) 17 18 th.TestJSONRequest(t, r, `{"member": "8989447062e04a818baf9e073fd04fa7"}`) 19 20 w.WriteHeader(http.StatusOK) 21 fmt.Fprintf(w, `{ 22 "created_at": "2013-09-20T19:22:19Z", 23 "image_id": "da3b75d9-3f4a-40e7-8a2c-bfab23927dea", 24 "member_id": "8989447062e04a818baf9e073fd04fa7", 25 "schema": "/v2/schemas/member", 26 "status": "pending", 27 "updated_at": "2013-09-20T19:25:31Z" 28 }`) 29 30 }) 31 } 32 33 // HandleImageMemberList happy path setup 34 func HandleImageMemberList(t *testing.T) { 35 th.Mux.HandleFunc("/images/da3b75d9-3f4a-40e7-8a2c-bfab23927dea/members", func(w http.ResponseWriter, r *http.Request) { 36 th.TestMethod(t, r, "GET") 37 th.TestHeader(t, r, "X-Auth-Token", fakeclient.TokenID) 38 39 w.Header().Add("Content-Type", "application/json") 40 fmt.Fprintf(w, `{ 41 "members": [ 42 { 43 "created_at": "2013-10-07T17:58:03Z", 44 "image_id": "da3b75d9-3f4a-40e7-8a2c-bfab23927dea", 45 "member_id": "123456789", 46 "schema": "/v2/schemas/member", 47 "status": "pending", 48 "updated_at": "2013-10-07T17:58:03Z" 49 }, 50 { 51 "created_at": "2013-10-07T17:58:55Z", 52 "image_id": "da3b75d9-3f4a-40e7-8a2c-bfab23927dea", 53 "member_id": "987654321", 54 "schema": "/v2/schemas/member", 55 "status": "accepted", 56 "updated_at": "2013-10-08T12:08:55Z" 57 } 58 ], 59 "schema": "/v2/schemas/members" 60 }`) 61 }) 62 } 63 64 // HandleImageMemberEmptyList happy path setup 65 func HandleImageMemberEmptyList(t *testing.T) { 66 th.Mux.HandleFunc("/images/da3b75d9-3f4a-40e7-8a2c-bfab23927dea/members", func(w http.ResponseWriter, r *http.Request) { 67 th.TestMethod(t, r, "GET") 68 th.TestHeader(t, r, "X-Auth-Token", fakeclient.TokenID) 69 70 w.Header().Add("Content-Type", "application/json") 71 fmt.Fprintf(w, `{ 72 "members": [], 73 "schema": "/v2/schemas/members" 74 }`) 75 }) 76 } 77 78 // HandleImageMemberDetails setup 79 func HandleImageMemberDetails(t *testing.T) { 80 th.Mux.HandleFunc("/images/da3b75d9-3f4a-40e7-8a2c-bfab23927dea/members/8989447062e04a818baf9e073fd04fa7", func(w http.ResponseWriter, r *http.Request) { 81 th.TestMethod(t, r, "GET") 82 th.TestHeader(t, r, "X-Auth-Token", fakeclient.TokenID) 83 84 w.WriteHeader(http.StatusOK) 85 fmt.Fprintf(w, `{ 86 "status": "pending", 87 "created_at": "2013-11-26T07:21:21Z", 88 "updated_at": "2013-11-26T07:21:21Z", 89 "image_id": "da3b75d9-3f4a-40e7-8a2c-bfab23927dea", 90 "member_id": "8989447062e04a818baf9e073fd04fa7", 91 "schema": "/v2/schemas/member" 92 }`) 93 }) 94 } 95 96 // HandleImageMemberDeleteSuccessfully setup 97 func HandleImageMemberDeleteSuccessfully(t *testing.T) *CallsCounter { 98 var counter CallsCounter 99 th.Mux.HandleFunc("/images/da3b75d9-3f4a-40e7-8a2c-bfab23927dea/members/8989447062e04a818baf9e073fd04fa7", func(w http.ResponseWriter, r *http.Request) { 100 counter.Counter = counter.Counter + 1 101 102 th.TestMethod(t, r, "DELETE") 103 th.TestHeader(t, r, "X-Auth-Token", fakeclient.TokenID) 104 105 w.WriteHeader(http.StatusNoContent) 106 }) 107 return &counter 108 } 109 110 // HandleImageMemberUpdate setup 111 func HandleImageMemberUpdate(t *testing.T) *CallsCounter { 112 var counter CallsCounter 113 th.Mux.HandleFunc("/images/da3b75d9-3f4a-40e7-8a2c-bfab23927dea/members/8989447062e04a818baf9e073fd04fa7", func(w http.ResponseWriter, r *http.Request) { 114 counter.Counter = counter.Counter + 1 115 116 th.TestMethod(t, r, "PUT") 117 th.TestHeader(t, r, "X-Auth-Token", fakeclient.TokenID) 118 119 th.TestJSONRequest(t, r, `{"status": "accepted"}`) 120 121 w.WriteHeader(http.StatusOK) 122 123 fmt.Fprintf(w, `{ 124 "status": "accepted", 125 "created_at": "2013-11-26T07:21:21Z", 126 "updated_at": "2013-11-26T07:21:21Z", 127 "image_id": "da3b75d9-3f4a-40e7-8a2c-bfab23927dea", 128 "member_id": "8989447062e04a818baf9e073fd04fa7", 129 "schema": "/v2/schemas/member" 130 }`) 131 }) 132 return &counter 133 } 134 135 // CallsCounter for checking if request handler was called at all 136 type CallsCounter struct { 137 Counter int 138 }