-
Notifications
You must be signed in to change notification settings - Fork 43
/
connection_test.go
58 lines (51 loc) · 1.55 KB
/
connection_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
* Copyright (c) 2020 Percipia
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*
* Contributor(s):
* Andrew Querol <aquerol@percipia.com>
*/
package eslgo
import (
"bufio"
"context"
"github.com/percipia/eslgo/command"
"github.com/stretchr/testify/assert"
"net"
"sync"
"testing"
"time"
)
func TestConn_SendCommand(t *testing.T) {
server, client := net.Pipe()
connection := newConnection(client, false, DefaultOptions)
defer connection.Close()
defer server.Close()
defer client.Close()
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
serverReader := bufio.NewReader(server)
defer serverReader.Discard(serverReader.Buffered())
var wait sync.WaitGroup
wait.Add(1)
go func() {
response, err := connection.SendCommand(ctx, command.Auth{
Password: "test1234",
})
assert.Nil(t, err)
assert.NotNil(t, response)
assert.True(t, response.IsOk())
assert.Equal(t, "+OK Job-UUID: c7709e9c-1517-11dc-842a-d3a3942d3d63", response.GetHeader("Reply-Text"))
wait.Done()
}()
// This is sorta lazy, we should be reading until the proper deliminator of \r\n\r\n
incomingCommand, err := serverReader.ReadString('\r')
assert.Nil(t, err)
assert.Equal(t, "auth test1234\r", incomingCommand)
_, err = server.Write([]byte("Content-Type: command/reply\r\nReply-Text: +OK Job-UUID: c7709e9c-1517-11dc-842a-d3a3942d3d63\r\n\r\n"))
assert.Nil(t, err)
wait.Wait()
}