-
Notifications
You must be signed in to change notification settings - Fork 2
/
context_test.go
128 lines (103 loc) · 2.91 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
126
127
128
package raiden_test
import (
"context"
"errors"
"testing"
"github.com/sev-2/raiden"
"github.com/stretchr/testify/assert"
"github.com/valyala/fasthttp"
"go.opentelemetry.io/otel/trace"
)
// Mock data for testing
type SomeParams struct {
Name string `json:"name" column:"name:s_name;type:varchar"`
}
type GetItem struct {
Id int64 `json:"id" column:"name:id;type:integer"`
SName string `json:"s_name" column:"name:s_name;type:varchar"`
}
type GetResult []GetItem
type SomeRpc struct {
raiden.Rpc
Params *SomeParams `json:"-"`
Return GetResult `json:"-"`
}
var (
mockSpan = trace.SpanFromContext(context.Background())
mockDataKey = "key"
mockDataVal = "value"
mockCtx = context.Background()
)
// Helper function to create a new Ctx instance
func newTestCtx() *raiden.Ctx {
ctx := &raiden.Ctx{
Context: mockCtx,
RequestCtx: &fasthttp.RequestCtx{},
}
ctx.SetSpan(mockSpan)
return ctx
}
func TestCtx_RequestContext(t *testing.T) {
ctx := newTestCtx()
assert.Equal(t, ctx.RequestCtx, ctx.RequestContext())
}
func TestCtx_Span(t *testing.T) {
ctx := newTestCtx()
assert.Equal(t, mockSpan, ctx.Span())
}
func TestCtx_SetSpan(t *testing.T) {
ctx := newTestCtx()
newSpan := trace.SpanFromContext(context.Background())
ctx.SetSpan(newSpan)
assert.Equal(t, newSpan, ctx.Span())
}
func TestCtx_Ctx(t *testing.T) {
ctx := newTestCtx()
assert.Equal(t, mockCtx, ctx.Ctx())
}
func TestCtx_SetCtx(t *testing.T) {
ctx := newTestCtx()
newCtx := context.WithValue(context.Background(), mockDataKey, mockDataVal)
ctx.SetCtx(newCtx)
assert.Equal(t, newCtx, ctx.Ctx())
}
func TestCtx_GetSet(t *testing.T) {
ctx := newTestCtx()
ctx.Set(mockDataKey, mockDataVal)
assert.Equal(t, mockDataVal, ctx.Get(mockDataKey))
}
func TestCtx_SendJson(t *testing.T) {
ctx := newTestCtx()
data := map[string]string{"key": "value"}
err := ctx.SendJson(data)
assert.NoError(t, err)
assert.Equal(t, "application/json", string(ctx.Response.Header.ContentType()))
}
func TestCtx_SendError(t *testing.T) {
ctx := newTestCtx()
err := ctx.SendError("error message")
assert.Error(t, err)
assert.Equal(t, fasthttp.StatusInternalServerError, err.(*raiden.ErrorResponse).StatusCode)
}
func TestCtx_SendErrorWithCode(t *testing.T) {
ctx := newTestCtx()
err := ctx.SendErrorWithCode(fasthttp.StatusBadRequest, errors.New("bad request"))
assert.Error(t, err)
assert.Equal(t, fasthttp.StatusBadRequest, err.(*raiden.ErrorResponse).StatusCode)
}
func TestCtx_WriteError(t *testing.T) {
ctx := newTestCtx()
err := &raiden.ErrorResponse{
Message: "error message",
StatusCode: fasthttp.StatusInternalServerError,
}
ctx.WriteError(err)
assert.Equal(t, "application/json", string(ctx.Response.Header.ContentType()))
}
func TestCtx_Write(t *testing.T) {
ctx := newTestCtx()
data := []byte("response data")
ctx.Write(data)
assert.Equal(t, fasthttp.StatusOK, ctx.Response.StatusCode())
assert.Equal(t, data, ctx.Response.Body())
}