Esempio n. 1
0
func TestTLS(t *testing.T) {
	t.Parallel()
	port, err := freeport.Get()
	ensure.Nil(t, err)

	cert, err := tls.X509KeyPair(localhostCert, localhostKey)
	if err != nil {
		t.Fatalf("error loading cert: %v", err)
	}
	const count = 10000
	hello := []byte("hello")
	finOkHandler := make(chan struct{})
	okHandler := func(w http.ResponseWriter, r *http.Request) {
		defer close(finOkHandler)
		w.WriteHeader(200)
		for i := 0; i < count; i++ {
			w.Write(hello)
		}
	}

	server := &http.Server{
		Addr:    fmt.Sprintf("0.0.0.0:%d", port),
		Handler: http.HandlerFunc(okHandler),
		TLSConfig: &tls.Config{
			NextProtos:   []string{"http/1.1"},
			Certificates: []tls.Certificate{cert},
		},
	}
	transport := &http.Transport{
		TLSClientConfig: &tls.Config{
			InsecureSkipVerify: true,
		},
	}
	client := &http.Client{Transport: transport}
	down := &httpdown.HTTP{}
	s, err := down.ListenAndServe(server)
	ensure.Nil(t, err)
	res, err := client.Get(fmt.Sprintf("https://%s/", server.Addr))
	ensure.Nil(t, err)

	finStop := make(chan struct{})
	go func() {
		defer close(finStop)
		ensure.Nil(t, s.Stop())
	}()

	actualBody, err := ioutil.ReadAll(res.Body)
	ensure.Nil(t, err)
	ensure.DeepEqual(t, actualBody, bytes.Repeat(hello, count))
	ensure.Nil(t, res.Body.Close())
	<-finOkHandler
	<-finStop
}
Esempio n. 2
0
func TestSafeRetry(t *testing.T) {
	t.Parallel()
	port, err := freeport.Get()
	if err != nil {
		t.Fatal(err)
	}
	addr := fmt.Sprintf("127.0.0.1:%d", port)
	server := httptest.NewUnstartedServer(sleepHandler(time.Millisecond))
	transport := &httpcontrol.Transport{
		MaxTries: 2,
	}
	first := false
	second := false
	transport.Stats = func(stats *httpcontrol.Stats) {
		if !first {
			first = true
			if stats.Error == nil {
				t.Fatal("was expecting error")
			}
			if !stats.Retry.Pending {
				t.Fatal("was expecting pending retry", stats.Error)
			}
			server.Listener, err = net.Listen("tcp", addr)
			if err != nil {
				t.Fatal(err)
			}
			server.Start()
			return
		}

		if !second {
			second = true
			if stats.Error != nil {
				t.Fatal(stats.Error, server.URL)
			}
			return
		}
	}
	defer call(transport.Close, t)
	client := &http.Client{Transport: transport}
	res, err := client.Get(fmt.Sprintf("http://%s/", addr))
	if err != nil {
		t.Fatal(err)
	}
	assertResponse(res, t)
	if !first {
		t.Fatal("did not see first request")
	}
	if !second {
		t.Fatal("did not see second request")
	}
}
Esempio n. 3
0
func TestSafeRetryAfterTimeout(t *testing.T) {
	t.Parallel()
	port, err := freeport.Get()
	if err != nil {
		t.Fatal(err)
	}
	addr := fmt.Sprintf("127.0.0.1:%d", port)
	server := httptest.NewUnstartedServer(sleepHandler(5 * time.Second))
	transport := &httpcontrol.Transport{
		MaxTries:          3,
		RequestTimeout:    5 * time.Millisecond,
		RetryAfterTimeout: true,
	}
	first := false
	second := false
	third := false
	transport.Stats = func(stats *httpcontrol.Stats) {
		if !first {
			first = true
			if stats.Error == nil {
				t.Fatal("was expecting error")
			}
			if !stats.Retry.Pending {
				t.Fatal("was expecting pending retry", stats.Error)
			}
			server.Listener, err = net.Listen("tcp", addr)
			if err != nil {
				t.Fatal(err)
			}
			server.Start()
			return
		}

		if !second {
			second = true
			if stats.Error == nil {
				t.Fatal("was expecting error")
			}
			if !stats.Retry.Pending {
				t.Fatal("was expecting pending retry", stats.Error)
			}
			return
		}

		if !third {
			third = true
			if stats.Error == nil {
				t.Fatal("was expecting error")
			}
			if !stats.Retry.Pending {
				t.Fatal("was expecting pending retry", stats.Error)
			}
		}
	}
	defer call(transport.Close, t)
	client := &http.Client{Transport: transport}
	_, err = client.Get(fmt.Sprintf("http://%s/", addr))

	// Expect this to fail
	if err == nil {
		t.Fatal(err)
	}

	if !first {
		t.Fatal("did not see first request")
	}
	if !second {
		t.Fatal("did not see second request")
	}

	if !third {
		t.Fatal("did not see third request")
	}
}