Example #1
0
func main() {
	http.HandleFunc("/", serveHTTP)
	handle(spdy.ConnectAndServe("http://localhost:8080/", &tls.Config{InsecureSkipVerify: true}, nil))
}
Example #2
0
func TestProxyConnect(t *testing.T) {
	cert, err := tls.X509KeyPair(localhostCert, localhostKey)
	if err != nil {
		panic(fmt.Sprintf("could not read certificate: %v", err))
	}

	serverTLSConfig := new(tls.Config)
	serverTLSConfig.Certificates = []tls.Certificate{cert}

	conn, err := net.Listen("tcp", "localhost:0")
	if err != nil {
		panic(fmt.Sprintf("could not listen: %v", err))
	}

	listener := tls.NewListener(conn, serverTLSConfig)

	errChan := make(chan error)

	go func() {
		srv := &http.Server{
			Addr: conn.Addr().String(),
			Handler: spdy.ProxyConnections(spdy.ProxyConnHandlerFunc(func(conn spdy.Conn) {
				req, err := http.NewRequest("GET", "http://example.com/", nil)
				if err != nil {
					errChan <- err
					return
				}
				resp, err := conn.RequestResponse(req, nil, 2)
				if err != nil {
					errChan <- err
					return
				}
				body, err := ioutil.ReadAll(resp.Body)
				if err != nil {
					errChan <- err
					return
				}

				if !bytes.Equal(body, []byte("HELLO")) {
					errChan <- fmt.Errorf("Expected HELLO. Got %v", string(body))
					return
				}

				close(errChan)
			})),
		}
		srv.Serve(listener)
		println("Serve done")
	}()

	clientTLSConfig := &tls.Config{InsecureSkipVerify: true}

	url := "https://" + conn.Addr().String()

	go func() {
		err = spdy.ConnectAndServe(url, clientTLSConfig, &http.Server{
			Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
				if r.Method != "GET" {
					errChan <- fmt.Errorf("Expected method GET. Got: %v", r.Method)
				}
				if r.URL.String() != "http://example.com/" {
					errChan <- fmt.Errorf("Expected http://example.com. Got %v", r.URL)
				}
				w.Write([]byte("HELLO"))
			}),
		})
		if err != nil {
			errChan <- fmt.Errorf("ConnectAndServeFailed: %v", err)
		}
	}()

	select {
	case err = <-errChan:
		if err != nil {
			t.Error(err)
		}
	case <-time.After(time.Second):
		t.Error("Timeout")
	}
}