github.com/gophercloud/gophercloud@v1.11.0/docs/contributor-tutorial/.template/testing/fixtures_test.go (about) 1 package testing 2 3 import ( 4 "fmt" 5 "net/http" 6 "testing" 7 8 "github.com/gophercloud/gophercloud/openstack/service/vN/resources" 9 th "github.com/gophercloud/gophercloud/testhelper" 10 "github.com/gophercloud/gophercloud/testhelper/client" 11 ) 12 13 // ListResult provides a single page of RESOURCE results. 14 const ListResult = ` 15 { 16 } 17 ` 18 19 // GetResult provides a Get result. 20 const GetResult = ` 21 { 22 } 23 ` 24 25 // CreateRequest provides the input to a Create request. 26 const CreateRequest = ` 27 { 28 } 29 ` 30 31 // UpdateRequest provides the input to as Update request. 32 const UpdateRequest = ` 33 { 34 } 35 ` 36 37 // UpdateResult provides an update result. 38 const UpdateResult = ` 39 { 40 } 41 ` 42 43 // FirstResource is the first resource in the List request. 44 var FirstResource = resources.Resource{} 45 46 // SecondResource is the second resource in the List request. 47 var SecondResource = resources.Resource{} 48 49 // SecondResourceUpdated is how SecondResource should look after an Update. 50 var SecondResourceUpdated = resources.Resource{} 51 52 // ExpectedResourcesSlice is the slice of resources expected to be returned from ListResult. 53 var ExpectedResourcesSlice = []resources.Resource{FirstResource, SecondResource} 54 55 // HandleListResourceSuccessfully creates an HTTP handler at `/resources` on the 56 // test handler mux that responds with a list of two resources. 57 func HandleListResourceSuccessfully(t *testing.T) { 58 th.Mux.HandleFunc("/resources", func(w http.ResponseWriter, r *http.Request) { 59 th.TestMethod(t, r, "GET") 60 th.TestHeader(t, r, "Accept", "application/json") 61 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 62 63 w.Header().Set("Content-Type", "application/json") 64 w.WriteHeader(http.StatusOK) 65 fmt.Fprintf(w, ListResult) 66 }) 67 } 68 69 // HandleGetResourceSuccessfully creates an HTTP handler at `/resources` on the 70 // test handler mux that responds with a single resource. 71 func HandleGetResourceSuccessfully(t *testing.T) { 72 th.Mux.HandleFunc("/resources/9fe1d3", func(w http.ResponseWriter, r *http.Request) { 73 th.TestMethod(t, r, "GET") 74 th.TestHeader(t, r, "Accept", "application/json") 75 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 76 77 w.Header().Set("Content-Type", "application/json") 78 w.WriteHeader(http.StatusOK) 79 fmt.Fprintf(w, GetResult) 80 }) 81 } 82 83 // HandleCreateResourceSuccessfully creates an HTTP handler at `/resources` on the 84 // test handler mux that tests resource creation. 85 func HandleCreateResourceSuccessfully(t *testing.T) { 86 th.Mux.HandleFunc("/resources", func(w http.ResponseWriter, r *http.Request) { 87 th.TestMethod(t, r, "POST") 88 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 89 th.TestJSONRequest(t, r, CreateRequest) 90 91 w.WriteHeader(http.StatusCreated) 92 fmt.Fprintf(w, GetResult) 93 }) 94 } 95 96 // HandleDeleteResourceSuccessfully creates an HTTP handler at `/resources` on the 97 // test handler mux that tests resource deletion. 98 func HandleDeleteResourceSuccessfully(t *testing.T) { 99 th.Mux.HandleFunc("/resources/9fe1d3", func(w http.ResponseWriter, r *http.Request) { 100 th.TestMethod(t, r, "DELETE") 101 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 102 103 w.WriteHeader(http.StatusNoContent) 104 }) 105 } 106 107 // HandleUpdateResourceSuccessfully creates an HTTP handler at `/resources` on the 108 // test handler mux that tests resource update. 109 func HandleUpdateResourceSuccessfully(t *testing.T) { 110 th.Mux.HandleFunc("/resources/9fe1d3", func(w http.ResponseWriter, r *http.Request) { 111 th.TestMethod(t, r, "PATCH") 112 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 113 th.TestJSONRequest(t, r, UpdateRequest) 114 115 w.WriteHeader(http.StatusOK) 116 fmt.Fprintf(w, UpdateResult) 117 }) 118 }