param_test.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package tango
  2. import (
  3. "testing"
  4. "bytes"
  5. "net/http/httptest"
  6. "net/http"
  7. )
  8. type ParamAction struct {
  9. Params
  10. }
  11. func (p *ParamAction) Get() string {
  12. return p.Params.Get(":name")
  13. }
  14. func TestParams1(t *testing.T) {
  15. buff := bytes.NewBufferString("")
  16. recorder := httptest.NewRecorder()
  17. recorder.Body = buff
  18. o := Classic()
  19. o.Get("/:name", new(ParamAction))
  20. req, err := http.NewRequest("GET", "http://localhost:8000/foobar", nil)
  21. if err != nil {
  22. t.Error(err)
  23. }
  24. o.ServeHTTP(recorder, req)
  25. expect(t, recorder.Code, http.StatusOK)
  26. refute(t, len(buff.String()), 0)
  27. expect(t, buff.String(), "foobar")
  28. }
  29. type Param2Action struct {
  30. Params
  31. }
  32. func (p *Param2Action) Get() string {
  33. return p.Params.Get(":0")
  34. }
  35. func TestParams2(t *testing.T) {
  36. buff := bytes.NewBufferString("")
  37. recorder := httptest.NewRecorder()
  38. recorder.Body = buff
  39. o := Classic()
  40. o.Get("/(.*)", new(Param2Action))
  41. req, err := http.NewRequest("GET", "http://localhost:8000/foobar", nil)
  42. if err != nil {
  43. t.Error(err)
  44. }
  45. o.ServeHTTP(recorder, req)
  46. expect(t, recorder.Code, http.StatusOK)
  47. refute(t, len(buff.String()), 0)
  48. expect(t, buff.String(), "foobar")
  49. }