blob: d2637c4ad31117c13011a00acd35193c9d957474 [file] [log] [blame]
Ash Wilsonb73b7f82014-08-29 15:38:06 -04001package services
2
3import (
4 "fmt"
5 "net/http"
6 "testing"
7
8 "github.com/rackspace/gophercloud"
9 "github.com/rackspace/gophercloud/testhelper"
10)
11
12func TestCreateSuccessful(t *testing.T) {
13 testhelper.SetupHTTP()
14 defer testhelper.TeardownHTTP()
15
16 testhelper.Mux.HandleFunc("/services", func(w http.ResponseWriter, r *http.Request) {
17 testhelper.TestMethod(t, r, "POST")
18 testhelper.TestHeader(t, r, "X-Auth-Token", "1111")
19 testhelper.TestJSONRequest(t, r, `{ "type": "compute" }`)
20
21 w.Header().Add("Content-Type", "application/json")
22 w.WriteHeader(http.StatusCreated)
23 fmt.Fprintf(w, `{
24 "service": {
25 "description": "Here's your service",
26 "id": "1234",
27 "name": "InscrutableOpenStackProjectName",
28 "type": "compute"
29 }
30 }`)
31 })
32
33 client := gophercloud.ServiceClient{
34 ProviderClient: gophercloud.ProviderClient{
35 TokenID: "1111",
36 },
37 Endpoint: testhelper.Endpoint(),
38 }
39
40 result, err := Create(&client, "compute")
41 if err != nil {
42 t.Fatalf("Unexpected error from Create: %v", err)
43 }
44
45 if result.Description == nil || *result.Description != "Here's your service" {
46 t.Errorf("Service description was unexpected [%s]", result.Description)
47 }
48 if result.ID != "1234" {
49 t.Errorf("Service ID was unexpected [%s]", result.ID)
50 }
51 if result.Name != "InscrutableOpenStackProjectName" {
52 t.Errorf("Service name was unexpected [%s]", result.Name)
53 }
54 if result.Type != "compute" {
55 t.Errorf("Service type was unexpected [%s]", result.Type)
56 }
57}