cloud-game/pkg/network/httpx/address_test.go
sergystepanov 431d215eee
Update server HTTPS configuration (#337)
* Merge HTTP and HTTPS routes builder in coordinator

* Extract HTTP/S routes

* Rename config in coordinator

* Generalize child services

* Extract games library helper function

* Use string address instead of port for HTTP/S

* Use a dedicated port extractor function

* Add missing GA tag templating

* Rename shared server address and port params

* Introduce TLS config parameters

* Simplify HTTP/S server constructors

* Update server auto port roll

* Extract init function in worker

* Reorder return params of address type port fn

* Refactor config handler names

* Update TLS default config params

* Extract HTTP to HTTPS redirect function

* Use httpx in monitoring

* Don't log echo requests

* Remove error return from the abstract server

* Add WSS option to the worker-coordinator connection

* Change default worker config

* Make worker send its internal connection params

* Decouple gamelib from the coordinator

* Expose HTTP/S listener address

* Keep original HTTP/S addresses

* Remove no config handler in worker

* Use HTTP-HTTPS redirection

* Wrap net.Listener into a struct

* Clean http server creation fn

* Redirect to https with a generated address

* Use URL in the redirector

* Use zone address param in worker

* Make use of actual addr and port in the monitoring servers

* Use auto-justified monitoring addresses info

* Add the non-HTTPS worker to HTTPS coordinator connection warning

* Embed TLS struct

* Move connection API struct into cws package
2021-08-09 10:42:06 +03:00

47 lines
1.3 KiB
Go

package httpx
import (
"net"
"testing"
)
type testListener struct {
addr net.TCPAddr
}
func (tl testListener) Accept() (net.Conn, error) { return nil, nil }
func (tl testListener) Close() error { return nil }
func (tl testListener) Addr() net.Addr { return &tl.addr }
func NewTCP(port int) Listener {
return Listener{testListener{addr: net.TCPAddr{Port: port}}}
}
func TestMergeAddresses(t *testing.T) {
tests := []struct {
addr string
zone string
ls Listener
rez string
}{
{addr: "", rez: "localhost"},
{addr: ":", ls: NewTCP(0), rez: "localhost"},
{addr: "", ls: NewTCP(393), rez: "localhost:393"},
{addr: ":8080", ls: NewTCP(8080), rez: "localhost:8080"},
{addr: ":8080", ls: NewTCP(8081), rez: "localhost:8081"},
{addr: "host:8080", ls: NewTCP(8080), rez: "host:8080"},
{addr: "host:8080", ls: NewTCP(8081), rez: "host:8081"},
{addr: "host:8080", zone: "test", ls: NewTCP(8081), rez: "test.host:8081"},
{addr: ":80", ls: NewTCP(80), rez: "localhost"},
{addr: ":", ls: NewTCP(344), rez: "localhost:344"},
{addr: "https://garbage.com:99a9a", rez: "https://garbage.com:99a9a"},
{addr: "[::]", rez: "[::]"},
}
for _, test := range tests {
address := buildAddress(test.addr, test.zone, test.ls)
if address != test.rez {
t.Errorf("expected %v, got %v", test.rez, address)
}
}
}