// authTokenPost verifies that providing certain AuthOptions and Scope results in an expected JSON structure. func authTokenPost(t *testing.T, options tokens.AuthOptions, scope *tokens.Scope, requestJSON string) { testhelper.SetupHTTP() defer testhelper.TeardownHTTP() client := gophercloud.ServiceClient{ ProviderClient: &gophercloud.ProviderClient{}, Endpoint: testhelper.Endpoint(), } testhelper.Mux.HandleFunc("/auth/tokens", func(w http.ResponseWriter, r *http.Request) { testhelper.TestMethod(t, r, "POST") testhelper.TestHeader(t, r, "Content-Type", "application/json") testhelper.TestHeader(t, r, "Accept", "application/json") testhelper.TestJSONRequest(t, r, requestJSON) w.WriteHeader(http.StatusCreated) fmt.Fprintf(w, `{ "token": { "expires_at": "2014-10-02T13:45:00.000000Z" } }`) }) if scope != nil { options.Scope = *scope } _, err := tokens.Create(&client, &options).Extract() if err != nil { t.Errorf("Create returned an error: %v", err) } }
func authTokenPostErr(t *testing.T, options tokens.AuthOptions, scope *tokens.Scope, includeToken bool, expectedErr error) { testhelper.SetupHTTP() defer testhelper.TeardownHTTP() client := gophercloud.ServiceClient{ ProviderClient: &gophercloud.ProviderClient{}, Endpoint: testhelper.Endpoint(), } if includeToken { client.TokenID = "abcdef123456" } if scope != nil { options.Scope = *scope } _, err := tokens.Create(&client, &options).Extract() if err == nil { t.Errorf("Create did NOT return an error") } if err != expectedErr { t.Errorf("Create returned an unexpected error: wanted %v, got %v", expectedErr, err) } }