Samuel A. Falvo II | fd78c30 | 2013-06-25 16:35:32 -0700 | [diff] [blame] | 1 | package gophercloud |
| 2 | |
Samuel A. Falvo II | 5d0d74c | 2013-06-25 17:23:18 -0700 | [diff] [blame] | 3 | import ( |
| 4 | "net/http" |
| 5 | ) |
| 6 | |
Samuel A. Falvo II | 4e89518 | 2013-06-26 15:44:18 -0700 | [diff] [blame^] | 7 | // 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 II | fd78c30 | 2013-06-25 16:35:32 -0700 | [diff] [blame] | 10 | type Context struct { |
Samuel A. Falvo II | 5d0d74c | 2013-06-25 17:23:18 -0700 | [diff] [blame] | 11 | // providerMap serves as a directory of supported providers. |
Samuel A. Falvo II | 4e89518 | 2013-06-26 15:44:18 -0700 | [diff] [blame^] | 12 | providerMap map[string]Provider |
Samuel A. Falvo II | 5d0d74c | 2013-06-25 17:23:18 -0700 | [diff] [blame] | 13 | |
| 14 | // httpClient refers to the current HTTP client interface to use. |
| 15 | httpClient *http.Client |
Samuel A. Falvo II | fd78c30 | 2013-06-25 16:35:32 -0700 | [diff] [blame] | 16 | } |
| 17 | |
Samuel A. Falvo II | 4e89518 | 2013-06-26 15:44:18 -0700 | [diff] [blame^] | 18 | // 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 II | fd78c30 | 2013-06-25 16:35:32 -0700 | [diff] [blame] | 25 | func TestContext() *Context { |
| 26 | return &Context{ |
Samuel A. Falvo II | 4e89518 | 2013-06-26 15:44:18 -0700 | [diff] [blame^] | 27 | providerMap: make(map[string]Provider), |
Samuel A. Falvo II | 839428e | 2013-06-25 18:02:24 -0700 | [diff] [blame] | 28 | httpClient: &http.Client{}, |
Samuel A. Falvo II | fd78c30 | 2013-06-25 16:35:32 -0700 | [diff] [blame] | 29 | } |
| 30 | } |
Samuel A. Falvo II | 5d0d74c | 2013-06-25 17:23:18 -0700 | [diff] [blame] | 31 | |
Samuel A. Falvo II | 4e89518 | 2013-06-26 15:44:18 -0700 | [diff] [blame^] | 32 | // 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 II | 5d0d74c | 2013-06-25 17:23:18 -0700 | [diff] [blame] | 35 | func (c *Context) UseCustomClient(hc *http.Client) { |
Samuel A. Falvo II | 839428e | 2013-06-25 18:02:24 -0700 | [diff] [blame] | 36 | c.httpClient = hc |
Samuel A. Falvo II | 5d0d74c | 2013-06-25 17:23:18 -0700 | [diff] [blame] | 37 | } |