context_test.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // Copyright 2012 The Gorilla 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 context
  5. import (
  6. "net/http"
  7. "testing"
  8. )
  9. type keyType int
  10. const (
  11. key1 keyType = iota
  12. key2
  13. )
  14. func TestContext(t *testing.T) {
  15. assertEqual := func(val interface{}, exp interface{}) {
  16. if val != exp {
  17. t.Errorf("Expected %v, got %v.", exp, val)
  18. }
  19. }
  20. r, _ := http.NewRequest("GET", "http://localhost:8080/", nil)
  21. // Get()
  22. assertEqual(Get(r, key1), nil)
  23. // Set()
  24. Set(r, key1, "1")
  25. assertEqual(Get(r, key1), "1")
  26. assertEqual(len(data[r]), 1)
  27. Set(r, key2, "2")
  28. assertEqual(Get(r, key2), "2")
  29. assertEqual(len(data[r]), 2)
  30. //GetOk
  31. value, ok := GetOk(r, key1)
  32. assertEqual(value, "1")
  33. assertEqual(ok, true)
  34. value, ok = GetOk(r, "not exists")
  35. assertEqual(value, nil)
  36. assertEqual(ok, false)
  37. Set(r, "nil value", nil)
  38. value, ok = GetOk(r, "nil value")
  39. assertEqual(value, nil)
  40. assertEqual(ok, true)
  41. // Delete()
  42. Delete(r, key1)
  43. assertEqual(Get(r, key1), nil)
  44. assertEqual(len(data[r]), 2)
  45. Delete(r, key2)
  46. assertEqual(Get(r, key2), nil)
  47. assertEqual(len(data[r]), 1)
  48. // Clear()
  49. Clear(r)
  50. assertEqual(len(data), 0)
  51. }