Micro & pluggable web framework for Go
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1009 lines
20KB

  1. // Copyright 2015 The Tango Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package tango
  5. import (
  6. "bytes"
  7. "fmt"
  8. "net/http"
  9. "net/http/httptest"
  10. "testing"
  11. )
  12. type ParamAction struct {
  13. Params
  14. }
  15. func (p *ParamAction) Get() string {
  16. return p.Params.Get(":name")
  17. }
  18. func TestParams1(t *testing.T) {
  19. buff := bytes.NewBufferString("")
  20. recorder := httptest.NewRecorder()
  21. recorder.Body = buff
  22. o := Classic()
  23. o.Get("/:name", new(ParamAction))
  24. req, err := http.NewRequest("GET", "http://localhost:8000/foobar", nil)
  25. if err != nil {
  26. t.Error(err)
  27. }
  28. o.ServeHTTP(recorder, req)
  29. expect(t, recorder.Code, http.StatusOK)
  30. refute(t, len(buff.String()), 0)
  31. expect(t, buff.String(), "foobar")
  32. }
  33. type Param2Action struct {
  34. Params
  35. }
  36. func (p *Param2Action) Get() string {
  37. return p.Params.Get(":name")
  38. }
  39. func TestParams2(t *testing.T) {
  40. buff := bytes.NewBufferString("")
  41. recorder := httptest.NewRecorder()
  42. recorder.Body = buff
  43. o := Classic()
  44. o.Get("/(:name.*)", new(Param2Action))
  45. req, err := http.NewRequest("GET", "http://localhost:8000/foobar", nil)
  46. if err != nil {
  47. t.Error(err)
  48. }
  49. o.ServeHTTP(recorder, req)
  50. expect(t, recorder.Code, http.StatusOK)
  51. refute(t, len(buff.String()), 0)
  52. expect(t, buff.String(), "foobar")
  53. }
  54. type Param3Action struct {
  55. Ctx
  56. }
  57. func (p *Param3Action) Get() string {
  58. fmt.Println(p.params)
  59. p.Params().Set(":name", "name")
  60. fmt.Println(p.params)
  61. return p.Params().Get(":name")
  62. }
  63. func TestParams3(t *testing.T) {
  64. buff := bytes.NewBufferString("")
  65. recorder := httptest.NewRecorder()
  66. recorder.Body = buff
  67. o := Classic()
  68. o.Get("/(:name.*)", new(Param3Action))
  69. req, err := http.NewRequest("GET", "http://localhost:8000/foobar", nil)
  70. if err != nil {
  71. t.Error(err)
  72. }
  73. o.ServeHTTP(recorder, req)
  74. expect(t, recorder.Code, http.StatusOK)
  75. refute(t, len(buff.String()), 0)
  76. expect(t, buff.String(), "name")
  77. }
  78. type Param4Action struct {
  79. Params
  80. }
  81. func (p *Param4Action) Get() string {
  82. fmt.Println(p.Params)
  83. p.Params.Set(":name", "name")
  84. fmt.Println(p.Params)
  85. return p.Params.Get(":name")
  86. }
  87. func TestParams4(t *testing.T) {
  88. buff := bytes.NewBufferString("")
  89. recorder := httptest.NewRecorder()
  90. recorder.Body = buff
  91. o := Classic()
  92. o.Get("/(:name.*)", new(Param4Action))
  93. req, err := http.NewRequest("GET", "http://localhost:8000/foobar", nil)
  94. if err != nil {
  95. t.Error(err)
  96. }
  97. o.ServeHTTP(recorder, req)
  98. expect(t, recorder.Code, http.StatusOK)
  99. refute(t, len(buff.String()), 0)
  100. expect(t, buff.String(), "name")
  101. }
  102. func TestParams5(t *testing.T) {
  103. buff := bytes.NewBufferString("")
  104. recorder := httptest.NewRecorder()
  105. recorder.Body = buff
  106. o := Classic()
  107. o.Get("/", func(ctx *Context) {
  108. ctx.Params().Set(":name", "test")
  109. ctx.Write([]byte(ctx.Params().Get(":name")))
  110. })
  111. req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
  112. if err != nil {
  113. t.Error(err)
  114. }
  115. o.ServeHTTP(recorder, req)
  116. expect(t, recorder.Code, http.StatusOK)
  117. expect(t, buff.String(), "test")
  118. }
  119. func TestParams6(t *testing.T) {
  120. buff := bytes.NewBufferString("")
  121. recorder := httptest.NewRecorder()
  122. recorder.Body = buff
  123. o := Classic()
  124. o.Get("/", func(ctx *Context) {
  125. ctx.Write([]byte(ctx.Params().Get(":name")))
  126. })
  127. req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
  128. if err != nil {
  129. t.Error(err)
  130. }
  131. o.ServeHTTP(recorder, req)
  132. expect(t, recorder.Code, http.StatusOK)
  133. expect(t, buff.String(), "")
  134. }
  135. type Param5Action struct {
  136. Params
  137. }
  138. func (p *Param5Action) Get() string {
  139. i, _ := p.Params.Int(":name")
  140. return fmt.Sprintf("%d", i)
  141. }
  142. func TestParams7(t *testing.T) {
  143. buff := bytes.NewBufferString("")
  144. recorder := httptest.NewRecorder()
  145. recorder.Body = buff
  146. o := Classic()
  147. o.Get("/(:name[0-9]+)", new(Param5Action))
  148. req, err := http.NewRequest("GET", "http://localhost:8000/1", nil)
  149. if err != nil {
  150. t.Error(err)
  151. }
  152. o.ServeHTTP(recorder, req)
  153. expect(t, recorder.Code, http.StatusOK)
  154. refute(t, len(buff.String()), 0)
  155. expect(t, buff.String(), "1")
  156. }
  157. type Param6Action struct {
  158. Params
  159. }
  160. func (p *Param6Action) Get() string {
  161. i, _ := p.Params.Int32(":name")
  162. return fmt.Sprintf("%d", i)
  163. }
  164. func TestParams8(t *testing.T) {
  165. buff := bytes.NewBufferString("")
  166. recorder := httptest.NewRecorder()
  167. recorder.Body = buff
  168. o := Classic()
  169. o.Get("/(:name[0-9]+)", new(Param6Action))
  170. req, err := http.NewRequest("GET", "http://localhost:8000/1", nil)
  171. if err != nil {
  172. t.Error(err)
  173. }
  174. o.ServeHTTP(recorder, req)
  175. expect(t, recorder.Code, http.StatusOK)
  176. refute(t, len(buff.String()), 0)
  177. expect(t, buff.String(), "1")
  178. }
  179. type Param7Action struct {
  180. Params
  181. }
  182. func (p *Param7Action) Get() string {
  183. i, _ := p.Params.Int64(":name")
  184. return fmt.Sprintf("%d", i)
  185. }
  186. func TestParams9(t *testing.T) {
  187. buff := bytes.NewBufferString("")
  188. recorder := httptest.NewRecorder()
  189. recorder.Body = buff
  190. o := Classic()
  191. o.Get("/(:name[0-9]+)", new(Param7Action))
  192. req, err := http.NewRequest("GET", "http://localhost:8000/1", nil)
  193. if err != nil {
  194. t.Error(err)
  195. }
  196. o.ServeHTTP(recorder, req)
  197. expect(t, recorder.Code, http.StatusOK)
  198. refute(t, len(buff.String()), 0)
  199. expect(t, buff.String(), "1")
  200. }
  201. type Param8Action struct {
  202. Params
  203. }
  204. func (p *Param8Action) Get() string {
  205. i, _ := p.Params.Float32(":name")
  206. return fmt.Sprintf("%.2f", i)
  207. }
  208. func TestParams10(t *testing.T) {
  209. buff := bytes.NewBufferString("")
  210. recorder := httptest.NewRecorder()
  211. recorder.Body = buff
  212. o := Classic()
  213. o.Get("/(:name[0-9]+)", new(Param8Action))
  214. req, err := http.NewRequest("GET", "http://localhost:8000/1", nil)
  215. if err != nil {
  216. t.Error(err)
  217. }
  218. o.ServeHTTP(recorder, req)
  219. expect(t, recorder.Code, http.StatusOK)
  220. refute(t, len(buff.String()), 0)
  221. expect(t, buff.String(), "1.00")
  222. }
  223. type Param9Action struct {
  224. Params
  225. }
  226. func (p *Param9Action) Get() string {
  227. i, _ := p.Params.Float64(":name")
  228. return fmt.Sprintf("%.2f", i)
  229. }
  230. func TestParams11(t *testing.T) {
  231. buff := bytes.NewBufferString("")
  232. recorder := httptest.NewRecorder()
  233. recorder.Body = buff
  234. o := Classic()
  235. o.Get("/(:name[0-9]+)", new(Param9Action))
  236. req, err := http.NewRequest("GET", "http://localhost:8000/1", nil)
  237. if err != nil {
  238. t.Error(err)
  239. }
  240. o.ServeHTTP(recorder, req)
  241. expect(t, recorder.Code, http.StatusOK)
  242. refute(t, len(buff.String()), 0)
  243. expect(t, buff.String(), "1.00")
  244. }
  245. type Param10Action struct {
  246. Params
  247. }
  248. func (p *Param10Action) Get() string {
  249. i, _ := p.Params.Uint(":name")
  250. return fmt.Sprintf("%d", i)
  251. }
  252. func TestParams12(t *testing.T) {
  253. buff := bytes.NewBufferString("")
  254. recorder := httptest.NewRecorder()
  255. recorder.Body = buff
  256. o := Classic()
  257. o.Get("/(:name[0-9]+)", new(Param10Action))
  258. req, err := http.NewRequest("GET", "http://localhost:8000/1", nil)
  259. if err != nil {
  260. t.Error(err)
  261. }
  262. o.ServeHTTP(recorder, req)
  263. expect(t, recorder.Code, http.StatusOK)
  264. refute(t, len(buff.String()), 0)
  265. expect(t, buff.String(), "1")
  266. }
  267. type Param11Action struct {
  268. Params
  269. }
  270. func (p *Param11Action) Get() string {
  271. i, _ := p.Params.Uint32(":name")
  272. return fmt.Sprintf("%d", i)
  273. }
  274. func TestParams13(t *testing.T) {
  275. buff := bytes.NewBufferString("")
  276. recorder := httptest.NewRecorder()
  277. recorder.Body = buff
  278. o := Classic()
  279. o.Get("/(:name[0-9]+)", new(Param11Action))
  280. req, err := http.NewRequest("GET", "http://localhost:8000/1", nil)
  281. if err != nil {
  282. t.Error(err)
  283. }
  284. o.ServeHTTP(recorder, req)
  285. expect(t, recorder.Code, http.StatusOK)
  286. refute(t, len(buff.String()), 0)
  287. expect(t, buff.String(), "1")
  288. }
  289. type Param12Action struct {
  290. Params
  291. }
  292. func (p *Param12Action) Get() string {
  293. i, _ := p.Params.Uint64(":name")
  294. return fmt.Sprintf("%d", i)
  295. }
  296. func TestParams14(t *testing.T) {
  297. buff := bytes.NewBufferString("")
  298. recorder := httptest.NewRecorder()
  299. recorder.Body = buff
  300. o := Classic()
  301. o.Get("/(:name[0-9]+)", new(Param12Action))
  302. req, err := http.NewRequest("GET", "http://localhost:8000/1", nil)
  303. if err != nil {
  304. t.Error(err)
  305. }
  306. o.ServeHTTP(recorder, req)
  307. expect(t, recorder.Code, http.StatusOK)
  308. refute(t, len(buff.String()), 0)
  309. expect(t, buff.String(), "1")
  310. }
  311. type Param13Action struct {
  312. Params
  313. }
  314. func (p *Param13Action) Get() string {
  315. i, _ := p.Params.Bool(":name")
  316. return fmt.Sprintf("%v", i)
  317. }
  318. func TestParams15(t *testing.T) {
  319. buff := bytes.NewBufferString("")
  320. recorder := httptest.NewRecorder()
  321. recorder.Body = buff
  322. o := Classic()
  323. o.Get("/(:name[0-9]+)", new(Param13Action))
  324. req, err := http.NewRequest("GET", "http://localhost:8000/1", nil)
  325. if err != nil {
  326. t.Error(err)
  327. }
  328. o.ServeHTTP(recorder, req)
  329. expect(t, recorder.Code, http.StatusOK)
  330. refute(t, len(buff.String()), 0)
  331. expect(t, buff.String(), "true")
  332. }
  333. type Param14Action struct {
  334. Params
  335. }
  336. func (p *Param14Action) Get() string {
  337. i := p.Params.MustInt(":name")
  338. return fmt.Sprintf("%d", i)
  339. }
  340. func TestParams16(t *testing.T) {
  341. buff := bytes.NewBufferString("")
  342. recorder := httptest.NewRecorder()
  343. recorder.Body = buff
  344. o := Classic()
  345. o.Get("/(:name[0-9]+)", new(Param14Action))
  346. req, err := http.NewRequest("GET", "http://localhost:8000/1", nil)
  347. if err != nil {
  348. t.Error(err)
  349. }
  350. o.ServeHTTP(recorder, req)
  351. expect(t, recorder.Code, http.StatusOK)
  352. refute(t, len(buff.String()), 0)
  353. expect(t, buff.String(), "1")
  354. }
  355. type Param15Action struct {
  356. Params
  357. }
  358. func (p *Param15Action) Get() string {
  359. i := p.Params.MustInt32(":name")
  360. return fmt.Sprintf("%d", i)
  361. }
  362. func TestParams17(t *testing.T) {
  363. buff := bytes.NewBufferString("")
  364. recorder := httptest.NewRecorder()
  365. recorder.Body = buff
  366. o := Classic()
  367. o.Get("/(:name[0-9]+)", new(Param15Action))
  368. req, err := http.NewRequest("GET", "http://localhost:8000/1", nil)
  369. if err != nil {
  370. t.Error(err)
  371. }
  372. o.ServeHTTP(recorder, req)
  373. expect(t, recorder.Code, http.StatusOK)
  374. refute(t, len(buff.String()), 0)
  375. expect(t, buff.String(), "1")
  376. }
  377. type Param16Action struct {
  378. Params
  379. }
  380. func (p *Param16Action) Get() string {
  381. i := p.Params.MustInt64(":name")
  382. return fmt.Sprintf("%d", i)
  383. }
  384. func TestParams18(t *testing.T) {
  385. buff := bytes.NewBufferString("")
  386. recorder := httptest.NewRecorder()
  387. recorder.Body = buff
  388. o := Classic()
  389. o.Get("/(:name[0-9]+)", new(Param16Action))
  390. req, err := http.NewRequest("GET", "http://localhost:8000/1", nil)
  391. if err != nil {
  392. t.Error(err)
  393. }
  394. o.ServeHTTP(recorder, req)
  395. expect(t, recorder.Code, http.StatusOK)
  396. refute(t, len(buff.String()), 0)
  397. expect(t, buff.String(), "1")
  398. }
  399. type Param17Action struct {
  400. Params
  401. }
  402. func (p *Param17Action) Get() string {
  403. i := p.Params.MustFloat32(":name")
  404. return fmt.Sprintf("%.2f", i)
  405. }
  406. func TestParams19(t *testing.T) {
  407. buff := bytes.NewBufferString("")
  408. recorder := httptest.NewRecorder()
  409. recorder.Body = buff
  410. o := Classic()
  411. o.Get("/(:name[0-9]+)", new(Param17Action))
  412. req, err := http.NewRequest("GET", "http://localhost:8000/1", nil)
  413. if err != nil {
  414. t.Error(err)
  415. }
  416. o.ServeHTTP(recorder, req)
  417. expect(t, recorder.Code, http.StatusOK)
  418. refute(t, len(buff.String()), 0)
  419. expect(t, buff.String(), "1.00")
  420. }
  421. type Param18Action struct {
  422. Params
  423. }
  424. func (p *Param18Action) Get() string {
  425. i := p.Params.MustFloat64(":name")
  426. return fmt.Sprintf("%.2f", i)
  427. }
  428. func TestParams20(t *testing.T) {
  429. buff := bytes.NewBufferString("")
  430. recorder := httptest.NewRecorder()
  431. recorder.Body = buff
  432. o := Classic()
  433. o.Get("/(:name[0-9]+)", new(Param18Action))
  434. req, err := http.NewRequest("GET", "http://localhost:8000/1", nil)
  435. if err != nil {
  436. t.Error(err)
  437. }
  438. o.ServeHTTP(recorder, req)
  439. expect(t, recorder.Code, http.StatusOK)
  440. refute(t, len(buff.String()), 0)
  441. expect(t, buff.String(), "1.00")
  442. }
  443. type Param19Action struct {
  444. Params
  445. }
  446. func (p *Param19Action) Get() string {
  447. i := p.Params.MustUint(":name")
  448. return fmt.Sprintf("%d", i)
  449. }
  450. func TestParams21(t *testing.T) {
  451. buff := bytes.NewBufferString("")
  452. recorder := httptest.NewRecorder()
  453. recorder.Body = buff
  454. o := Classic()
  455. o.Get("/(:name[0-9]+)", new(Param19Action))
  456. req, err := http.NewRequest("GET", "http://localhost:8000/1", nil)
  457. if err != nil {
  458. t.Error(err)
  459. }
  460. o.ServeHTTP(recorder, req)
  461. expect(t, recorder.Code, http.StatusOK)
  462. refute(t, len(buff.String()), 0)
  463. expect(t, buff.String(), "1")
  464. }
  465. type Param20Action struct {
  466. Params
  467. }
  468. func (p *Param20Action) Get() string {
  469. i := p.Params.MustUint32(":name")
  470. return fmt.Sprintf("%d", i)
  471. }
  472. func TestParams22(t *testing.T) {
  473. buff := bytes.NewBufferString("")
  474. recorder := httptest.NewRecorder()
  475. recorder.Body = buff
  476. o := Classic()
  477. o.Get("/(:name[0-9]+)", new(Param20Action))
  478. req, err := http.NewRequest("GET", "http://localhost:8000/1", nil)
  479. if err != nil {
  480. t.Error(err)
  481. }
  482. o.ServeHTTP(recorder, req)
  483. expect(t, recorder.Code, http.StatusOK)
  484. refute(t, len(buff.String()), 0)
  485. expect(t, buff.String(), "1")
  486. }
  487. type Param21Action struct {
  488. Params
  489. }
  490. func (p *Param21Action) Get() string {
  491. i := p.Params.MustUint64(":name")
  492. return fmt.Sprintf("%d", i)
  493. }
  494. func TestParams23(t *testing.T) {
  495. buff := bytes.NewBufferString("")
  496. recorder := httptest.NewRecorder()
  497. recorder.Body = buff
  498. o := Classic()
  499. o.Get("/(:name[0-9]+)", new(Param21Action))
  500. req, err := http.NewRequest("GET", "http://localhost:8000/1", nil)
  501. if err != nil {
  502. t.Error(err)
  503. }
  504. o.ServeHTTP(recorder, req)
  505. expect(t, recorder.Code, http.StatusOK)
  506. refute(t, len(buff.String()), 0)
  507. expect(t, buff.String(), "1")
  508. }
  509. type Param22Action struct {
  510. Params
  511. }
  512. func (p *Param22Action) Get() string {
  513. i := p.Params.MustBool(":name")
  514. return fmt.Sprintf("%v", i)
  515. }
  516. func TestParams24(t *testing.T) {
  517. buff := bytes.NewBufferString("")
  518. recorder := httptest.NewRecorder()
  519. recorder.Body = buff
  520. o := Classic()
  521. o.Get("/(:name[0-9]+)", new(Param22Action))
  522. req, err := http.NewRequest("GET", "http://localhost:8000/1", nil)
  523. if err != nil {
  524. t.Error(err)
  525. }
  526. o.ServeHTTP(recorder, req)
  527. expect(t, recorder.Code, http.StatusOK)
  528. refute(t, len(buff.String()), 0)
  529. expect(t, buff.String(), "true")
  530. }
  531. type Param23Action struct {
  532. Params
  533. }
  534. func (p *Param23Action) Get() string {
  535. i, _ := p.Params.String(":name")
  536. return fmt.Sprintf("%v", i)
  537. }
  538. func TestParams25(t *testing.T) {
  539. buff := bytes.NewBufferString("")
  540. recorder := httptest.NewRecorder()
  541. recorder.Body = buff
  542. o := Classic()
  543. o.Get("/(:name[0-9]+)", new(Param23Action))
  544. req, err := http.NewRequest("GET", "http://localhost:8000/1", nil)
  545. if err != nil {
  546. t.Error(err)
  547. }
  548. o.ServeHTTP(recorder, req)
  549. expect(t, recorder.Code, http.StatusOK)
  550. refute(t, len(buff.String()), 0)
  551. expect(t, buff.String(), "1")
  552. }
  553. type Param24Action struct {
  554. Params
  555. }
  556. func (p *Param24Action) Get() string {
  557. i := p.Params.MustString(":name")
  558. return fmt.Sprintf("%v", i)
  559. }
  560. func TestParams26(t *testing.T) {
  561. buff := bytes.NewBufferString("")
  562. recorder := httptest.NewRecorder()
  563. recorder.Body = buff
  564. o := Classic()
  565. o.Get("/(:name[0-9]+)", new(Param24Action))
  566. req, err := http.NewRequest("GET", "http://localhost:8000/1", nil)
  567. if err != nil {
  568. t.Error(err)
  569. }
  570. o.ServeHTTP(recorder, req)
  571. expect(t, recorder.Code, http.StatusOK)
  572. refute(t, len(buff.String()), 0)
  573. expect(t, buff.String(), "1")
  574. }
  575. type Param25Action struct {
  576. Ctx
  577. }
  578. func (p *Param25Action) Get() string {
  579. i := p.ParamInt(":name")
  580. return fmt.Sprintf("%d", i)
  581. }
  582. func TestParams27(t *testing.T) {
  583. buff := bytes.NewBufferString("")
  584. recorder := httptest.NewRecorder()
  585. recorder.Body = buff
  586. o := Classic()
  587. o.Get("/(:name[0-9]+)", new(Param25Action))
  588. req, err := http.NewRequest("GET", "http://localhost:8000/1", nil)
  589. if err != nil {
  590. t.Error(err)
  591. }
  592. o.ServeHTTP(recorder, req)
  593. expect(t, recorder.Code, http.StatusOK)
  594. refute(t, len(buff.String()), 0)
  595. expect(t, buff.String(), "1")
  596. }
  597. type Param26Action struct {
  598. Ctx
  599. }
  600. func (p *Param26Action) Get() string {
  601. i := p.ParamInt32(":name")
  602. return fmt.Sprintf("%d", i)
  603. }
  604. func TestParams28(t *testing.T) {
  605. buff := bytes.NewBufferString("")
  606. recorder := httptest.NewRecorder()
  607. recorder.Body = buff
  608. o := Classic()
  609. o.Get("/(:name[0-9]+)", new(Param26Action))
  610. req, err := http.NewRequest("GET", "http://localhost:8000/1", nil)
  611. if err != nil {
  612. t.Error(err)
  613. }
  614. o.ServeHTTP(recorder, req)
  615. expect(t, recorder.Code, http.StatusOK)
  616. refute(t, len(buff.String()), 0)
  617. expect(t, buff.String(), "1")
  618. }
  619. type Param27Action struct {
  620. Ctx
  621. }
  622. func (p *Param27Action) Get() string {
  623. i := p.ParamInt64(":name")
  624. return fmt.Sprintf("%d", i)
  625. }
  626. func TestParams29(t *testing.T) {
  627. buff := bytes.NewBufferString("")
  628. recorder := httptest.NewRecorder()
  629. recorder.Body = buff
  630. o := Classic()
  631. o.Get("/(:name[0-9]+)", new(Param27Action))
  632. req, err := http.NewRequest("GET", "http://localhost:8000/1", nil)
  633. if err != nil {
  634. t.Error(err)
  635. }
  636. o.ServeHTTP(recorder, req)
  637. expect(t, recorder.Code, http.StatusOK)
  638. refute(t, len(buff.String()), 0)
  639. expect(t, buff.String(), "1")
  640. }
  641. type Param28Action struct {
  642. Ctx
  643. }
  644. func (p *Param28Action) Get() string {
  645. i := p.ParamFloat32(":name")
  646. return fmt.Sprintf("%.2f", i)
  647. }
  648. func TestParams30(t *testing.T) {
  649. buff := bytes.NewBufferString("")
  650. recorder := httptest.NewRecorder()
  651. recorder.Body = buff
  652. o := Classic()
  653. o.Get("/(:name[0-9]+)", new(Param28Action))
  654. req, err := http.NewRequest("GET", "http://localhost:8000/1", nil)
  655. if err != nil {
  656. t.Error(err)
  657. }
  658. o.ServeHTTP(recorder, req)
  659. expect(t, recorder.Code, http.StatusOK)
  660. refute(t, len(buff.String()), 0)
  661. expect(t, buff.String(), "1.00")
  662. }
  663. type Param29Action struct {
  664. Ctx
  665. }
  666. func (p *Param29Action) Get() string {
  667. i := p.ParamFloat64(":name")
  668. return fmt.Sprintf("%.2f", i)
  669. }
  670. func TestParams31(t *testing.T) {
  671. buff := bytes.NewBufferString("")
  672. recorder := httptest.NewRecorder()
  673. recorder.Body = buff
  674. o := Classic()
  675. o.Get("/(:name[0-9]+)", new(Param29Action))
  676. req, err := http.NewRequest("GET", "http://localhost:8000/1", nil)
  677. if err != nil {
  678. t.Error(err)
  679. }
  680. o.ServeHTTP(recorder, req)
  681. expect(t, recorder.Code, http.StatusOK)
  682. refute(t, len(buff.String()), 0)
  683. expect(t, buff.String(), "1.00")
  684. }
  685. type Param30Action struct {
  686. Ctx
  687. }
  688. func (p *Param30Action) Get() string {
  689. i := p.ParamUint(":name")
  690. return fmt.Sprintf("%d", i)
  691. }
  692. func TestParams32(t *testing.T) {
  693. buff := bytes.NewBufferString("")
  694. recorder := httptest.NewRecorder()
  695. recorder.Body = buff
  696. o := Classic()
  697. o.Get("/(:name[0-9]+)", new(Param30Action))
  698. req, err := http.NewRequest("GET", "http://localhost:8000/1", nil)
  699. if err != nil {
  700. t.Error(err)
  701. }
  702. o.ServeHTTP(recorder, req)
  703. expect(t, recorder.Code, http.StatusOK)
  704. refute(t, len(buff.String()), 0)
  705. expect(t, buff.String(), "1")
  706. }
  707. type Param31Action struct {
  708. Ctx
  709. }
  710. func (p *Param31Action) Get() string {
  711. i := p.ParamUint32(":name")
  712. return fmt.Sprintf("%d", i)
  713. }
  714. func TestParams33(t *testing.T) {
  715. buff := bytes.NewBufferString("")
  716. recorder := httptest.NewRecorder()
  717. recorder.Body = buff
  718. o := Classic()
  719. o.Get("/(:name[0-9]+)", new(Param31Action))
  720. req, err := http.NewRequest("GET", "http://localhost:8000/1", nil)
  721. if err != nil {
  722. t.Error(err)
  723. }
  724. o.ServeHTTP(recorder, req)
  725. expect(t, recorder.Code, http.StatusOK)
  726. refute(t, len(buff.String()), 0)
  727. expect(t, buff.String(), "1")
  728. }
  729. type Param32Action struct {
  730. Ctx
  731. }
  732. func (p *Param32Action) Get() string {
  733. i := p.ParamUint64(":name")
  734. return fmt.Sprintf("%d", i)
  735. }
  736. func TestParams34(t *testing.T) {
  737. buff := bytes.NewBufferString("")
  738. recorder := httptest.NewRecorder()
  739. recorder.Body = buff
  740. o := Classic()
  741. o.Get("/(:name[0-9]+)", new(Param32Action))
  742. req, err := http.NewRequest("GET", "http://localhost:8000/1", nil)
  743. if err != nil {
  744. t.Error(err)
  745. }
  746. o.ServeHTTP(recorder, req)
  747. expect(t, recorder.Code, http.StatusOK)
  748. refute(t, len(buff.String()), 0)
  749. expect(t, buff.String(), "1")
  750. }
  751. type Param33Action struct {
  752. Ctx
  753. }
  754. func (p *Param33Action) Get() string {
  755. i := p.ParamBool(":name")
  756. return fmt.Sprintf("%v", i)
  757. }
  758. func TestParams35(t *testing.T) {
  759. buff := bytes.NewBufferString("")
  760. recorder := httptest.NewRecorder()
  761. recorder.Body = buff
  762. o := Classic()
  763. o.Get("/(:name[0-9]+)", new(Param33Action))
  764. req, err := http.NewRequest("GET", "http://localhost:8000/1", nil)
  765. if err != nil {
  766. t.Error(err)
  767. }
  768. o.ServeHTTP(recorder, req)
  769. expect(t, recorder.Code, http.StatusOK)
  770. refute(t, len(buff.String()), 0)
  771. expect(t, buff.String(), "true")
  772. }
  773. type Param34Action struct {
  774. Ctx
  775. }
  776. func (p *Param34Action) Get() string {
  777. i := p.Param(":name")
  778. return fmt.Sprintf("%v", i)
  779. }
  780. func TestParams36(t *testing.T) {
  781. buff := bytes.NewBufferString("")
  782. recorder := httptest.NewRecorder()
  783. recorder.Body = buff
  784. o := Classic()
  785. o.Get("/(:name[0-9]+)", new(Param34Action))
  786. req, err := http.NewRequest("GET", "http://localhost:8000/1", nil)
  787. if err != nil {
  788. t.Error(err)
  789. }
  790. o.ServeHTTP(recorder, req)
  791. expect(t, recorder.Code, http.StatusOK)
  792. refute(t, len(buff.String()), 0)
  793. expect(t, buff.String(), "1")
  794. }