From 2c30880ae2fe590c82d581461b23cf59ec2ff83b Mon Sep 17 00:00:00 2001 From: Stefan Majewsky Date: Fri, 16 Feb 2018 17:12:35 +0100 Subject: add useProxy() helper function for tests --- shared_test.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'shared_test.go') diff --git a/shared_test.go b/shared_test.go index 1e7130e..ba05453 100644 --- a/shared_test.go +++ b/shared_test.go @@ -22,6 +22,8 @@ import ( "crypto/rand" "encoding/hex" "math" + "net/http" + "net/url" "os" "testing" @@ -30,6 +32,23 @@ import ( "github.com/gophercloud/gophercloud/openstack/objectstorage/v1/swauth" ) +//This function can be used during debugging to redirect the HTTP requests for +//a specific unit test through a mitmproxy. Put this at the beginning of your +//testcase like so: +// +// testWithAccount(t, func(a *Account) { +// useProxy(a, "http://localhost:8888") +// ... +// +// testWithContainer(t, func(c *Container) { +// useProxy(c.Account(), "http://localhost:8888") +// ... +func useProxy(a *Account, proxyURL string) { + t := *(http.DefaultTransport.(*http.Transport)) + t.Proxy = func(*http.Request) (*url.URL, error) { return url.Parse(proxyURL) } + a.client.ProviderClient.HTTPClient.Transport = &t +} + func testWithAccount(t *testing.T, testCode func(a *Account)) { stAuth := os.Getenv("ST_AUTH") stUser := os.Getenv("ST_USER") -- cgit v1.2.3