Samuel A. Falvo II | 2e2b877 | 2013-07-04 15:40:15 -0700 | [diff] [blame] | 1 | package gophercloud |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "io/ioutil" |
| 6 | "net/http" |
| 7 | "strings" |
| 8 | ) |
| 9 | |
| 10 | type transport struct { |
| 11 | called int |
| 12 | response string |
| 13 | expectTenantId bool |
| 14 | tenantIdFound bool |
| 15 | } |
| 16 | |
| 17 | func (t *transport) RoundTrip(req *http.Request) (rsp *http.Response, err error) { |
| 18 | var authContainer *AuthContainer |
| 19 | |
| 20 | t.called++ |
| 21 | |
| 22 | headers := make(http.Header) |
| 23 | headers.Add("Content-Type", "application/xml; charset=UTF-8") |
| 24 | |
| 25 | body := ioutil.NopCloser(strings.NewReader(t.response)) |
| 26 | |
| 27 | rsp = &http.Response{ |
| 28 | Status: "200 OK", |
| 29 | StatusCode: 200, |
| 30 | Proto: "HTTP/1.1", |
| 31 | ProtoMajor: 1, |
| 32 | ProtoMinor: 1, |
| 33 | Header: headers, |
| 34 | Body: body, |
| 35 | ContentLength: -1, |
| 36 | TransferEncoding: nil, |
| 37 | Close: true, |
| 38 | Trailer: nil, |
| 39 | Request: req, |
| 40 | } |
| 41 | |
| 42 | bytes, err := ioutil.ReadAll(req.Body) |
| 43 | if err != nil { |
| 44 | return nil, err |
| 45 | } |
| 46 | err = json.Unmarshal(bytes, &authContainer) |
| 47 | if err != nil { |
| 48 | return nil, err |
| 49 | } |
| 50 | t.tenantIdFound = (authContainer.Auth.TenantId != "") |
| 51 | |
| 52 | if t.tenantIdFound != t.expectTenantId { |
| 53 | rsp.Status = "500 Internal Server Error" |
| 54 | rsp.StatusCode = 500 |
| 55 | } |
| 56 | return |
| 57 | } |
| 58 | |
| 59 | func newTransport() *transport { |
| 60 | return &transport{} |
| 61 | } |
| 62 | |
| 63 | func (t *transport) IgnoreTenantId() *transport { |
| 64 | t.expectTenantId = false |
| 65 | return t |
| 66 | } |
| 67 | |
| 68 | func (t *transport) ExpectTenantId() *transport { |
| 69 | t.expectTenantId = true |
| 70 | return t |
| 71 | } |
| 72 | |
| 73 | func (t *transport) WithResponse(r string) *transport { |
| 74 | t.response = r |
| 75 | return t |
| 76 | } |