Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 // Package urlfetch provides a way for an application to get http.RoundTripper | |
| 6 // that can make outbound HTTP requests. If used for https:// protocol, will | |
| 7 // always validate SSL certificates. | |
| 8 package urlfetch | |
| 9 | |
| 10 import ( | |
| 11 "net/http" | |
| 12 | |
| 13 "golang.org/x/net/context" | |
| 14 ) | |
| 15 | |
| 16 type key int | |
| 17 | |
| 18 var serviceKey key | |
| 19 | |
| 20 // Factory is the function signature for factory methods compatible with | |
| 21 // SetFactory. | |
| 22 type Factory func(context.Context) http.RoundTripper | |
| 23 | |
| 24 // Get pulls http.RoundTripper implementation from context or | |
| 25 // http.DefaultTransport if not set. | |
| 26 func Get(c context.Context) http.RoundTripper { | |
| 27 if f, ok := c.Value(serviceKey).(Factory); ok && f != nil { | |
| 28 return f(c) | |
| 29 } | |
| 30 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
| |
| 31 } | |
| 32 | |
| 33 // SetFactory sets the function to produce http.RoundTripper instances, | |
| 34 // as returned by the Get method. | |
| 35 func SetFactory(c context.Context, f Factory) context.Context { | |
| 36 return context.WithValue(c, serviceKey, f) | |
| 37 } | |
| 38 | |
| 39 // Set sets the current http.RoundTripper object in the context. Useful for | |
| 40 // testing with a quick mock. This is just a shorthand SetFactory invocation | |
| 41 // to set a factory which always returns the same object. | |
| 42 func Set(c context.Context, r http.RoundTripper) context.Context { | |
| 43 return SetFactory(c, func(context.Context) http.RoundTripper { return r }) | |
| 44 } | |
| OLD | NEW |