Skip to content

Commit

Permalink
update unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lixizan committed Apr 27, 2023
1 parent c1bf64e commit 2122f60
Showing 1 changed file with 25 additions and 9 deletions.
34 changes: 25 additions & 9 deletions upgrader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,23 @@ import (
"bytes"
"crypto/tls"
"errors"
"fmt"
"github.com/stretchr/testify/assert"
"net"
"net/http"
"strconv"
"sync"
"sync/atomic"
"testing"
)

var _port = int64(9999)

func nextPort() string {
port := atomic.AddInt64(&_port, 1)
return strconv.Itoa(int(port))
}

func newHttpWriter() *httpWriter {
server, client := net.Pipe()
var r = bytes.NewBuffer(nil)
Expand Down Expand Up @@ -209,44 +219,50 @@ func TestNewServer(t *testing.T) {
var as = assert.New(t)

t.Run("ok", func(t *testing.T) {
var addr = ":" + nextPort()
var server = NewServer(new(BuiltinEventHandler), nil)
go server.Run(":12345")
go server.Run(addr)
_, _, err := NewClient(new(BuiltinEventHandler), &ClientOption{
Addr: "ws://localhost:12345",
Addr: "ws://localhost" + addr,
})
as.NoError(err)
})

t.Run("tls", func(t *testing.T) {
var addr = ":" + nextPort()
var server = NewServer(new(BuiltinEventHandler), nil)
go server.RunTLS(":12346", "", "")
go server.RunTLS(addr, "", "")
})

t.Run("fail 1", func(t *testing.T) {
var addr = ":" + nextPort()
var wg = sync.WaitGroup{}
wg.Add(1)
var server = NewServer(new(BuiltinEventHandler), nil)
server.OnError = func(conn net.Conn, err error) {
wg.Done()
}
go server.Run(":12347")
client, err := net.Dial("tcp", "localhost:12347")
go server.Run(addr)
client, err := net.Dial("tcp", "localhost"+addr)
as.NoError(err)
client.Write([]byte("POST ws://localhost:12347 HTTP/1.1\r\n\r\n"))
var payload = fmt.Sprintf("POST ws://localhost%s HTTP/1.1\r\n\r\n", addr)
client.Write([]byte(payload))
wg.Wait()
})

t.Run("fail 2", func(t *testing.T) {
var addr = ":" + nextPort()
var wg = sync.WaitGroup{}
wg.Add(1)
var server = NewServer(new(BuiltinEventHandler), nil)
server.OnError = func(conn net.Conn, err error) {
wg.Done()
}
go server.Run(":12347")
client, err := net.Dial("tcp", "localhost:12347")
go server.Run(addr)
client, err := net.Dial("tcp", "localhost"+addr)
as.NoError(err)
client.Write([]byte("GET ws://localhost:12347 HTTP/1.1 GWS\r\n\r\n"))
var payload = fmt.Sprintf("GET ws://localhost%s HTTP/1.1 GWS\r\n\r\n", addr)
client.Write([]byte(payload))
wg.Wait()
})
}
Expand Down

0 comments on commit 2122f60

Please sign in to comment.