-
Notifications
You must be signed in to change notification settings - Fork 75
/
context_test.go
125 lines (110 loc) · 3.05 KB
/
context_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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright 2020 lesismal. All rights reserved.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package arpc
import (
"errors"
"testing"
"github.com/lesismal/arpc/codec"
)
func TestContext_Get(t *testing.T) {
ctx := &Context{Message: &Message{}}
if v, ok := ctx.Get("key"); ok {
t.Fatalf("Context.Get() error, returns %v, want nil", v)
}
values := ctx.Values()
if len(values) > 0 {
t.Fatalf("invalid Context.Values() length, returns %v, want 0", len(values))
}
}
func TestContext_Set(t *testing.T) {
key := "key"
value := "value"
ctx := &Context{Message: &Message{}}
ctx.Set(key, nil)
cv, ok := ctx.Get(key)
if ok {
t.Fatalf("Context.Get() failed: Get '%v', want nil", cv)
}
ctx.Set(key, value)
cv, ok = ctx.Get(key)
if !ok {
t.Fatalf("Context.Get() failed: Get nil, want '%v'", value)
}
if cv != value {
t.Fatalf("Context.Get() failed: Get '%v', want '%v'", cv, value)
}
}
func TestContext_Body(t *testing.T) {
bodyValue := "body"
ctx := &Context{
Client: &Client{Codec: codec.DefaultCodec},
Message: newMessage(CmdRequest, "method", bodyValue, false, false, 0, DefaultHandler, codec.DefaultCodec, nil),
}
if string(ctx.Body()) != bodyValue {
t.Fatalf("Context.Body() = %v, want %v", string(ctx.Body()), bodyValue)
}
}
func TestContext_Bind(t *testing.T) {
ctx := &Context{
Client: &Client{Codec: codec.DefaultCodec},
Message: newMessage(CmdRequest, "method", "data", true, false, 0, DefaultHandler, codec.DefaultCodec, nil),
}
if err := ctx.Bind(nil); err == nil {
t.Fatalf("Context.Bind() error = nil, want %v", err)
}
}
func TestContext_Abort(t *testing.T) {
ok := false
h1 := func(ctx *Context) {
ctx.Abort()
}
h2 := func(ctx *Context) {
ok = true
}
ctx := &Context{handlers: []HandlerFunc{h1, h2}}
ctx.Next()
if ok {
t.Fatalf("Context.Abort() ok != false, have %v", ok)
}
}
func TestContext_Deadline(t *testing.T) {
ctx := &Context{}
if deadline, ok := ctx.Deadline(); !deadline.IsZero() || ok {
t.Fatalf("Context.Deadline() err, have %v, %v", ok, deadline.IsZero())
}
}
func TestContext_Done(t *testing.T) {
ctx := &Context{}
if done := ctx.Done(); done != nil {
t.Fatalf("Context.Bind() done != nil, have %v", done)
}
}
func TestContext_Err(t *testing.T) {
ctx := &Context{}
ctx.Err()
if err := ctx.Err(); err != nil {
t.Fatalf("Context.Err() error != nil, have %v", err)
}
}
func TestContext_Value(t *testing.T) {
ctx := &Context{Message: &Message{}}
if value := ctx.Value(3); value != nil {
t.Fatalf("Context.Value() value != nil, have %v", value)
}
ctx.Set("key", "value")
if value := ctx.Value("key"); value != "value" {
t.Fatalf("Context.Value() value != 'value', have %v", value)
}
}
func TestContext_ResponseError(t *testing.T) {
ctx := &Context{
Client: &Client{Handler: DefaultHandler},
Message: newMessage(CmdRequest, "test", nil, false, false, 0, DefaultHandler, codec.DefaultCodec, nil),
}
err := errors.New("test err")
ctx.Error(err)
if ctx.ResponseError() != err {
t.Fatalf("Context.ResponseError() != 'test err'")
}
}