package httpconn

import (
	"net"
	"net/http"
	"net/http/httptest"
	"strings"
	"testing"

	"golang.org/x/net/nettest"
)

func TestNetTest_TestConn(t *testing.T) {
	nettest.TestConn(t, func() (c1, c2 net.Conn, stop func(), err error) {

		connCh := make(chan net.Conn, 1)
		doneCh := make(chan bool)

		mux := http.NewServeMux()
		mux.HandleFunc("/connect", func(w http.ResponseWriter, r *http.Request) {
			conn, err := Accept(w, r)
			if err != nil {
				panic(err)
			}
			connCh <- conn
			<-doneCh
		})

		srv := httptest.NewServer(mux)

		c1, err = DialHTTP(strings.TrimPrefix(srv.URL, "http://"), "/connect")
		if err != nil {
			panic(err)
		}
		c2 = <-connCh

		return c1, c2, func() {
			doneCh <- true
			srv.Close()
		}, nil
	})
}