context_test.go 871 B

1234567891011121314151617181920212223242526272829303132333435
  1. package context
  2. import (
  3. "testing"
  4. "github.com/docker/docker/pkg/version"
  5. )
  6. func TestContext(t *testing.T) {
  7. ctx := Background()
  8. // First make sure getting non-existent values doesn't break
  9. if id := ctx.RequestID(); id != "" {
  10. t.Fatalf("RequestID() should have been '', was: %q", id)
  11. }
  12. if ver := ctx.Version(); ver != "" {
  13. t.Fatalf("Version() should have been '', was: %q", ver)
  14. }
  15. // Test basic set/get
  16. ctx = WithValue(ctx, RequestID, "123")
  17. if ctx.RequestID() != "123" {
  18. t.Fatalf("RequestID() should have been '123'")
  19. }
  20. // Now make sure after a 2nd set we can still get both
  21. ctx = WithValue(ctx, APIVersion, version.Version("x.y"))
  22. if id := ctx.RequestID(); id != "123" {
  23. t.Fatalf("RequestID() should have been '123', was %q", id)
  24. }
  25. if ver := ctx.Version(); ver != "x.y" {
  26. t.Fatalf("Version() should have been 'x.y', was %q", ver)
  27. }
  28. }