params_test.go 648 B

1234567891011121314151617181920212223242526272829303132
  1. package context
  2. import (
  3. "context"
  4. "net/http"
  5. "strings"
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. func TestVars(t *testing.T) {
  10. expect := map[string]string{
  11. "a": "1",
  12. "b": "2",
  13. }
  14. r, err := http.NewRequest(http.MethodGet, "/", nil)
  15. assert.Nil(t, err)
  16. r = r.WithContext(context.WithValue(context.Background(), pathVars, expect))
  17. assert.EqualValues(t, expect, Vars(r))
  18. }
  19. func TestVarsNil(t *testing.T) {
  20. r, err := http.NewRequest(http.MethodGet, "/", nil)
  21. assert.Nil(t, err)
  22. assert.Nil(t, Vars(r))
  23. }
  24. func TestContextKey(t *testing.T) {
  25. ck := contextKey("hello")
  26. assert.True(t, strings.Contains(ck.String(), "hello"))
  27. }