1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: Apache-2.0
17 "git.arvados.org/arvados.git/sdk/go/arvados"
24 // URL where the proxy is listening. Same as Server.URL, but
25 // with parsing already done for you.
28 // A dump of each request that has been proxied.
31 // If non-nil, func will be called on each incoming request
32 // before proxying it.
33 Director func(*http.Request)
38 // NewProxy returns a new Proxy that saves a dump of each reqeust
39 // before forwarding to the indicated service.
40 func NewProxy(c *check.C, svc arvados.Service) *Proxy {
42 c.Assert(svc.InternalURLs, check.HasLen, 1)
43 for u := range svc.InternalURLs {
47 rp := httputil.NewSingleHostReverseProxy(&target)
48 rp.ErrorHandler = func(w http.ResponseWriter, r *http.Request, err error) {
49 dump, _ := httputil.DumpRequest(r, false)
50 c.Logf("arvadostest.Proxy ErrorHandler(%s): %s\n%s", r.URL, err, dump)
51 http.Error(w, err.Error(), http.StatusBadGateway)
53 rp.Transport = &http.Transport{
54 DialContext: (&net.Dialer{
55 Timeout: 30 * time.Second,
56 KeepAlive: 30 * time.Second,
60 IdleConnTimeout: 90 * time.Second,
61 TLSHandshakeTimeout: 10 * time.Second,
62 ExpectContinueTimeout: 1 * time.Second,
63 TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
65 srv := httptest.NewServer(rp)
66 u, err := url.Parse(srv.URL)
67 c.Assert(err, check.IsNil)
73 rp.Director = func(r *http.Request) {
76 if proxy.Director != nil {
79 dump, _ := httputil.DumpRequest(r, true)
81 proxy.RequestDumps = append(proxy.RequestDumps, dump)
83 r.URL.Scheme = target.Scheme
84 r.URL.Host = target.Host
89 // Wait waits until all of the proxied requests that have been sent to
90 // Director() have also been recorded in RequestDumps.
91 func (proxy *Proxy) Wait() {