Chromium Code Reviews| Index: service/urlfetch/context.go |
| diff --git a/service/urlfetch/context.go b/service/urlfetch/context.go |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..962e92a802637d98db73a9b55bb5345aeb20ec40 |
| --- /dev/null |
| +++ b/service/urlfetch/context.go |
| @@ -0,0 +1,44 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +// Package urlfetch provides a way for an application to get http.RoundTripper |
| +// that can make outbound HTTP requests. If used for https:// protocol, will |
| +// always validate SSL certificates. |
| +package urlfetch |
| + |
| +import ( |
| + "net/http" |
| + |
| + "golang.org/x/net/context" |
| +) |
| + |
| +type key int |
| + |
| +var serviceKey key |
| + |
| +// Factory is the function signature for factory methods compatible with |
| +// SetFactory. |
| +type Factory func(context.Context) http.RoundTripper |
| + |
| +// Get pulls http.RoundTripper implementation from context or |
| +// http.DefaultTransport if not set. |
| +func Get(c context.Context) http.RoundTripper { |
| + if f, ok := c.Value(serviceKey).(Factory); ok && f != nil { |
| + return f(c) |
| + } |
| + return http.DefaultTransport |
|
Vadim Sh.
2015/09/18 01:15:02
no memory implementation for now because this cove
iannucci
2015/09/18 01:19:38
but this means that it'll be making actual http ac
Vadim Sh.
2015/09/18 01:22:35
Well... One way of testing http stuff is actualy v
|
| +} |
| + |
| +// SetFactory sets the function to produce http.RoundTripper instances, |
| +// as returned by the Get method. |
| +func SetFactory(c context.Context, f Factory) context.Context { |
| + return context.WithValue(c, serviceKey, f) |
| +} |
| + |
| +// Set sets the current http.RoundTripper object in the context. Useful for |
| +// testing with a quick mock. This is just a shorthand SetFactory invocation |
| +// to set a factory which always returns the same object. |
| +func Set(c context.Context, r http.RoundTripper) context.Context { |
| + return SetFactory(c, func(context.Context) http.RoundTripper { return r }) |
| +} |