| 1 | package testhelpers |
| 2 | |
| 3 | import ( |
| 4 | "log" |
| 5 | "net/http" |
| 6 | "net/http/httputil" |
| 7 | "os" |
| 8 | |
| 9 | "github.com/Azure/go-autorest/autorest" |
| 10 | ) |
| 11 | |
| 12 | func buildSender() autorest.Sender { |
| 13 | return autorest.DecorateSender(&http.Client{ |
| 14 | Transport: &http.Transport{ |
| 15 | Proxy: http.ProxyFromEnvironment, |
| 16 | }, |
| 17 | }, withRequestLogging()) |
| 18 | } |
| 19 | |
| 20 | func withRequestLogging() autorest.SendDecorator { |
| 21 | return func(s autorest.Sender) autorest.Sender { |
| 22 | return autorest.SenderFunc(func(r *http.Request) (*http.Response, error) { |
| 23 | shouldLog := os.Getenv("TEST_LOG") != "" |
| 24 | |
| 25 | if shouldLog { |
| 26 | // strip the authorization header prior to printing |
| 27 | authHeaderName := "Authorization" |
| 28 | auth := r.Header.Get(authHeaderName) |
| 29 | if auth != "" { |
| 30 | r.Header.Del(authHeaderName) |
| 31 | } |
| 32 | |
| 33 | // dump request to wire format |
| 34 | if dump, err := httputil.DumpRequestOut(r, true); err == nil { |
| 35 | log.Printf("[DEBUG] AzureRM Request: \n%s\n", dump) |
| 36 | } else { |
| 37 | // fallback to basic message |
| 38 | log.Printf("[DEBUG] AzureRM Request: %s to %s\n", r.Method, r.URL) |
| 39 | } |
| 40 | |
| 41 | // add the auth header back |
| 42 | if auth != "" { |
| 43 | r.Header.Add(authHeaderName, auth) |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | resp, err := s.Do(r) |
| 48 | |
| 49 | if shouldLog { |
| 50 | if resp != nil { |
| 51 | // dump response to wire format |
| 52 | if dump, err2 := httputil.DumpResponse(resp, true); err2 == nil { |
| 53 | log.Printf("[DEBUG] AzureRM Response for %s: \n%s\n", r.URL, dump) |
| 54 | } else { |
| 55 | // fallback to basic message |
| 56 | log.Printf("[DEBUG] AzureRM Response: %s for %s\n", resp.Status, r.URL) |
| 57 | } |
| 58 | } else { |
| 59 | log.Printf("[DEBUG] Request to %s completed with no response", r.URL) |
| 60 | } |
| 61 | } |
| 62 | return resp, err |
| 63 | }) |
| 64 | } |
| 65 | } |