Пример #1
0
func TestProxy(t *testing.T) {
	count := 0
	code := http.StatusTeapot
	handler := func(w http.ResponseWriter, _ *http.Request) { count++; w.WriteHeader(code) }
	server := httptest.NewServer(http.HandlerFunc(handler))
	defer server.Close()

	u, err := url.Parse(server.URL)
	if err != nil {
		t.Fatal(err)
	}

	resolver := fixedResolver{[]string{u.Host}, time.Minute}
	proxy := proxy.Proxy(proxy.Resolver(resolver))
	transport := &http.Transport{}
	transport.RegisterProtocol("dummy", proxy)
	client := &http.Client{}
	client.Transport = transport

	resp, err := client.Get("dummy://foo.bar.net/path/query?key=value")
	if err != nil {
		t.Fatal(err)
	}

	if want, have := 1, count; want != have {
		t.Errorf("want count %d, have %d", want, have)
	}

	if want, have := code, resp.StatusCode; want != have {
		t.Errorf("want HTTP %d, have %d", want, have)
	}
}
Пример #2
0
// ExampleDefaultUsage shows how to wire up a default DNS SRV proxy into
// http.DefaultClient.
func ExampleDefaultUsage() {
	var rt http.RoundTripper
	rt = http.DefaultTransport
	rt = proxy.Proxy(proxy.Next(rt))
	rt = retry.Retry(retry.Next(rt))

	t := &http.Transport{}
	t.RegisterProtocol("dnssrv", rt)
	http.DefaultClient.Transport = t

	resp, err := http.Get("dnssrv://foo.bar.srv.internal.name/normal/path?key=value")
	if err != nil {
		log.Fatal(err)
	}

	io.Copy(os.Stdout, resp.Body)
	resp.Body.Close()
}
Пример #3
0
// ExampleCustomUsage shows how to wire up a customized DNS SRV proxy into
// http.DefaultClient.
func ExampleCustomUsage() {
	var rt http.RoundTripper
	rt = http.DefaultTransport

	rt = proxy.Proxy(
		proxy.Next(rt),
		proxy.Scheme("https"),
		proxy.PoolReporter(os.Stderr),
		proxy.Factory(pool.RoundRobin),
	)

	rt = retry.Retry(
		retry.Next(rt),
		retry.Pass(func(resp *http.Response, err error) error {
			if err != nil {
				return err
			}
			if resp.StatusCode >= 500 {
				return fmt.Errorf("HTTP %d %s", resp.StatusCode, resp.Status)
			}
			return nil
		}),
		retry.MaxAttempts(10),
		retry.Timeout(750*time.Millisecond),
	)

	t := &http.Transport{}
	t.RegisterProtocol("dnssrv", rt)
	http.DefaultClient.Transport = t

	resp, err := http.Get("dnssrv://foo.bar.srv.internal.name/normal/path?key=value")
	if err != nil {
		log.Fatal(err)
	}

	io.Copy(os.Stdout, resp.Body)
	resp.Body.Close()
}