Former-commit-id: a665e3f1253feb0d520c79ea499cba1f205c9c4f
This commit is contained in:
corebreaker 2017-07-18 23:13:05 +03:00
parent 6b747affad
commit 0e3b29c551

View File

@ -2,76 +2,76 @@
package host_test package host_test
import ( import (
"net" "net"
"net/url" "net/url"
"testing" "testing"
"time" "time"
"github.com/kataras/iris" "github.com/kataras/iris"
"github.com/kataras/iris/context" "github.com/kataras/iris/context"
"github.com/kataras/iris/core/host" "github.com/kataras/iris/core/host"
"github.com/kataras/iris/httptest" "github.com/kataras/iris/httptest"
) )
func TestProxy(t *testing.T) { func TestProxy(t *testing.T) {
expectedIndex := "ok /" expectedIndex := "ok /"
expectedAbout := "ok /about" expectedAbout := "ok /about"
unexpectedRoute := "unexpected" unexpectedRoute := "unexpected"
// proxySrv := iris.New() // proxySrv := iris.New()
u, err := url.Parse("https://localhost:4444") u, err := url.Parse("https://localhost:4444")
if err != nil { if err != nil {
t.Fatalf("%v while parsing url", err) t.Fatalf("%v while parsing url", err)
} }
// p := host.ProxyHandler(u) // p := host.ProxyHandler(u)
// transport := &http.Transport{ // transport := &http.Transport{
// TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, // TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
// } // }
// p.Transport = transport // p.Transport = transport
// proxySrv.Downgrade(p.ServeHTTP) // proxySrv.Downgrade(p.ServeHTTP)
// go proxySrv.Run(iris.Addr(":80"), iris.WithoutBanner, iris.WithoutInterruptHandler) // go proxySrv.Run(iris.Addr(":80"), iris.WithoutBanner, iris.WithoutInterruptHandler)
proxy := host.NewProxy("", u) proxy := host.NewProxy("", u)
addr := &net.TCPAddr{ addr := &net.TCPAddr{
IP: net.IPv4(127, 0, 0, 1), IP: net.IPv4(127, 0, 0, 1),
Port: 0, Port: 0,
} }
listener, err := net.ListenTCP("tcp", addr) listener, err := net.ListenTCP("tcp", addr)
if err != nil { if err != nil {
t.Fatalf("%v while creating listener", err) t.Fatalf("%v while creating listener", err)
} }
go proxy.Serve(listener) // should be localhost/127.0.0.1:80 but travis throws permission denied. go proxy.Serve(listener) // should be localhost/127.0.0.1:80 but travis throws permission denied.
t.Log(listener.Addr().String()) t.Log(listener.Addr().String())
<-time.After(time.Second) <-time.After(time.Second)
t.Log(listener.Addr().String()) t.Log(listener.Addr().String())
app := iris.New() app := iris.New()
app.Get("/", func(ctx context.Context) { app.Get("/", func(ctx context.Context) {
ctx.WriteString(expectedIndex) ctx.WriteString(expectedIndex)
}) })
app.Get("/about", func(ctx context.Context) { app.Get("/about", func(ctx context.Context) {
ctx.WriteString(expectedAbout) ctx.WriteString(expectedAbout)
}) })
app.OnErrorCode(iris.StatusNotFound, func(ctx context.Context) { app.OnErrorCode(iris.StatusNotFound, func(ctx context.Context) {
ctx.WriteString(unexpectedRoute) ctx.WriteString(unexpectedRoute)
}) })
l, err := net.Listen("tcp", "localhost:4444") // should be localhost/127.0.0.1:443 but travis throws permission denied. l, err := net.Listen("tcp", "localhost:4444") // should be localhost/127.0.0.1:443 but travis throws permission denied.
if err != nil { if err != nil {
t.Fatalf("%v while creating tcp4 listener for new tls local test listener", err) t.Fatalf("%v while creating tcp4 listener for new tls local test listener", err)
} }
// main server // main server
go app.Run(iris.Listener(httptest.NewLocalTLSListener(l)), iris.WithoutBanner) go app.Run(iris.Listener(httptest.NewLocalTLSListener(l)), iris.WithoutBanner)
e := httptest.NewInsecure(t, httptest.URL("http://"+listener.Addr().String())) e := httptest.NewInsecure(t, httptest.URL("http://"+listener.Addr().String()))
e.GET("/").Expect().Status(iris.StatusOK).Body().Equal(expectedIndex) e.GET("/").Expect().Status(iris.StatusOK).Body().Equal(expectedIndex)
e.GET("/about").Expect().Status(iris.StatusOK).Body().Equal(expectedAbout) e.GET("/about").Expect().Status(iris.StatusOK).Body().Equal(expectedAbout)
e.GET("/notfound").Expect().Status(iris.StatusNotFound).Body().Equal(unexpectedRoute) e.GET("/notfound").Expect().Status(iris.StatusNotFound).Body().Equal(unexpectedRoute)
} }