2022-06-15 14:49:31 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"encoding/hex"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2022-06-10 23:03:16 +00:00
|
|
|
"github.com/stretchr/testify/suite"
|
2022-06-15 14:49:31 +00:00
|
|
|
)
|
|
|
|
|
2022-06-10 15:32:15 +00:00
|
|
|
func TestGetOutboundIPSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(GetOutboundIPSuite))
|
|
|
|
}
|
|
|
|
|
|
|
|
type GetOutboundIPSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
TestPairingServerComponents
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *GetOutboundIPSuite) SetupSuite() {
|
|
|
|
s.SetupPairingServerComponents(s.T())
|
|
|
|
}
|
|
|
|
|
2022-06-15 14:49:31 +00:00
|
|
|
func testHandler(t *testing.T) func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
say, ok := r.URL.Query()["say"]
|
|
|
|
if !ok || len(say) == 0 {
|
|
|
|
say = append(say, "nothing")
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := w.Write([]byte("Hello I like to be a tls server. You said: `" + say[0] + "` " + time.Now().String()))
|
|
|
|
if err != nil {
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-03 14:50:40 +00:00
|
|
|
func makeThingToSay() (string, error) {
|
|
|
|
b := make([]byte, 32)
|
|
|
|
_, err := rand.Read(b)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return hex.EncodeToString(b), nil
|
|
|
|
}
|
|
|
|
|
2022-06-15 13:52:14 +00:00
|
|
|
func (s *GetOutboundIPSuite) TestGetOutboundIPWithFullServerE2e() {
|
|
|
|
s.PS.mode = Sending
|
|
|
|
s.PS.SetHandlers(HandlerPatternMap{"/hello": testHandler(s.T())})
|
2022-06-15 14:49:31 +00:00
|
|
|
|
2022-06-10 23:03:16 +00:00
|
|
|
err := s.PS.Start()
|
2022-06-15 13:52:14 +00:00
|
|
|
s.Require().NoError(err)
|
2022-06-15 14:49:31 +00:00
|
|
|
|
2022-06-15 15:36:17 +00:00
|
|
|
// Give time for the sever to be ready, hacky I know, I'll iron this out
|
2022-06-15 14:49:31 +00:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
2022-06-15 15:36:17 +00:00
|
|
|
|
|
|
|
// Server generates a QR code connection string
|
2022-06-10 23:03:16 +00:00
|
|
|
cp, err := s.PS.MakeConnectionParams()
|
2022-06-15 13:52:14 +00:00
|
|
|
s.Require().NoError(err)
|
2022-06-15 15:36:17 +00:00
|
|
|
|
2022-08-07 22:14:33 +00:00
|
|
|
qr := cp.ToString()
|
2022-06-15 15:36:17 +00:00
|
|
|
|
|
|
|
// Client reads QR code and parses the connection string
|
|
|
|
ccp := new(ConnectionParams)
|
|
|
|
err = ccp.FromString(qr)
|
2022-06-15 13:52:14 +00:00
|
|
|
s.Require().NoError(err)
|
2022-06-15 15:36:17 +00:00
|
|
|
|
2022-06-29 15:21:22 +00:00
|
|
|
c, err := NewPairingClient(ccp, nil)
|
2022-06-15 13:52:14 +00:00
|
|
|
s.Require().NoError(err)
|
2022-06-15 14:49:31 +00:00
|
|
|
|
2022-05-03 14:50:40 +00:00
|
|
|
thing, err := makeThingToSay()
|
2022-06-15 13:52:14 +00:00
|
|
|
s.Require().NoError(err)
|
2022-06-15 14:49:31 +00:00
|
|
|
|
2022-05-03 14:50:40 +00:00
|
|
|
response, err := c.Get(c.baseAddress.String() + "/hello?say=" + thing)
|
2022-06-15 13:52:14 +00:00
|
|
|
s.Require().NoError(err)
|
2022-06-15 14:49:31 +00:00
|
|
|
|
|
|
|
defer response.Body.Close()
|
|
|
|
|
|
|
|
content, err := ioutil.ReadAll(response.Body)
|
2022-06-15 13:52:14 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().Equal("Hello I like to be a tls server. You said: `"+thing+"`", string(content[:109]))
|
2022-06-15 14:49:31 +00:00
|
|
|
}
|