forked from webrpc/webrpc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample_test.go
68 lines (55 loc) · 1.35 KB
/
example_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
59
60
61
62
63
64
65
66
67
68
package main
import (
"context"
"net/http"
"testing"
"time"
"errors"
"github.com/stretchr/testify/assert"
)
var (
client ExampleServiceClient
)
// func TestMain()
func init() {
go func() {
startServer()
}()
client = NewExampleServiceClient("http://0.0.0.0:4242", &http.Client{
Timeout: time.Duration(2 * time.Second),
})
time.Sleep(time.Millisecond * 500)
}
func TestPing(t *testing.T) {
err := client.Ping(context.Background())
assert.NoError(t, err)
}
func TestStatus(t *testing.T) {
resp, err := client.Status(context.Background())
assert.Equal(t, true, resp)
assert.NoError(t, err)
}
func TestGetUser(t *testing.T) {
{
arg1 := map[string]string{"a": "1"}
code, user, err := client.GetUser(context.Background(), arg1, 12)
assert.Equal(t, uint32(200), code)
assert.Equal(t, &User{ID: 12, Username: "hihi"}, user)
assert.NoError(t, err)
}
{
// Error case, expecting to receive an error
code, user, err := client.GetUser(context.Background(), nil, 911)
assert.True(t, errors.Is(err, ErrNotFound))
assert.Nil(t, user)
assert.Equal(t, uint32(0), code)
assert.Error(t, err)
assert.Contains(t, err.Error(), "not found")
}
{
name, user, err := client.FindUser(context.Background(), &SearchFilter{Q: "joe"})
assert.Equal(t, "joe", name)
assert.Equal(t, &User{ID: 123, Username: "joe"}, user)
assert.NoError(t, err)
}
}