コード例 #1
0
ファイル: http_test.go プロジェクト: kataras/iris
// Contains the server test for multi running servers
func TestMultiRunningServers_v1_PROXY(t *testing.T) {
	defer iris.Close()
	host := "localhost" // you have to add it to your hosts file( for windows, as 127.0.0.1 mydomain.com)
	hostTLS := "localhost:9999"
	iris.Close()
	defer iris.Close()
	iris.ResetDefault()
	iris.Default.Config.DisableBanner = true
	// create the key and cert files on the fly, and delete them when this test finished
	certFile, ferr := ioutil.TempFile("", "cert")

	if ferr != nil {
		t.Fatal(ferr.Error())
	}

	keyFile, ferr := ioutil.TempFile("", "key")
	if ferr != nil {
		t.Fatal(ferr.Error())
	}

	defer func() {
		certFile.Close()
		time.Sleep(350 * time.Millisecond)
		os.Remove(certFile.Name())

		keyFile.Close()
		time.Sleep(350 * time.Millisecond)
		os.Remove(keyFile.Name())
	}()

	certFile.WriteString(testTLSCert)
	keyFile.WriteString(testTLSKey)

	iris.Get("/", func(ctx *iris.Context) {
		ctx.Write("Hello from %s", hostTLS)
	})

	go iris.ListenTLS(hostTLS, certFile.Name(), keyFile.Name())
	if ok := <-iris.Default.Available; !ok {
		t.Fatal("Unexpected error: server cannot start, please report this as bug!!")
	}

	closeProxy := iris.Proxy("localhost:8080", "https://"+hostTLS)
	defer closeProxy()

	e := httptest.New(iris.Default, t, httptest.ExplicitURL(true))

	e.Request("GET", "http://"+host+":8080").Expect().Status(iris.StatusOK).Body().Equal("Hello from " + hostTLS)
	e.Request("GET", "https://"+hostTLS).Expect().Status(iris.StatusOK).Body().Equal("Hello from " + hostTLS)

}
コード例 #2
0
ファイル: http_test.go プロジェクト: kataras/iris
// Contains the server test for multi running servers
func TestMultiRunningServers_v2(t *testing.T) {
	defer iris.Close()
	domain := "localhost"
	hostTLS := "localhost:9999"

	iris.ResetDefault()
	iris.Default.Config.DisableBanner = true

	// create the key and cert files on the fly, and delete them when this test finished
	certFile, ferr := ioutil.TempFile("", "cert")

	if ferr != nil {
		t.Fatal(ferr.Error())
	}

	keyFile, ferr := ioutil.TempFile("", "key")
	if ferr != nil {
		t.Fatal(ferr.Error())
	}

	certFile.WriteString(testTLSCert)
	keyFile.WriteString(testTLSKey)

	defer func() {
		certFile.Close()
		time.Sleep(350 * time.Millisecond)
		os.Remove(certFile.Name())

		keyFile.Close()
		time.Sleep(350 * time.Millisecond)
		os.Remove(keyFile.Name())
	}()

	iris.Get("/", func(ctx *iris.Context) {
		ctx.Write("Hello from %s", hostTLS)
	})

	// add a secondary server
	//Servers.Add(ServerConfiguration{ListeningAddr: domain + ":80", RedirectTo: "https://" + host, Virtual: true})
	// add our primary/main server
	//Servers.Add(ServerConfiguration{ListeningAddr: host, CertFile: certFile.Name(), KeyFile: keyFile.Name(), Virtual: true})

	//go Go()

	// using the proxy handler
	fsrv1 := &fasthttp.Server{Handler: iris.ProxyHandler(domain+":8080", "https://"+hostTLS)}
	go fsrv1.ListenAndServe(domain + ":8080")
	// using the same iris' handler but not as proxy, just the same handler
	fsrv2 := &fasthttp.Server{Handler: iris.Default.Router}
	go fsrv2.ListenAndServe(domain + ":8888")

	go iris.ListenTLS(hostTLS, certFile.Name(), keyFile.Name())

	if ok := <-iris.Default.Available; !ok {
		t.Fatal("Unexpected error: server cannot start, please report this as bug!!")
	}

	e := httptest.New(iris.Default, t, httptest.ExplicitURL(true))

	e.Request("GET", "http://"+domain+":8080").Expect().Status(iris.StatusOK).Body().Equal("Hello from " + hostTLS)
	e.Request("GET", "http://localhost:8888").Expect().Status(iris.StatusOK).Body().Equal("Hello from " + hostTLS)
	e.Request("GET", "https://"+hostTLS).Expect().Status(iris.StatusOK).Body().Equal("Hello from " + hostTLS)

}