blob: 70d70a7a886ae36e489cdc7835e4390fb367b0a5 [file] [log] [blame]
Samuel A. Falvo IIfd78c302013-06-25 16:35:32 -07001package gophercloud
2
Samuel A. Falvo II5d0d74c2013-06-25 17:23:18 -07003import (
4 "net/http"
5)
6
Samuel A. Falvo II4e895182013-06-26 15:44:18 -07007// Context structures encapsulate Gophercloud-global state in a manner which
8// facilitates easier unit testing. As a user of this SDK, you'll never
9// have to use this structure, except when contributing new code to the SDK.
Samuel A. Falvo IIfd78c302013-06-25 16:35:32 -070010type Context struct {
Samuel A. Falvo II5d0d74c2013-06-25 17:23:18 -070011 // providerMap serves as a directory of supported providers.
Samuel A. Falvo II4e895182013-06-26 15:44:18 -070012 providerMap map[string]Provider
Samuel A. Falvo II5d0d74c2013-06-25 17:23:18 -070013
14 // httpClient refers to the current HTTP client interface to use.
15 httpClient *http.Client
Samuel A. Falvo IIfd78c302013-06-25 16:35:32 -070016}
17
Samuel A. Falvo II4e895182013-06-26 15:44:18 -070018// TestContext yields a new Context instance, pre-initialized with a barren
19// state suitable for per-unit-test customization. This configuration consists
20// of:
21//
22// * An empty provider map.
23//
24// * An HTTP client built by the net/http package (see http://godoc.org/net/http#Client).
Samuel A. Falvo IIfd78c302013-06-25 16:35:32 -070025func TestContext() *Context {
26 return &Context{
Samuel A. Falvo II4e895182013-06-26 15:44:18 -070027 providerMap: make(map[string]Provider),
Samuel A. Falvo II839428e2013-06-25 18:02:24 -070028 httpClient: &http.Client{},
Samuel A. Falvo IIfd78c302013-06-25 16:35:32 -070029 }
30}
Samuel A. Falvo II5d0d74c2013-06-25 17:23:18 -070031
Samuel A. Falvo II4e895182013-06-26 15:44:18 -070032// UseCustomClient configures the context to use a customized HTTP client
33// instance. By default, TestContext() will return a Context which uses
34// the net/http package's default client instance.
Samuel A. Falvo II5d0d74c2013-06-25 17:23:18 -070035func (c *Context) UseCustomClient(hc *http.Client) {
Samuel A. Falvo II839428e2013-06-25 18:02:24 -070036 c.httpClient = hc
Samuel A. Falvo II5d0d74c2013-06-25 17:23:18 -070037}