Samuel A. Falvo II | 2e2b877 | 2013-07-04 15:40:15 -0700 | [diff] [blame] | 1 | package gophercloud |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
Samuel A. Falvo II | 20f1aa4 | 2013-07-31 14:32:03 -0700 | [diff] [blame] | 5 | "fmt" |
Samuel A. Falvo II | 2e2b877 | 2013-07-04 15:40:15 -0700 | [diff] [blame] | 6 | "io/ioutil" |
| 7 | "net/http" |
| 8 | "strings" |
Samuel A. Falvo II | 2d0f6da | 2013-07-15 16:41:52 -0700 | [diff] [blame] | 9 | "testing" |
Samuel A. Falvo II | 2e2b877 | 2013-07-04 15:40:15 -0700 | [diff] [blame] | 10 | ) |
| 11 | |
| 12 | type transport struct { |
| 13 | called int |
| 14 | response string |
| 15 | expectTenantId bool |
| 16 | tenantIdFound bool |
Samuel A. Falvo II | 20f1aa4 | 2013-07-31 14:32:03 -0700 | [diff] [blame] | 17 | status int |
Samuel A. Falvo II | 2e2b877 | 2013-07-04 15:40:15 -0700 | [diff] [blame] | 18 | } |
| 19 | |
| 20 | func (t *transport) RoundTrip(req *http.Request) (rsp *http.Response, err error) { |
| 21 | var authContainer *AuthContainer |
| 22 | |
| 23 | t.called++ |
| 24 | |
| 25 | headers := make(http.Header) |
| 26 | headers.Add("Content-Type", "application/xml; charset=UTF-8") |
| 27 | |
| 28 | body := ioutil.NopCloser(strings.NewReader(t.response)) |
| 29 | |
Samuel A. Falvo II | 2d0f6da | 2013-07-15 16:41:52 -0700 | [diff] [blame] | 30 | if t.status == 0 { |
| 31 | t.status = 200 |
| 32 | } |
| 33 | statusMsg := "OK" |
| 34 | if (t.status < 200) || (299 < t.status) { |
| 35 | statusMsg = "Error" |
| 36 | } |
| 37 | |
Samuel A. Falvo II | 2e2b877 | 2013-07-04 15:40:15 -0700 | [diff] [blame] | 38 | rsp = &http.Response{ |
Samuel A. Falvo II | 2d0f6da | 2013-07-15 16:41:52 -0700 | [diff] [blame] | 39 | Status: fmt.Sprintf("%d %s", t.status, statusMsg), |
| 40 | StatusCode: t.status, |
Samuel A. Falvo II | 2e2b877 | 2013-07-04 15:40:15 -0700 | [diff] [blame] | 41 | Proto: "HTTP/1.1", |
| 42 | ProtoMajor: 1, |
| 43 | ProtoMinor: 1, |
| 44 | Header: headers, |
| 45 | Body: body, |
| 46 | ContentLength: -1, |
| 47 | TransferEncoding: nil, |
| 48 | Close: true, |
| 49 | Trailer: nil, |
| 50 | Request: req, |
| 51 | } |
| 52 | |
| 53 | bytes, err := ioutil.ReadAll(req.Body) |
| 54 | if err != nil { |
| 55 | return nil, err |
| 56 | } |
| 57 | err = json.Unmarshal(bytes, &authContainer) |
| 58 | if err != nil { |
| 59 | return nil, err |
| 60 | } |
| 61 | t.tenantIdFound = (authContainer.Auth.TenantId != "") |
| 62 | |
| 63 | if t.tenantIdFound != t.expectTenantId { |
| 64 | rsp.Status = "500 Internal Server Error" |
| 65 | rsp.StatusCode = 500 |
| 66 | } |
| 67 | return |
| 68 | } |
| 69 | |
| 70 | func newTransport() *transport { |
| 71 | return &transport{} |
| 72 | } |
| 73 | |
| 74 | func (t *transport) IgnoreTenantId() *transport { |
| 75 | t.expectTenantId = false |
| 76 | return t |
| 77 | } |
| 78 | |
| 79 | func (t *transport) ExpectTenantId() *transport { |
| 80 | t.expectTenantId = true |
| 81 | return t |
| 82 | } |
| 83 | |
| 84 | func (t *transport) WithResponse(r string) *transport { |
| 85 | t.response = r |
Samuel A. Falvo II | 2d0f6da | 2013-07-15 16:41:52 -0700 | [diff] [blame] | 86 | t.status = 200 |
Samuel A. Falvo II | 2e2b877 | 2013-07-04 15:40:15 -0700 | [diff] [blame] | 87 | return t |
| 88 | } |
Samuel A. Falvo II | 2d0f6da | 2013-07-15 16:41:52 -0700 | [diff] [blame] | 89 | |
| 90 | func (t *transport) WithError(code int) *transport { |
| 91 | t.response = fmt.Sprintf("Error %d", code) |
| 92 | t.status = code |
| 93 | return t |
| 94 | } |
| 95 | |
| 96 | func (t *transport) VerifyCalls(test *testing.T, n int) error { |
| 97 | if t.called != n { |
| 98 | err := fmt.Errorf("Expected Transport to be called %d times; found %d instead", n, t.called) |
| 99 | test.Error(err) |
| 100 | return err |
| 101 | } |
| 102 | return nil |
Samuel A. Falvo II | 20f1aa4 | 2013-07-31 14:32:03 -0700 | [diff] [blame] | 103 | } |