| 635 | } |
| 636 | |
| 637 | func TestWSProxyPath(t *testing.T) { |
| 638 | const proxyPath = "proxy1" |
| 639 | |
| 640 | // Listen to a random port |
| 641 | l, err := net.Listen("tcp", ":0") |
| 642 | if err != nil { |
| 643 | t.Fatalf("Error in listen: %v", err) |
| 644 | } |
| 645 | defer l.Close() |
| 646 | |
| 647 | proxyPort := l.Addr().(*net.TCPAddr).Port |
| 648 | |
| 649 | ch := make(chan struct{}, 1) |
| 650 | proxySrv := &http.Server{ |
| 651 | Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 652 | if r.URL.Path == "/"+proxyPath { |
| 653 | ch <- struct{}{} |
| 654 | } |
| 655 | }), |
| 656 | } |
| 657 | defer proxySrv.Shutdown(context.Background()) |
| 658 | go proxySrv.Serve(l) |
| 659 | |
| 660 | for _, test := range []struct { |
| 661 | name string |
| 662 | path string |
| 663 | }{ |
| 664 | {"without slash", proxyPath}, |
| 665 | {"with slash", "/" + proxyPath}, |
| 666 | } { |
| 667 | t.Run(test.name, func(t *testing.T) { |
| 668 | url := fmt.Sprintf("ws://127.0.0.1:%d", proxyPort) |
| 669 | nc, err := Connect(url, ProxyPath(test.path)) |
| 670 | if err == nil { |
| 671 | nc.Close() |
| 672 | t.Fatal("Did not expect to connect") |
| 673 | } |
| 674 | select { |
| 675 | case <-ch: |
| 676 | // OK: |
| 677 | case <-time.After(time.Second): |
| 678 | t.Fatal("Proxy was not reached") |
| 679 | } |
| 680 | }) |
| 681 | } |
| 682 | } |
| 683 | |
| 684 | // --- helpers --- |
| 685 | |