context_test.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. // Copyright 2014 Manu Martinez-Almeida. All rights reserved.
  2. // Use of this source code is governed by a MIT style
  3. // license that can be found in the LICENSE file.
  4. package gin
  5. import (
  6. "bytes"
  7. "errors"
  8. "html/template"
  9. "mime/multipart"
  10. "net/http"
  11. "net/http/httptest"
  12. "testing"
  13. "time"
  14. "github.com/gin-gonic/gin/binding"
  15. "github.com/manucorporat/sse"
  16. "github.com/stretchr/testify/assert"
  17. )
  18. // Unit tests TODO
  19. // func (c *Context) File(filepath string) {
  20. // func (c *Context) Negotiate(code int, config Negotiate) {
  21. // BAD case: func (c *Context) Render(code int, render render.Render, obj ...interface{}) {
  22. // test that information is not leaked when reusing Contexts (using the Pool)
  23. func createTestContext() (c *Context, w *httptest.ResponseRecorder, r *Engine) {
  24. w = httptest.NewRecorder()
  25. r = New()
  26. c = r.allocateContext()
  27. c.reset()
  28. c.writermem.reset(w)
  29. return
  30. }
  31. func createMultipartForm() (body *bytes.Buffer, header string, err error) {
  32. boundary := "--testboundary"
  33. header = MIMEMultipartPOSTForm + "; boundary=" + boundary
  34. body = &bytes.Buffer{}
  35. mw := multipart.NewWriter(body)
  36. defer mw.Close()
  37. if err = mw.SetBoundary(boundary); err != nil {
  38. return
  39. }
  40. if err = mw.WriteField("foo", "bar"); err != nil {
  41. return
  42. }
  43. if err = mw.WriteField("bar", "foo"); err != nil {
  44. return
  45. }
  46. return
  47. }
  48. func TestContextReset(t *testing.T) {
  49. router := New()
  50. c := router.allocateContext()
  51. assert.Equal(t, c.engine, router)
  52. c.index = 2
  53. c.Writer = &responseWriter{ResponseWriter: httptest.NewRecorder()}
  54. c.Params = Params{Param{}}
  55. c.Error(errors.New("test"))
  56. c.Set("foo", "bar")
  57. c.reset()
  58. assert.False(t, c.IsAborted())
  59. assert.Nil(t, c.Keys)
  60. assert.Nil(t, c.Accepted)
  61. assert.Len(t, c.Errors, 0)
  62. assert.Empty(t, c.Errors.Errors())
  63. assert.Empty(t, c.Errors.ByType(ErrorTypeAny))
  64. assert.Len(t, c.Params, 0)
  65. assert.Equal(t, c.index, -1)
  66. assert.Equal(t, c.Writer.(*responseWriter), &c.writermem)
  67. }
  68. // TestContextSetGet tests that a parameter is set correctly on the
  69. // current context and can be retrieved using Get.
  70. func TestContextSetGet(t *testing.T) {
  71. c, _, _ := createTestContext()
  72. c.Set("foo", "bar")
  73. value, err := c.Get("foo")
  74. assert.Equal(t, value, "bar")
  75. assert.True(t, err)
  76. value, err = c.Get("foo2")
  77. assert.Nil(t, value)
  78. assert.False(t, err)
  79. assert.Equal(t, c.MustGet("foo"), "bar")
  80. assert.Panics(t, func() { c.MustGet("no_exist") })
  81. }
  82. func TestContextSetGetValues(t *testing.T) {
  83. c, _, _ := createTestContext()
  84. c.Set("string", "this is a string")
  85. c.Set("int32", int32(-42))
  86. c.Set("int64", int64(42424242424242))
  87. c.Set("uint64", uint64(42))
  88. c.Set("float32", float32(4.2))
  89. c.Set("float64", 4.2)
  90. var a interface{} = 1
  91. c.Set("intInterface", a)
  92. assert.Exactly(t, c.MustGet("string").(string), "this is a string")
  93. assert.Exactly(t, c.MustGet("int32").(int32), int32(-42))
  94. assert.Exactly(t, c.MustGet("int64").(int64), int64(42424242424242))
  95. assert.Exactly(t, c.MustGet("uint64").(uint64), uint64(42))
  96. assert.Exactly(t, c.MustGet("float32").(float32), float32(4.2))
  97. assert.Exactly(t, c.MustGet("float64").(float64), 4.2)
  98. assert.Exactly(t, c.MustGet("intInterface").(int), 1)
  99. }
  100. func TestContextCopy(t *testing.T) {
  101. c, _, _ := createTestContext()
  102. c.index = 2
  103. c.Request, _ = http.NewRequest("POST", "/hola", nil)
  104. c.handlers = HandlersChain{func(c *Context) {}}
  105. c.Params = Params{Param{Key: "foo", Value: "bar"}}
  106. c.Set("foo", "bar")
  107. cp := c.Copy()
  108. assert.Nil(t, cp.handlers)
  109. assert.Nil(t, cp.writermem.ResponseWriter)
  110. assert.Equal(t, &cp.writermem, cp.Writer.(*responseWriter))
  111. assert.Equal(t, cp.Request, c.Request)
  112. assert.Equal(t, cp.index, AbortIndex)
  113. assert.Equal(t, cp.Keys, c.Keys)
  114. assert.Equal(t, cp.engine, c.engine)
  115. assert.Equal(t, cp.Params, c.Params)
  116. }
  117. func TestContextFormParse(t *testing.T) {
  118. c, _, _ := createTestContext()
  119. c.Request, _ = http.NewRequest("GET", "http://example.com/?foo=bar&page=10", nil)
  120. assert.Equal(t, c.DefaultQuery("foo", "none"), "bar")
  121. assert.Equal(t, c.Query("foo"), "bar")
  122. assert.Empty(t, c.PostForm("foo"))
  123. assert.Equal(t, c.DefaultQuery("page", "0"), "10")
  124. assert.Equal(t, c.Query("page"), "10")
  125. assert.Empty(t, c.PostForm("page"))
  126. assert.Equal(t, c.DefaultQuery("NoKey", "nada"), "nada")
  127. assert.Empty(t, c.Query("NoKey"))
  128. assert.Empty(t, c.PostForm("NoKey"))
  129. }
  130. func TestContextPostFormParse(t *testing.T) {
  131. c, _, _ := createTestContext()
  132. body := bytes.NewBufferString("foo=bar&page=11&both=POST")
  133. c.Request, _ = http.NewRequest("POST", "/?both=GET&id=main", body)
  134. c.Request.Header.Add("Content-Type", MIMEPOSTForm)
  135. assert.Equal(t, c.DefaultPostForm("foo", "none"), "bar")
  136. assert.Equal(t, c.PostForm("foo"), "bar")
  137. assert.Empty(t, c.Query("foo"))
  138. assert.Equal(t, c.DefaultPostForm("page", "0"), "11")
  139. assert.Equal(t, c.PostForm("page"), "11")
  140. assert.Equal(t, c.Query("page"), "")
  141. assert.Equal(t, c.PostForm("both"), "POST")
  142. assert.Equal(t, c.Query("both"), "GET")
  143. assert.Equal(t, c.Query("id"), "main")
  144. assert.Empty(t, c.PostForm("id"))
  145. assert.Equal(t, c.DefaultPostForm("NoKey", "nada"), "nada")
  146. assert.Empty(t, c.PostForm("NoKey"))
  147. assert.Empty(t, c.Query("NoKey"))
  148. c.Param("page")
  149. c.Query("page")
  150. c.PostForm("page")
  151. }
  152. // Tests that the response is serialized as JSON
  153. // and Content-Type is set to application/json
  154. func TestContextRenderJSON(t *testing.T) {
  155. c, w, _ := createTestContext()
  156. c.JSON(201, H{"foo": "bar"})
  157. assert.Equal(t, w.Code, 201)
  158. assert.Equal(t, w.Body.String(), "{\"foo\":\"bar\"}\n")
  159. assert.Equal(t, w.HeaderMap.Get("Content-Type"), "application/json; charset=utf-8")
  160. }
  161. // Tests that the response is serialized as JSON
  162. // and Content-Type is set to application/json
  163. func TestContextRenderIndentedJSON(t *testing.T) {
  164. c, w, _ := createTestContext()
  165. c.IndentedJSON(201, H{"foo": "bar", "bar": "foo", "nested": H{"foo": "bar"}})
  166. assert.Equal(t, w.Code, 201)
  167. assert.Equal(t, w.Body.String(), "{\n \"bar\": \"foo\",\n \"foo\": \"bar\",\n \"nested\": {\n \"foo\": \"bar\"\n }\n}")
  168. assert.Equal(t, w.HeaderMap.Get("Content-Type"), "application/json; charset=utf-8")
  169. }
  170. // Tests that the response executes the templates
  171. // and responds with Content-Type set to text/html
  172. func TestContextRenderHTML(t *testing.T) {
  173. c, w, router := createTestContext()
  174. templ := template.Must(template.New("t").Parse(`Hello {{.name}}`))
  175. router.SetHTMLTemplate(templ)
  176. c.HTML(201, "t", H{"name": "alexandernyquist"})
  177. assert.Equal(t, w.Code, 201)
  178. assert.Equal(t, w.Body.String(), "Hello alexandernyquist")
  179. assert.Equal(t, w.HeaderMap.Get("Content-Type"), "text/html; charset=utf-8")
  180. }
  181. // TestContextXML tests that the response is serialized as XML
  182. // and Content-Type is set to application/xml
  183. func TestContextRenderXML(t *testing.T) {
  184. c, w, _ := createTestContext()
  185. c.XML(201, H{"foo": "bar"})
  186. assert.Equal(t, w.Code, 201)
  187. assert.Equal(t, w.Body.String(), "<map><foo>bar</foo></map>")
  188. assert.Equal(t, w.HeaderMap.Get("Content-Type"), "application/xml; charset=utf-8")
  189. }
  190. // TestContextString tests that the response is returned
  191. // with Content-Type set to text/plain
  192. func TestContextRenderString(t *testing.T) {
  193. c, w, _ := createTestContext()
  194. c.String(201, "test %s %d", "string", 2)
  195. assert.Equal(t, w.Code, 201)
  196. assert.Equal(t, w.Body.String(), "test string 2")
  197. assert.Equal(t, w.HeaderMap.Get("Content-Type"), "text/plain; charset=utf-8")
  198. }
  199. // TestContextString tests that the response is returned
  200. // with Content-Type set to text/html
  201. func TestContextRenderHTMLString(t *testing.T) {
  202. c, w, _ := createTestContext()
  203. c.Header("Content-Type", "text/html; charset=utf-8")
  204. c.String(201, "<html>%s %d</html>", "string", 3)
  205. assert.Equal(t, w.Code, 201)
  206. assert.Equal(t, w.Body.String(), "<html>string 3</html>")
  207. assert.Equal(t, w.HeaderMap.Get("Content-Type"), "text/html; charset=utf-8")
  208. }
  209. // TestContextData tests that the response can be written from `bytesting`
  210. // with specified MIME type
  211. func TestContextRenderData(t *testing.T) {
  212. c, w, _ := createTestContext()
  213. c.Data(201, "text/csv", []byte(`foo,bar`))
  214. assert.Equal(t, w.Code, 201)
  215. assert.Equal(t, w.Body.String(), "foo,bar")
  216. assert.Equal(t, w.HeaderMap.Get("Content-Type"), "text/csv")
  217. }
  218. func TestContextRenderSSE(t *testing.T) {
  219. c, w, _ := createTestContext()
  220. c.SSEvent("float", 1.5)
  221. c.Render(-1, sse.Event{
  222. Id: "123",
  223. Data: "text",
  224. })
  225. c.SSEvent("chat", H{
  226. "foo": "bar",
  227. "bar": "foo",
  228. })
  229. assert.Equal(t, w.Body.String(), "event: float\ndata: 1.5\n\nid: 123\ndata: text\n\nevent: chat\ndata: {\"bar\":\"foo\",\"foo\":\"bar\"}\n\n")
  230. }
  231. func TestContextRenderFile(t *testing.T) {
  232. c, w, _ := createTestContext()
  233. c.Request, _ = http.NewRequest("GET", "/", nil)
  234. c.File("./gin.go")
  235. assert.Equal(t, w.Code, 200)
  236. assert.Contains(t, w.Body.String(), "func New() *Engine {")
  237. assert.Equal(t, w.HeaderMap.Get("Content-Type"), "text/plain; charset=utf-8")
  238. }
  239. func TestContextHeaders(t *testing.T) {
  240. c, _, _ := createTestContext()
  241. c.Header("Content-Type", "text/plain")
  242. c.Header("X-Custom", "value")
  243. assert.Equal(t, c.Writer.Header().Get("Content-Type"), "text/plain")
  244. assert.Equal(t, c.Writer.Header().Get("X-Custom"), "value")
  245. c.Header("Content-Type", "text/html")
  246. c.Header("X-Custom", "")
  247. assert.Equal(t, c.Writer.Header().Get("Content-Type"), "text/html")
  248. _, exist := c.Writer.Header()["X-Custom"]
  249. assert.False(t, exist)
  250. }
  251. // TODO
  252. func TestContextRenderRedirectWithRelativePath(t *testing.T) {
  253. c, w, _ := createTestContext()
  254. c.Request, _ = http.NewRequest("POST", "http://example.com", nil)
  255. assert.Panics(t, func() { c.Redirect(299, "/new_path") })
  256. assert.Panics(t, func() { c.Redirect(309, "/new_path") })
  257. c.Redirect(302, "/path")
  258. c.Writer.WriteHeaderNow()
  259. assert.Equal(t, w.Code, 302)
  260. assert.Equal(t, w.Header().Get("Location"), "/path")
  261. }
  262. func TestContextRenderRedirectWithAbsolutePath(t *testing.T) {
  263. c, w, _ := createTestContext()
  264. c.Request, _ = http.NewRequest("POST", "http://example.com", nil)
  265. c.Redirect(302, "http://google.com")
  266. c.Writer.WriteHeaderNow()
  267. assert.Equal(t, w.Code, 302)
  268. assert.Equal(t, w.Header().Get("Location"), "http://google.com")
  269. }
  270. func TestContextNegotiationFormat(t *testing.T) {
  271. c, _, _ := createTestContext()
  272. c.Request, _ = http.NewRequest("POST", "", nil)
  273. assert.Panics(t, func() { c.NegotiateFormat() })
  274. assert.Equal(t, c.NegotiateFormat(MIMEJSON, MIMEXML), MIMEJSON)
  275. assert.Equal(t, c.NegotiateFormat(MIMEHTML, MIMEJSON), MIMEHTML)
  276. }
  277. func TestContextNegotiationFormatWithAccept(t *testing.T) {
  278. c, _, _ := createTestContext()
  279. c.Request, _ = http.NewRequest("POST", "/", nil)
  280. c.Request.Header.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
  281. assert.Equal(t, c.NegotiateFormat(MIMEJSON, MIMEXML), MIMEXML)
  282. assert.Equal(t, c.NegotiateFormat(MIMEXML, MIMEHTML), MIMEHTML)
  283. assert.Equal(t, c.NegotiateFormat(MIMEJSON), "")
  284. }
  285. func TestContextNegotiationFormatCustum(t *testing.T) {
  286. c, _, _ := createTestContext()
  287. c.Request, _ = http.NewRequest("POST", "/", nil)
  288. c.Request.Header.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
  289. c.Accepted = nil
  290. c.SetAccepted(MIMEJSON, MIMEXML)
  291. assert.Equal(t, c.NegotiateFormat(MIMEJSON, MIMEXML), MIMEJSON)
  292. assert.Equal(t, c.NegotiateFormat(MIMEXML, MIMEHTML), MIMEXML)
  293. assert.Equal(t, c.NegotiateFormat(MIMEJSON), MIMEJSON)
  294. }
  295. // TestContextData tests that the response can be written from `bytesting`
  296. // with specified MIME type
  297. func TestContextAbortWithStatus(t *testing.T) {
  298. c, w, _ := createTestContext()
  299. c.index = 4
  300. c.AbortWithStatus(401)
  301. c.Writer.WriteHeaderNow()
  302. assert.Equal(t, c.index, AbortIndex)
  303. assert.Equal(t, c.Writer.Status(), 401)
  304. assert.Equal(t, w.Code, 401)
  305. assert.True(t, c.IsAborted())
  306. }
  307. func TestContextError(t *testing.T) {
  308. c, _, _ := createTestContext()
  309. assert.Empty(t, c.Errors)
  310. c.Error(errors.New("first error"))
  311. assert.Len(t, c.Errors, 1)
  312. assert.Equal(t, c.Errors.String(), "Error #01: first error\n")
  313. c.Error(&Error{
  314. Err: errors.New("second error"),
  315. Meta: "some data 2",
  316. Type: ErrorTypePublic,
  317. })
  318. assert.Len(t, c.Errors, 2)
  319. assert.Equal(t, c.Errors[0].Err, errors.New("first error"))
  320. assert.Nil(t, c.Errors[0].Meta)
  321. assert.Equal(t, c.Errors[0].Type, ErrorTypePrivate)
  322. assert.Equal(t, c.Errors[1].Err, errors.New("second error"))
  323. assert.Equal(t, c.Errors[1].Meta, "some data 2")
  324. assert.Equal(t, c.Errors[1].Type, ErrorTypePublic)
  325. assert.Equal(t, c.Errors.Last(), c.Errors[1])
  326. }
  327. func TestContextTypedError(t *testing.T) {
  328. c, _, _ := createTestContext()
  329. c.Error(errors.New("externo 0")).SetType(ErrorTypePublic)
  330. c.Error(errors.New("interno 0")).SetType(ErrorTypePrivate)
  331. for _, err := range c.Errors.ByType(ErrorTypePublic) {
  332. assert.Equal(t, err.Type, ErrorTypePublic)
  333. }
  334. for _, err := range c.Errors.ByType(ErrorTypePrivate) {
  335. assert.Equal(t, err.Type, ErrorTypePrivate)
  336. }
  337. assert.Equal(t, c.Errors.Errors(), []string{"externo 0", "interno 0"})
  338. }
  339. func TestContextAbortWithError(t *testing.T) {
  340. c, w, _ := createTestContext()
  341. c.AbortWithError(401, errors.New("bad input")).SetMeta("some input")
  342. c.Writer.WriteHeaderNow()
  343. assert.Equal(t, w.Code, 401)
  344. assert.Equal(t, c.index, AbortIndex)
  345. assert.True(t, c.IsAborted())
  346. }
  347. func TestContextClientIP(t *testing.T) {
  348. c, _, _ := createTestContext()
  349. c.Request, _ = http.NewRequest("POST", "/", nil)
  350. c.Request.Header.Set("X-Real-IP", " 10.10.10.10 ")
  351. c.Request.Header.Set("X-Forwarded-For", " 20.20.20.20 , 30.30.30.30")
  352. c.Request.RemoteAddr = " 40.40.40.40 "
  353. assert.Equal(t, c.ClientIP(), "10.10.10.10")
  354. c.Request.Header.Del("X-Real-IP")
  355. assert.Equal(t, c.ClientIP(), "20.20.20.20")
  356. c.Request.Header.Set("X-Forwarded-For", "30.30.30.30")
  357. assert.Equal(t, c.ClientIP(), "30.30.30.30")
  358. c.Request.Header.Del("X-Forwarded-For")
  359. assert.Equal(t, c.ClientIP(), "40.40.40.40")
  360. }
  361. func TestContextContentType(t *testing.T) {
  362. c, _, _ := createTestContext()
  363. c.Request, _ = http.NewRequest("POST", "/", nil)
  364. c.Request.Header.Set("Content-Type", "application/json; charset=utf-8")
  365. assert.Equal(t, c.ContentType(), "application/json")
  366. }
  367. func TestContextAutoBind(t *testing.T) {
  368. c, w, _ := createTestContext()
  369. c.Request, _ = http.NewRequest("POST", "/", bytes.NewBufferString("{\"foo\":\"bar\", \"bar\":\"foo\"}"))
  370. c.Request.Header.Add("Content-Type", MIMEJSON)
  371. var obj struct {
  372. Foo string `json:"foo"`
  373. Bar string `json:"bar"`
  374. }
  375. assert.NoError(t, c.Bind(&obj))
  376. assert.Equal(t, obj.Bar, "foo")
  377. assert.Equal(t, obj.Foo, "bar")
  378. assert.Equal(t, w.Body.Len(), 0)
  379. }
  380. func TestContextMultipartPostFormAutoBind(t *testing.T) {
  381. c, w, _ := createTestContext()
  382. var obj struct {
  383. Foo string `form:"foo"`
  384. Bar string `form:"bar"`
  385. }
  386. body, header, err := createMultipartForm()
  387. if err != nil {
  388. t.Error(err)
  389. }
  390. c.Request, _ = http.NewRequest("POST", "/", body)
  391. c.Request.Header.Add("Content-Type", header)
  392. assert.NoError(t, c.Bind(&obj))
  393. assert.Equal(t, obj.Bar, "foo")
  394. assert.Equal(t, obj.Foo, "bar")
  395. assert.Equal(t, w.Body.Len(), 0)
  396. }
  397. func TestContextBadAutoBind(t *testing.T) {
  398. c, w, _ := createTestContext()
  399. c.Request, _ = http.NewRequest("POST", "http://example.com", bytes.NewBufferString("\"foo\":\"bar\", \"bar\":\"foo\"}"))
  400. c.Request.Header.Add("Content-Type", MIMEJSON)
  401. var obj struct {
  402. Foo string `json:"foo"`
  403. Bar string `json:"bar"`
  404. }
  405. assert.False(t, c.IsAborted())
  406. assert.Error(t, c.Bind(&obj))
  407. c.Writer.WriteHeaderNow()
  408. assert.Empty(t, obj.Bar)
  409. assert.Empty(t, obj.Foo)
  410. assert.Equal(t, w.Code, 400)
  411. assert.True(t, c.IsAborted())
  412. }
  413. func TestContextBindWith(t *testing.T) {
  414. c, w, _ := createTestContext()
  415. c.Request, _ = http.NewRequest("POST", "/", bytes.NewBufferString("{\"foo\":\"bar\", \"bar\":\"foo\"}"))
  416. c.Request.Header.Add("Content-Type", MIMEXML)
  417. var obj struct {
  418. Foo string `json:"foo"`
  419. Bar string `json:"bar"`
  420. }
  421. assert.NoError(t, c.BindWith(&obj, binding.JSON))
  422. assert.Equal(t, obj.Bar, "foo")
  423. assert.Equal(t, obj.Foo, "bar")
  424. assert.Equal(t, w.Body.Len(), 0)
  425. }
  426. func TestContextMultipartBindWith(t *testing.T) {
  427. c, w, _ := createTestContext()
  428. var obj struct {
  429. Foo string `form:"foo"`
  430. Bar string `form:"bar"`
  431. }
  432. body, header, err := createMultipartForm()
  433. if err != nil {
  434. t.Error(err)
  435. }
  436. c.Request, _ = http.NewRequest("POST", "/", body)
  437. c.Request.Header.Add("Content-Type", header)
  438. assert.NoError(t, c.BindWith(&obj, binding.MultipartForm))
  439. assert.Equal(t, obj.Bar, "foo")
  440. assert.Equal(t, obj.Foo, "bar")
  441. assert.Equal(t, w.Body.Len(), 0)
  442. }
  443. func TestContextGolangContext(t *testing.T) {
  444. c, _, _ := createTestContext()
  445. c.Request, _ = http.NewRequest("POST", "/", bytes.NewBufferString("{\"foo\":\"bar\", \"bar\":\"foo\"}"))
  446. assert.NoError(t, c.Err())
  447. assert.Nil(t, c.Done())
  448. ti, ok := c.Deadline()
  449. assert.Equal(t, ti, time.Time{})
  450. assert.False(t, ok)
  451. assert.Equal(t, c.Value(0), c.Request)
  452. assert.Nil(t, c.Value("foo"))
  453. c.Set("foo", "bar")
  454. assert.Equal(t, c.Value("foo"), "bar")
  455. assert.Nil(t, c.Value(1))
  456. }