2022-06-15 14:49:31 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2022-10-12 12:51:13 +00:00
|
|
|
"time"
|
2022-06-15 14:49:31 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
)
|
|
|
|
|
2022-10-12 12:51:13 +00:00
|
|
|
const (
|
|
|
|
waitTime = 50 * time.Millisecond
|
|
|
|
)
|
|
|
|
|
2022-06-15 14:49:31 +00:00
|
|
|
func TestServerURLSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(ServerURLSuite))
|
|
|
|
}
|
|
|
|
|
|
|
|
type ServerURLSuite struct {
|
|
|
|
suite.Suite
|
2022-06-15 15:36:17 +00:00
|
|
|
TestKeyComponents
|
2022-10-21 15:19:46 +00:00
|
|
|
TestLoggerComponents
|
2022-06-15 14:49:31 +00:00
|
|
|
|
2022-09-27 22:59:02 +00:00
|
|
|
server *MediaServer
|
|
|
|
serverNoPort *MediaServer
|
2022-10-12 12:51:13 +00:00
|
|
|
testStart time.Time
|
2022-06-15 14:49:31 +00:00
|
|
|
}
|
|
|
|
|
2022-10-12 12:51:13 +00:00
|
|
|
func (s *ServerURLSuite) SetupTest() {
|
2022-06-15 15:36:17 +00:00
|
|
|
s.SetupKeyComponents(s.T())
|
2022-10-21 15:19:46 +00:00
|
|
|
s.SetupLoggerComponents()
|
2022-06-15 15:36:17 +00:00
|
|
|
|
2022-06-15 14:49:31 +00:00
|
|
|
s.server = &MediaServer{Server: Server{
|
2023-01-06 12:21:14 +00:00
|
|
|
hostname: DefaultIP.String(),
|
2022-10-21 15:19:46 +00:00
|
|
|
portManger: newPortManager(s.Logger, nil),
|
2022-06-15 14:49:31 +00:00
|
|
|
}}
|
2022-10-12 10:00:14 +00:00
|
|
|
err := s.server.SetPort(1337)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
2022-06-15 14:49:31 +00:00
|
|
|
s.serverNoPort = &MediaServer{Server: Server{
|
2023-01-06 12:21:14 +00:00
|
|
|
hostname: DefaultIP.String(),
|
2022-10-21 15:19:46 +00:00
|
|
|
portManger: newPortManager(s.Logger, nil),
|
2022-06-15 14:49:31 +00:00
|
|
|
}}
|
2022-10-12 12:51:13 +00:00
|
|
|
go func() {
|
|
|
|
time.Sleep(waitTime)
|
2022-10-20 15:14:09 +00:00
|
|
|
s.serverNoPort.port = 80
|
2022-10-12 12:51:13 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
s.testStart = time.Now()
|
|
|
|
}
|
|
|
|
|
2022-10-12 14:09:28 +00:00
|
|
|
// testNoPort takes two strings and compares expects them both to be equal
|
|
|
|
// then compares ServerURLSuite.testStart to the current time
|
|
|
|
// the difference must be greater than waitTime.
|
|
|
|
// This is caused by the ServerURLSuite.SetupTest waiting waitTime before unlocking the portWait sync.Mutex
|
2022-10-12 12:51:13 +00:00
|
|
|
func (s *ServerURLSuite) testNoPort(expected string, actual string) {
|
|
|
|
s.Require().Equal(expected, actual)
|
2023-01-13 17:12:46 +00:00
|
|
|
s.Require().Greater(time.Since(s.testStart), waitTime)
|
2022-06-15 14:49:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ServerURLSuite) TestServer_MakeBaseURL() {
|
|
|
|
s.Require().Equal("https://127.0.0.1:1337", s.server.MakeBaseURL().String())
|
2022-10-20 15:14:09 +00:00
|
|
|
s.testNoPort("https://127.0.0.1:80", s.serverNoPort.MakeBaseURL().String())
|
2022-06-15 14:49:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ServerURLSuite) TestServer_MakeImageServerURL() {
|
|
|
|
s.Require().Equal("https://127.0.0.1:1337/messages/", s.server.MakeImageServerURL())
|
2022-10-20 15:14:09 +00:00
|
|
|
s.testNoPort("https://127.0.0.1:80/messages/", s.serverNoPort.MakeImageServerURL())
|
2022-06-15 14:49:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ServerURLSuite) TestServer_MakeIdenticonURL() {
|
|
|
|
s.Require().Equal(
|
|
|
|
"https://127.0.0.1:1337/messages/identicons?publicKey=0xdaff0d11decade",
|
|
|
|
s.server.MakeIdenticonURL("0xdaff0d11decade"))
|
2022-10-12 12:51:13 +00:00
|
|
|
s.testNoPort(
|
2022-10-20 15:14:09 +00:00
|
|
|
"https://127.0.0.1:80/messages/identicons?publicKey=0xdaff0d11decade",
|
2022-06-15 14:49:31 +00:00
|
|
|
s.serverNoPort.MakeIdenticonURL("0xdaff0d11decade"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ServerURLSuite) TestServer_MakeImageURL() {
|
|
|
|
s.Require().Equal(
|
|
|
|
"https://127.0.0.1:1337/messages/images?messageId=0x10aded70ffee",
|
|
|
|
s.server.MakeImageURL("0x10aded70ffee"))
|
2022-10-12 12:51:13 +00:00
|
|
|
s.testNoPort(
|
2022-10-20 15:14:09 +00:00
|
|
|
"https://127.0.0.1:80/messages/images?messageId=0x10aded70ffee",
|
2022-06-15 14:49:31 +00:00
|
|
|
s.serverNoPort.MakeImageURL("0x10aded70ffee"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ServerURLSuite) TestServer_MakeAudioURL() {
|
|
|
|
s.Require().Equal(
|
|
|
|
"https://127.0.0.1:1337/messages/audio?messageId=0xde1e7ebee71e",
|
|
|
|
s.server.MakeAudioURL("0xde1e7ebee71e"))
|
2022-10-12 12:51:13 +00:00
|
|
|
s.testNoPort(
|
2022-10-20 15:14:09 +00:00
|
|
|
"https://127.0.0.1:80/messages/audio?messageId=0xde1e7ebee71e",
|
2022-06-15 14:49:31 +00:00
|
|
|
s.serverNoPort.MakeAudioURL("0xde1e7ebee71e"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ServerURLSuite) TestServer_MakeStickerURL() {
|
|
|
|
s.Require().Equal(
|
|
|
|
"https://127.0.0.1:1337/ipfs?hash=0xdeadbeef4ac0",
|
|
|
|
s.server.MakeStickerURL("0xdeadbeef4ac0"))
|
2022-10-12 12:51:13 +00:00
|
|
|
s.testNoPort(
|
2022-10-20 15:14:09 +00:00
|
|
|
"https://127.0.0.1:80/ipfs?hash=0xdeadbeef4ac0",
|
2022-06-15 14:49:31 +00:00
|
|
|
s.serverNoPort.MakeStickerURL("0xdeadbeef4ac0"))
|
|
|
|
}
|