github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/openstack/identity/v3/federation/protocols/testing/requests_test.go (about) 1 package testing 2 3 import ( 4 "fmt" 5 "net/http" 6 "testing" 7 8 "github.com/opentelekomcloud/gophertelekomcloud/openstack/identity/v3/federation/protocols" 9 th "github.com/opentelekomcloud/gophertelekomcloud/testhelper" 10 fake "github.com/opentelekomcloud/gophertelekomcloud/testhelper/client" 11 ) 12 13 func TestProtocolCreate(t *testing.T) { 14 th.SetupHTTP() 15 defer th.TeardownHTTP() 16 17 th.Mux.HandleFunc(protocolURL, func(w http.ResponseWriter, r *http.Request) { 18 th.TestMethod(t, r, "PUT") 19 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 20 th.TestHeader(t, r, "Content-Type", "application/json") 21 th.TestHeader(t, r, "Accept", "application/json") 22 th.TestJSONRequest(t, r, createRequestBody) 23 w.WriteHeader(201) 24 _, _ = fmt.Fprint(w, getResponseBody) 25 }) 26 27 protocol, err := protocols.Create(fake.ServiceClient(), providerID, protocolID, protocols.CreateOpts{ 28 MappingID: mappingID, 29 }).Extract() 30 th.AssertNoErr(t, err) 31 th.AssertEquals(t, mappingID, protocol.MappingID) 32 } 33 34 func TestProtocolList(t *testing.T) { 35 th.SetupHTTP() 36 defer th.TeardownHTTP() 37 38 th.Mux.HandleFunc(listURL, func(w http.ResponseWriter, r *http.Request) { 39 th.TestMethod(t, r, "GET") 40 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 41 th.TestHeader(t, r, "Content-Type", "application/json") 42 th.TestHeader(t, r, "Accept", "application/json") 43 44 w.Header().Add("Content-Type", "application/json") 45 _, _ = fmt.Fprint(w, listResponseBody) 46 }) 47 48 pages, err := protocols.List(fake.ServiceClient(), providerID).AllPages() 49 th.AssertNoErr(t, err) 50 prots, err := protocols.ExtractProtocols(pages) 51 th.AssertNoErr(t, err) 52 th.AssertEquals(t, 1, len(prots)) 53 } 54 55 func TestProtocolGet(t *testing.T) { 56 th.SetupHTTP() 57 defer th.TeardownHTTP() 58 59 th.Mux.HandleFunc(protocolURL, func(w http.ResponseWriter, r *http.Request) { 60 th.TestMethod(t, r, "GET") 61 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 62 th.TestHeader(t, r, "Content-Type", "application/json") 63 th.TestHeader(t, r, "Accept", "application/json") 64 65 w.Header().Add("Content-Type", "application/json") 66 _, _ = fmt.Fprint(w, getResponseBody) 67 }) 68 69 protocol, err := protocols.Get(fake.ServiceClient(), providerID, protocolID).Extract() 70 th.AssertNoErr(t, err) 71 th.AssertEquals(t, mappingID, protocol.MappingID) 72 } 73 74 func TestProtocolUpdate(t *testing.T) { 75 th.SetupHTTP() 76 defer th.TeardownHTTP() 77 78 th.Mux.HandleFunc(protocolURL, func(w http.ResponseWriter, r *http.Request) { 79 th.TestMethod(t, r, "PATCH") 80 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 81 th.TestHeader(t, r, "Content-Type", "application/json") 82 th.TestHeader(t, r, "Accept", "application/json") 83 84 w.Header().Add("Content-Type", "application/json") 85 _, _ = fmt.Fprint(w, getResponseBody) 86 }) 87 88 opts := protocols.UpdateOpts{ 89 MappingID: "SAML2", 90 } 91 protocol, err := protocols.Update(fake.ServiceClient(), providerID, protocolID, opts).Extract() 92 th.AssertNoErr(t, err) 93 th.AssertEquals(t, mappingID, protocol.MappingID) 94 } 95 96 func TestProtocolDelete(t *testing.T) { 97 th.SetupHTTP() 98 defer th.TeardownHTTP() 99 100 th.Mux.HandleFunc(protocolURL, func(w http.ResponseWriter, r *http.Request) { 101 th.TestMethod(t, r, "DELETE") 102 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 103 th.TestHeader(t, r, "Content-Type", "application/json") 104 th.TestHeader(t, r, "Accept", "application/json") 105 106 w.WriteHeader(204) 107 }) 108 }