summaryrefslogtreecommitdiff
path: root/libgo/go/exp/ssh/tcpip_func_test.go
blob: 261297241e9edcc41c6f8bb464d8f616d6f9038e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package ssh

// direct-tcpip functional tests

import (
	"net"
	"net/http"
	"testing"
)

func TestTCPIPHTTP(t *testing.T) {
	if *sshuser == "" {
		t.Log("ssh.user not defined, skipping test")
		return
	}
	// google.com will generate at least one redirect, possibly three
	// depending on your location.
	doTest(t, "http://google.com")
}

func TestTCPIPHTTPS(t *testing.T) {
	if *sshuser == "" {
		t.Log("ssh.user not defined, skipping test")
		return
	}
	doTest(t, "https://encrypted.google.com/")
}

func doTest(t *testing.T, url string) {
	config := &ClientConfig{
		User: *sshuser,
		Auth: []ClientAuth{
			ClientAuthPassword(password(*sshpass)),
		},
	}
	conn, err := Dial("tcp", "localhost:22", config)
	if err != nil {
		t.Fatalf("Unable to connect: %s", err)
	}
	defer conn.Close()
	tr := &http.Transport{
		Dial: func(n, addr string) (net.Conn, error) {
			return conn.Dial(n, addr)
		},
	}
	client := &http.Client{
		Transport: tr,
	}
	resp, err := client.Get(url)
	if err != nil {
		t.Fatalf("unable to proxy: %s", err)
	}
	// got a body without error
	t.Log(resp)
}