tcpip_func_test.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // Copyright 2011 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package ssh
  5. // direct-tcpip functional tests
  6. import (
  7. "net"
  8. "net/http"
  9. "testing"
  10. )
  11. func TestTCPIPHTTP(t *testing.T) {
  12. if *sshuser == "" {
  13. t.Log("ssh.user not defined, skipping test")
  14. return
  15. }
  16. // google.com will generate at least one redirect, possibly three
  17. // depending on your location.
  18. doTest(t, "http://google.com")
  19. }
  20. func TestTCPIPHTTPS(t *testing.T) {
  21. if *sshuser == "" {
  22. t.Log("ssh.user not defined, skipping test")
  23. return
  24. }
  25. doTest(t, "https://encrypted.google.com/")
  26. }
  27. func doTest(t *testing.T, url string) {
  28. config := &ClientConfig{
  29. User: *sshuser,
  30. Auth: []ClientAuth{
  31. ClientAuthPassword(password(*sshpass)),
  32. },
  33. }
  34. conn, err := Dial("tcp", "localhost:22", config)
  35. if err != nil {
  36. t.Fatalf("Unable to connect: %s", err)
  37. }
  38. defer conn.Close()
  39. tr := &http.Transport{
  40. Dial: func(n, addr string) (net.Conn, error) {
  41. return conn.Dial(n, addr)
  42. },
  43. }
  44. client := &http.Client{
  45. Transport: tr,
  46. }
  47. resp, err := client.Get(url)
  48. if err != nil {
  49. t.Fatalf("unable to proxy: %s", err)
  50. }
  51. // got a body without error
  52. t.Log(resp)
  53. }