This commit is contained in:
Jonathan Leibiusky @xetorthio
2017-06-19 11:03:31 -03:00
parent 1ee90c43d1
commit cd6d172cfa
6 changed files with 173 additions and 33 deletions

49
router/router_test.go Normal file
View File

@@ -0,0 +1,49 @@
package router
import (
"crypto/tls"
"fmt"
"io/ioutil"
"net"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"github.com/stretchr/testify/assert"
)
func TestProxy_TLS(t *testing.T) {
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{Transport: tr}
const msg = "It works!"
var receivedHost string
ts := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, msg)
}))
defer ts.Close()
r := NewRouter(func(host string) (*net.TCPAddr, error) {
receivedHost = host
u, _ := url.Parse(ts.URL)
a, _ := net.ResolveTCPAddr("tcp", u.Host)
return a, nil
})
go r.Listen(":8080")
req, err := http.NewRequest("GET", "https://localhost:8080", nil)
assert.Nil(t, err)
resp, err := client.Do(req)
assert.Nil(t, err)
body, err := ioutil.ReadAll(resp.Body)
assert.Nil(t, err)
assert.Equal(t, msg, string(body))
assert.Equal(t, "localhost:8080", receivedHost)
}