transport_test.go 833 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. // Copyright 2015 The Go 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 internal
  5. import (
  6. "net/http"
  7. "testing"
  8. "golang.org/x/net/context"
  9. )
  10. func TestContextClient(t *testing.T) {
  11. rc := &http.Client{}
  12. RegisterContextClientFunc(func(context.Context) (*http.Client, error) {
  13. return rc, nil
  14. })
  15. c := &http.Client{}
  16. ctx := context.WithValue(nil, HTTPClient, c)
  17. hc, err := ContextClient(ctx)
  18. if err != nil {
  19. t.Fatalf("want valid client; got err = %v", err)
  20. }
  21. if hc != c {
  22. t.Fatalf("want context client = %p; got = %p", c, hc)
  23. }
  24. hc, err = ContextClient(context.TODO())
  25. if err != nil {
  26. t.Fatalf("want valid client; got err = %v", err)
  27. }
  28. if hc != rc {
  29. t.Fatalf("want registered client = %p; got = %p", c, hc)
  30. }
  31. }