本文整理匯總了Golang中github.com/gophercloud/gophercloud/testhelper.SetupHTTP函數的典型用法代碼示例。如果您正苦於以下問題:Golang SetupHTTP函數的具體用法?Golang SetupHTTP怎麽用?Golang SetupHTTP使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了SetupHTTP函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: TestGet
func TestGet(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/v2.0/extensions/agent", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "GET")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `
{
"extension": {
"updated": "2013-02-03T10:00:00-00:00",
"name": "agent",
"links": [],
"namespace": "http://docs.openstack.org/ext/agent/api/v2.0",
"alias": "agent",
"description": "The agent management extension."
}
}
`)
})
ext, err := extensions.Get(fake.ServiceClient(), "agent").Extract()
th.AssertNoErr(t, err)
th.AssertEquals(t, ext.Updated, "2013-02-03T10:00:00-00:00")
th.AssertEquals(t, ext.Name, "agent")
th.AssertEquals(t, ext.Namespace, "http://docs.openstack.org/ext/agent/api/v2.0")
th.AssertEquals(t, ext.Alias, "agent")
th.AssertEquals(t, ext.Description, "The agent management extension.")
}
示例2: TestChooseVersionFromSuffix
func TestChooseVersionFromSuffix(t *testing.T) {
testhelper.SetupHTTP()
defer testhelper.TeardownHTTP()
v2 := &utils.Version{ID: "v2.0", Priority: 2, Suffix: "/v2.0/"}
v3 := &utils.Version{ID: "v3.0", Priority: 3, Suffix: "/v3.0/"}
c := &gophercloud.ProviderClient{
IdentityBase: testhelper.Endpoint(),
IdentityEndpoint: testhelper.Endpoint() + "v2.0/",
}
v, endpoint, err := utils.ChooseVersion(c, []*utils.Version{v2, v3})
if err != nil {
t.Fatalf("Unexpected error from ChooseVersion: %v", err)
}
if v != v2 {
t.Errorf("Expected %#v to win, but %#v did instead", v2, v)
}
expected := testhelper.Endpoint() + "v2.0/"
if endpoint != expected {
t.Errorf("Expected endpoint [%s], but was [%s] instead", expected, endpoint)
}
}
示例3: createLinked
func createLinked(t *testing.T) pagination.Pager {
testhelper.SetupHTTP()
testhelper.Mux.HandleFunc("/page1", func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
fmt.Fprintf(w, `{ "ints": [1, 2, 3], "links": { "next": "%s/page2" } }`, testhelper.Server.URL)
})
testhelper.Mux.HandleFunc("/page2", func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
fmt.Fprintf(w, `{ "ints": [4, 5, 6], "links": { "next": "%s/page3" } }`, testhelper.Server.URL)
})
testhelper.Mux.HandleFunc("/page3", func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
fmt.Fprintf(w, `{ "ints": [7, 8, 9], "links": { "next": null } }`)
})
client := createClient()
createPage := func(r pagination.PageResult) pagination.Page {
return LinkedPageResult{pagination.LinkedPageBase{PageResult: r}}
}
return pagination.NewPager(client, testhelper.Server.URL+"/page1", createPage)
}
示例4: TestUpdateNova
// Verifies that it is possible to update a share network using nova network
func TestUpdateNova(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
MockUpdateNovaResponse(t)
expected := sharenetworks.ShareNetwork{
ID: "713df749-aac0-4a54-af52-10f6c991e80c",
Name: "net_my2",
CreatedAt: gophercloud.JSONRFC3339MilliNoZ(time.Date(2015, 9, 4, 14, 54, 25, 0, time.UTC)),
Description: "new description",
NetworkType: "",
CIDR: "",
NovaNetID: "new-nova-id",
NeutronNetID: "",
NeutronSubnetID: "",
IPVersion: 4,
SegmentationID: 0,
UpdatedAt: gophercloud.JSONRFC3339MilliNoZ(time.Date(2015, 9, 7, 8, 2, 53, 512184000, time.UTC)),
ProjectID: "16e1ab15c35a457e9c2b2aa189f544e1",
}
options := sharenetworks.UpdateOpts{
Name: "net_my2",
Description: "new description",
NovaNetID: "new-nova-id",
}
v, err := sharenetworks.Update(client.ServiceClient(), "713df749-aac0-4a54-af52-10f6c991e80c", options).Extract()
th.AssertNoErr(t, err)
th.CheckDeepEquals(t, &expected, v)
}
示例5: TestListPools
func TestListPools(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
HandlePoolListSuccessfully(t)
pages := 0
err := pools.List(fake.ServiceClient(), pools.ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
pages++
actual, err := pools.ExtractPools(page)
if err != nil {
return false, err
}
if len(actual) != 2 {
t.Fatalf("Expected 2 pools, got %d", len(actual))
}
th.CheckDeepEquals(t, PoolWeb, actual[0])
th.CheckDeepEquals(t, PoolDb, actual[1])
return true, nil
})
th.AssertNoErr(t, err)
if pages != 1 {
t.Errorf("Expected 1 page, saw %d", pages)
}
}
示例6: TestListAll
func TestListAll(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
MockListResponse(t)
allPages, err := volumes.List(client.ServiceClient(), &volumes.ListOpts{}).AllPages()
th.AssertNoErr(t, err)
actual, err := volumes.ExtractVolumes(allPages)
th.AssertNoErr(t, err)
expected := []volumes.Volume{
{
ID: "289da7f8-6440-407c-9fb4-7db01ec49164",
Name: "vol-001",
},
{
ID: "96c3bda7-c82a-4f50-be73-ca7621794835",
Name: "vol-002",
},
}
th.CheckDeepEquals(t, expected, actual)
}
示例7: TestPaginatedListDetail
// Verifies that share networks list can be called with query parameters
func TestPaginatedListDetail(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
MockFilteredListResponse(t)
options := &sharenetworks.ListOpts{
Offset: 0,
Limit: 1,
}
count := 0
err := sharenetworks.ListDetail(client.ServiceClient(), options).EachPage(func(page pagination.Page) (bool, error) {
count++
_, err := sharenetworks.ExtractShareNetworks(page)
if err != nil {
t.Errorf("Failed to extract share networks: %v", err)
return false, err
}
return true, nil
})
th.AssertNoErr(t, err)
th.AssertEquals(t, count, 3)
}
示例8: TestCreateMemberSuccessfully
func TestCreateMemberSuccessfully(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
HandleCreateImageMemberSuccessfully(t)
im, err := members.Create(fakeclient.ServiceClient(), "da3b75d9-3f4a-40e7-8a2c-bfab23927dea",
"8989447062e04a818baf9e073fd04fa7").Extract()
th.AssertNoErr(t, err)
createdAt, err := time.Parse(time.RFC3339, createdAtString)
th.AssertNoErr(t, err)
updatedAt, err := time.Parse(time.RFC3339, updatedAtString)
th.AssertNoErr(t, err)
th.AssertDeepEquals(t, members.Member{
CreatedAt: createdAt,
ImageID: "da3b75d9-3f4a-40e7-8a2c-bfab23927dea",
MemberID: "8989447062e04a818baf9e073fd04fa7",
Schema: "/v2/schemas/member",
Status: "pending",
UpdatedAt: updatedAt,
}, *im)
}
示例9: TestMemberListEmpty
func TestMemberListEmpty(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
HandleImageMemberEmptyList(t)
pager := members.List(fakeclient.ServiceClient(), "da3b75d9-3f4a-40e7-8a2c-bfab23927dea")
t.Logf("Pager state %v", pager)
count, pages := 0, 0
err := pager.EachPage(func(page pagination.Page) (bool, error) {
pages++
t.Logf("Page %v", page)
members, err := members.ExtractMembers(page)
if err != nil {
return false, err
}
for _, i := range members {
t.Logf("%s\t%s\t%s\t%s\t\n", i.ImageID, i.MemberID, i.Status, i.Schema)
count++
}
return true, nil
})
th.AssertNoErr(t, err)
th.AssertEquals(t, 0, pages)
th.AssertEquals(t, 0, count)
}
示例10: TestShowMemberDetails
func TestShowMemberDetails(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
HandleImageMemberDetails(t)
md, err := members.Get(fakeclient.ServiceClient(),
"da3b75d9-3f4a-40e7-8a2c-bfab23927dea",
"8989447062e04a818baf9e073fd04fa7").Extract()
th.AssertNoErr(t, err)
if md == nil {
t.Errorf("Expected non-nil value for md")
}
createdAt, err := time.Parse(time.RFC3339, "2013-11-26T07:21:21Z")
th.AssertNoErr(t, err)
updatedAt, err := time.Parse(time.RFC3339, "2013-11-26T07:21:21Z")
th.AssertNoErr(t, err)
th.AssertDeepEquals(t, members.Member{
CreatedAt: createdAt,
ImageID: "da3b75d9-3f4a-40e7-8a2c-bfab23927dea",
MemberID: "8989447062e04a818baf9e073fd04fa7",
Schema: "/v2/schemas/member",
Status: "pending",
UpdatedAt: updatedAt,
}, *md)
}
示例11: TestMemberUpdateSuccessfully
func TestMemberUpdateSuccessfully(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
counter := HandleImageMemberUpdate(t)
im, err := members.Update(fakeclient.ServiceClient(), "da3b75d9-3f4a-40e7-8a2c-bfab23927dea",
"8989447062e04a818baf9e073fd04fa7",
members.UpdateOpts{
Status: "accepted",
}).Extract()
th.AssertEquals(t, 1, counter.Counter)
th.AssertNoErr(t, err)
createdAt, err := time.Parse(time.RFC3339, "2013-11-26T07:21:21Z")
th.AssertNoErr(t, err)
updatedAt, err := time.Parse(time.RFC3339, "2013-11-26T07:21:21Z")
th.AssertNoErr(t, err)
th.AssertDeepEquals(t, members.Member{
CreatedAt: createdAt,
ImageID: "da3b75d9-3f4a-40e7-8a2c-bfab23927dea",
MemberID: "8989447062e04a818baf9e073fd04fa7",
Schema: "/v2/schemas/member",
Status: "accepted",
UpdatedAt: updatedAt,
}, *im)
}
示例12: TestGet
func TestGet(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/networks/d32019d3-bc6e-4319-9c1d-6722fc136a22", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "GET")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `
{
"network": {
"admin_state_up": true,
"id": "8d05a1b1-297a-46ca-8974-17debf51ca3c",
"name": "ext_net",
"router:external": true,
"shared": false,
"status": "ACTIVE",
"subnets": [
"2f1fb918-9b0e-4bf9-9a50-6cebbb4db2c5"
],
"tenant_id": "5eb8995cf717462c9df8d1edfa498010"
}
}
`)
})
res := networks.Get(fake.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22")
n, err := external.ExtractGet(res)
th.AssertNoErr(t, err)
th.AssertEquals(t, true, n.External)
}
示例13: TestListServers
func TestListServers(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
HandleServerListSuccessfully(t)
pages := 0
err := servers.List(client.ServiceClient(), servers.ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
pages++
actual, err := servers.ExtractServers(page)
if err != nil {
return false, err
}
if len(actual) != 3 {
t.Fatalf("Expected 3 servers, got %d", len(actual))
}
th.CheckDeepEquals(t, ServerHerp, actual[0])
th.CheckDeepEquals(t, ServerDerp, actual[1])
th.CheckDeepEquals(t, ServerMerp, actual[2])
return true, nil
})
th.AssertNoErr(t, err)
if pages != 1 {
t.Errorf("Expected 1 page, saw %d", pages)
}
}
示例14: TestFetch
// test the fetch function
func TestFetch(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
baseurl, err := getBasePath()
th.AssertNoErr(t, err)
fakeURL := strings.Join([]string{baseurl, "file.yaml"}, "/")
urlparsed, err := url.Parse(fakeURL)
th.AssertNoErr(t, err)
th.Mux.HandleFunc(urlparsed.Path, func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "GET")
w.Header().Set("Content-Type", "application/jason")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "Fee-fi-fo-fum")
})
client := fakeClient{BaseClient: getHTTPClient()}
te := TE{
URL: "file.yaml",
client: client,
}
err = te.Fetch()
th.AssertNoErr(t, err)
th.AssertEquals(t, fakeURL, te.URL)
th.AssertEquals(t, "Fee-fi-fo-fum", string(te.Bin))
}
示例15: TestList
func TestList(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
HandleList(t)
expectedDBs := []databases.Database{
{Name: "anotherexampledb"},
{Name: "exampledb"},
{Name: "nextround"},
{Name: "sampledb"},
{Name: "testingdb"},
}
pages := 0
err := databases.List(fake.ServiceClient(), instanceID).EachPage(func(page pagination.Page) (bool, error) {
pages++
actual, err := databases.ExtractDBs(page)
if err != nil {
return false, err
}
th.CheckDeepEquals(t, expectedDBs, actual)
return true, nil
})
th.AssertNoErr(t, err)
if pages != 1 {
t.Errorf("Expected 1 page, saw %d", pages)
}
}