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.

68 lines
1.3KB

  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. "net/http"
  8. "net/http/httptest"
  9. "testing"
  10. )
  11. type PrefixAction struct {
  12. }
  13. func (PrefixAction) Get() string {
  14. return "Prefix"
  15. }
  16. type NoPrefixAction struct {
  17. }
  18. func (NoPrefixAction) Get() string {
  19. return "NoPrefix"
  20. }
  21. func TestPrefix(t *testing.T) {
  22. buff := bytes.NewBufferString("")
  23. recorder := httptest.NewRecorder()
  24. recorder.Body = buff
  25. var isPrefix bool
  26. o := Classic()
  27. o.Use(Prefix("/prefix", HandlerFunc(func(ctx *Context) {
  28. isPrefix = true
  29. ctx.Next()
  30. })))
  31. o.Get("/prefix/t", new(PrefixAction))
  32. o.Get("/t", new(NoPrefixAction))
  33. req, err := http.NewRequest("GET", "http://localhost:8000/prefix/t", nil)
  34. if err != nil {
  35. t.Error(err)
  36. }
  37. o.ServeHTTP(recorder, req)
  38. expect(t, recorder.Code, http.StatusOK)
  39. refute(t, len(buff.String()), 0)
  40. expect(t, buff.String(), "Prefix")
  41. expect(t, isPrefix, true)
  42. isPrefix = false
  43. buff.Reset()
  44. req, err = http.NewRequest("GET", "http://localhost:8000/t", nil)
  45. if err != nil {
  46. t.Error(err)
  47. }
  48. o.ServeHTTP(recorder, req)
  49. expect(t, recorder.Code, http.StatusOK)
  50. refute(t, len(buff.String()), 0)
  51. expect(t, buff.String(), "NoPrefix")
  52. expect(t, isPrefix, false)
  53. }