PageRenderTime 66ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 1ms

/vendor/github.com/Azure/go-autorest/autorest/client_test.go

https://gitlab.com/unofficial-mirrors/openshift-origin
Go | 402 lines | 317 code | 72 blank | 13 comment | 62 complexity | 199ae1402b0078f96d895a484ac3d889 MD5 | raw file
  1. package autorest
  2. // Copyright 2017 Microsoft Corporation
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. import (
  16. "bytes"
  17. "fmt"
  18. "io/ioutil"
  19. "log"
  20. "math/rand"
  21. "net/http"
  22. "net/http/httptest"
  23. "reflect"
  24. "testing"
  25. "time"
  26. "github.com/Azure/go-autorest/autorest/mocks"
  27. )
  28. func TestLoggingInspectorWithInspection(t *testing.T) {
  29. b := bytes.Buffer{}
  30. c := Client{}
  31. li := LoggingInspector{Logger: log.New(&b, "", 0)}
  32. c.RequestInspector = li.WithInspection()
  33. Prepare(mocks.NewRequestWithContent("Content"),
  34. c.WithInspection())
  35. if len(b.String()) <= 0 {
  36. t.Fatal("autorest: LoggingInspector#WithInspection did not record Request to the log")
  37. }
  38. }
  39. func TestLoggingInspectorWithInspectionEmitsErrors(t *testing.T) {
  40. b := bytes.Buffer{}
  41. c := Client{}
  42. r := mocks.NewRequestWithContent("Content")
  43. li := LoggingInspector{Logger: log.New(&b, "", 0)}
  44. c.RequestInspector = li.WithInspection()
  45. if _, err := Prepare(r,
  46. c.WithInspection()); err != nil {
  47. t.Error(err)
  48. }
  49. if len(b.String()) <= 0 {
  50. t.Fatal("autorest: LoggingInspector#WithInspection did not record Request to the log")
  51. }
  52. }
  53. func TestLoggingInspectorWithInspectionRestoresBody(t *testing.T) {
  54. b := bytes.Buffer{}
  55. c := Client{}
  56. r := mocks.NewRequestWithContent("Content")
  57. li := LoggingInspector{Logger: log.New(&b, "", 0)}
  58. c.RequestInspector = li.WithInspection()
  59. Prepare(r,
  60. c.WithInspection())
  61. s, _ := ioutil.ReadAll(r.Body)
  62. if len(s) <= 0 {
  63. t.Fatal("autorest: LoggingInspector#WithInspection did not restore the Request body")
  64. }
  65. }
  66. func TestLoggingInspectorByInspecting(t *testing.T) {
  67. b := bytes.Buffer{}
  68. c := Client{}
  69. li := LoggingInspector{Logger: log.New(&b, "", 0)}
  70. c.ResponseInspector = li.ByInspecting()
  71. Respond(mocks.NewResponseWithContent("Content"),
  72. c.ByInspecting())
  73. if len(b.String()) <= 0 {
  74. t.Fatal("autorest: LoggingInspector#ByInspection did not record Response to the log")
  75. }
  76. }
  77. func TestLoggingInspectorByInspectingEmitsErrors(t *testing.T) {
  78. b := bytes.Buffer{}
  79. c := Client{}
  80. r := mocks.NewResponseWithContent("Content")
  81. li := LoggingInspector{Logger: log.New(&b, "", 0)}
  82. c.ResponseInspector = li.ByInspecting()
  83. if err := Respond(r,
  84. c.ByInspecting()); err != nil {
  85. t.Fatal(err)
  86. }
  87. if len(b.String()) <= 0 {
  88. t.Fatal("autorest: LoggingInspector#ByInspection did not record Response to the log")
  89. }
  90. }
  91. func TestLoggingInspectorByInspectingRestoresBody(t *testing.T) {
  92. b := bytes.Buffer{}
  93. c := Client{}
  94. r := mocks.NewResponseWithContent("Content")
  95. li := LoggingInspector{Logger: log.New(&b, "", 0)}
  96. c.ResponseInspector = li.ByInspecting()
  97. Respond(r,
  98. c.ByInspecting())
  99. s, _ := ioutil.ReadAll(r.Body)
  100. if len(s) <= 0 {
  101. t.Fatal("autorest: LoggingInspector#ByInspecting did not restore the Response body")
  102. }
  103. }
  104. func TestNewClientWithUserAgent(t *testing.T) {
  105. ua := "UserAgent"
  106. c := NewClientWithUserAgent(ua)
  107. completeUA := fmt.Sprintf("%s %s", defaultUserAgent, ua)
  108. if c.UserAgent != completeUA {
  109. t.Fatalf("autorest: NewClientWithUserAgent failed to set the UserAgent -- expected %s, received %s",
  110. completeUA, c.UserAgent)
  111. }
  112. }
  113. func TestAddToUserAgent(t *testing.T) {
  114. ua := "UserAgent"
  115. c := NewClientWithUserAgent(ua)
  116. ext := "extension"
  117. err := c.AddToUserAgent(ext)
  118. if err != nil {
  119. t.Fatalf("autorest: AddToUserAgent returned error -- expected nil, received %s", err)
  120. }
  121. completeUA := fmt.Sprintf("%s %s %s", defaultUserAgent, ua, ext)
  122. if c.UserAgent != completeUA {
  123. t.Fatalf("autorest: AddToUserAgent failed to add an extension to the UserAgent -- expected %s, received %s",
  124. completeUA, c.UserAgent)
  125. }
  126. err = c.AddToUserAgent("")
  127. if err == nil {
  128. t.Fatalf("autorest: AddToUserAgent didn't return error -- expected %s, received nil",
  129. fmt.Errorf("Extension was empty, User Agent stayed as %s", c.UserAgent))
  130. }
  131. if c.UserAgent != completeUA {
  132. t.Fatalf("autorest: AddToUserAgent failed to not add an empty extension to the UserAgent -- expected %s, received %s",
  133. completeUA, c.UserAgent)
  134. }
  135. }
  136. func TestClientSenderReturnsHttpClientByDefault(t *testing.T) {
  137. c := Client{}
  138. if fmt.Sprintf("%T", c.sender()) != "*http.Client" {
  139. t.Fatal("autorest: Client#sender failed to return http.Client by default")
  140. }
  141. }
  142. func TestClientSenderReturnsSetSender(t *testing.T) {
  143. c := Client{}
  144. s := mocks.NewSender()
  145. c.Sender = s
  146. if c.sender() != s {
  147. t.Fatal("autorest: Client#sender failed to return set Sender")
  148. }
  149. }
  150. func TestClientDoInvokesSender(t *testing.T) {
  151. c := Client{}
  152. s := mocks.NewSender()
  153. c.Sender = s
  154. c.Do(&http.Request{})
  155. if s.Attempts() != 1 {
  156. t.Fatal("autorest: Client#Do failed to invoke the Sender")
  157. }
  158. }
  159. func TestClientDoSetsUserAgent(t *testing.T) {
  160. ua := "UserAgent"
  161. c := Client{UserAgent: ua}
  162. r := mocks.NewRequest()
  163. s := mocks.NewSender()
  164. c.Sender = s
  165. c.Do(r)
  166. if r.UserAgent() != ua {
  167. t.Fatalf("autorest: Client#Do failed to correctly set User-Agent header: %s=%s",
  168. http.CanonicalHeaderKey(headerUserAgent), r.UserAgent())
  169. }
  170. }
  171. func TestClientDoSetsAuthorization(t *testing.T) {
  172. r := mocks.NewRequest()
  173. s := mocks.NewSender()
  174. c := Client{Authorizer: mockAuthorizer{}, Sender: s}
  175. c.Do(r)
  176. if len(r.Header.Get(http.CanonicalHeaderKey(headerAuthorization))) <= 0 {
  177. t.Fatalf("autorest: Client#Send failed to set Authorization header -- %s=%s",
  178. http.CanonicalHeaderKey(headerAuthorization),
  179. r.Header.Get(http.CanonicalHeaderKey(headerAuthorization)))
  180. }
  181. }
  182. func TestClientDoInvokesRequestInspector(t *testing.T) {
  183. r := mocks.NewRequest()
  184. s := mocks.NewSender()
  185. i := &mockInspector{}
  186. c := Client{RequestInspector: i.WithInspection(), Sender: s}
  187. c.Do(r)
  188. if !i.wasInvoked {
  189. t.Fatal("autorest: Client#Send failed to invoke the RequestInspector")
  190. }
  191. }
  192. func TestClientDoInvokesResponseInspector(t *testing.T) {
  193. r := mocks.NewRequest()
  194. s := mocks.NewSender()
  195. i := &mockInspector{}
  196. c := Client{ResponseInspector: i.ByInspecting(), Sender: s}
  197. c.Do(r)
  198. if !i.wasInvoked {
  199. t.Fatal("autorest: Client#Send failed to invoke the ResponseInspector")
  200. }
  201. }
  202. func TestClientDoReturnsErrorIfPrepareFails(t *testing.T) {
  203. c := Client{}
  204. s := mocks.NewSender()
  205. c.Authorizer = mockFailingAuthorizer{}
  206. c.Sender = s
  207. _, err := c.Do(&http.Request{})
  208. if err == nil {
  209. t.Fatalf("autorest: Client#Do failed to return an error when Prepare failed")
  210. }
  211. }
  212. func TestClientDoDoesNotSendIfPrepareFails(t *testing.T) {
  213. c := Client{}
  214. s := mocks.NewSender()
  215. c.Authorizer = mockFailingAuthorizer{}
  216. c.Sender = s
  217. c.Do(&http.Request{})
  218. if s.Attempts() > 0 {
  219. t.Fatal("autorest: Client#Do failed to invoke the Sender")
  220. }
  221. }
  222. func TestClientAuthorizerReturnsNullAuthorizerByDefault(t *testing.T) {
  223. c := Client{}
  224. if fmt.Sprintf("%T", c.authorizer()) != "autorest.NullAuthorizer" {
  225. t.Fatal("autorest: Client#authorizer failed to return the NullAuthorizer by default")
  226. }
  227. }
  228. func TestClientAuthorizerReturnsSetAuthorizer(t *testing.T) {
  229. c := Client{}
  230. c.Authorizer = mockAuthorizer{}
  231. if fmt.Sprintf("%T", c.authorizer()) != "autorest.mockAuthorizer" {
  232. t.Fatal("autorest: Client#authorizer failed to return the set Authorizer")
  233. }
  234. }
  235. func TestClientWithAuthorizer(t *testing.T) {
  236. c := Client{}
  237. c.Authorizer = mockAuthorizer{}
  238. req, _ := Prepare(&http.Request{},
  239. c.WithAuthorization())
  240. if req.Header.Get(headerAuthorization) == "" {
  241. t.Fatal("autorest: Client#WithAuthorizer failed to return the WithAuthorizer from the active Authorizer")
  242. }
  243. }
  244. func TestClientWithInspection(t *testing.T) {
  245. c := Client{}
  246. r := &mockInspector{}
  247. c.RequestInspector = r.WithInspection()
  248. Prepare(&http.Request{},
  249. c.WithInspection())
  250. if !r.wasInvoked {
  251. t.Fatal("autorest: Client#WithInspection failed to invoke RequestInspector")
  252. }
  253. }
  254. func TestClientWithInspectionSetsDefault(t *testing.T) {
  255. c := Client{}
  256. r1 := &http.Request{}
  257. r2, _ := Prepare(r1,
  258. c.WithInspection())
  259. if !reflect.DeepEqual(r1, r2) {
  260. t.Fatal("autorest: Client#WithInspection failed to provide a default RequestInspector")
  261. }
  262. }
  263. func TestClientByInspecting(t *testing.T) {
  264. c := Client{}
  265. r := &mockInspector{}
  266. c.ResponseInspector = r.ByInspecting()
  267. Respond(&http.Response{},
  268. c.ByInspecting())
  269. if !r.wasInvoked {
  270. t.Fatal("autorest: Client#ByInspecting failed to invoke ResponseInspector")
  271. }
  272. }
  273. func TestClientByInspectingSetsDefault(t *testing.T) {
  274. c := Client{}
  275. r := &http.Response{}
  276. Respond(r,
  277. c.ByInspecting())
  278. if !reflect.DeepEqual(r, &http.Response{}) {
  279. t.Fatal("autorest: Client#ByInspecting failed to provide a default ResponseInspector")
  280. }
  281. }
  282. func TestCookies(t *testing.T) {
  283. second := "second"
  284. expected := http.Cookie{
  285. Name: "tastes",
  286. Value: "delicious",
  287. }
  288. server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  289. http.SetCookie(w, &expected)
  290. b, err := ioutil.ReadAll(r.Body)
  291. if err != nil {
  292. t.Fatalf("autorest: ioutil.ReadAll failed reading request body: %s", err)
  293. }
  294. if string(b) == second {
  295. cookie, err := r.Cookie(expected.Name)
  296. if err != nil {
  297. t.Fatalf("autorest: r.Cookie could not get request cookie: %s", err)
  298. }
  299. if cookie == nil {
  300. t.Fatalf("autorest: got nil cookie, expecting %v", expected)
  301. }
  302. if cookie.Value != expected.Value {
  303. t.Fatalf("autorest: got cookie value '%s', expecting '%s'", cookie.Value, expected.Name)
  304. }
  305. }
  306. }))
  307. defer server.Close()
  308. client := NewClientWithUserAgent("")
  309. _, err := SendWithSender(client, mocks.NewRequestForURL(server.URL))
  310. if err != nil {
  311. t.Fatalf("autorest: first request failed: %s", err)
  312. }
  313. r2, err := http.NewRequest(http.MethodGet, server.URL, mocks.NewBody(second))
  314. if err != nil {
  315. t.Fatalf("autorest: failed creating second request: %s", err)
  316. }
  317. _, err = SendWithSender(client, r2)
  318. if err != nil {
  319. t.Fatalf("autorest: second request failed: %s", err)
  320. }
  321. }
  322. func randomString(n int) string {
  323. const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
  324. r := rand.New(rand.NewSource(time.Now().UTC().UnixNano()))
  325. s := make([]byte, n)
  326. for i := range s {
  327. s[i] = chars[r.Intn(len(chars))]
  328. }
  329. return string(s)
  330. }