This repository has been archived by the owner on Jan 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrequest_test.go
112 lines (89 loc) · 2.74 KB
/
request_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
package fastjsonrpc_test
import (
"bytes"
"reflect"
"testing"
. "github.com/serjvanilla/fastjsonrpc"
"github.com/valyala/fasthttp"
)
func TestRequestID(t *testing.T) {
r := NewRepository()
r.Register("ping", func(ctx *RequestCtx) {
if !bytes.Equal(ctx.ID(), []byte("1")) {
t.Fatalf("unexpected id: `%s`", ctx.ID())
}
})
ctx := new(fasthttp.RequestCtx)
ctx.Request.Header.SetMethod(fasthttp.MethodPost)
ctx.Request.SetBodyString(`{"jsonrpc":"2.0","method":"ping","id":1}`)
r.RequestHandler()(ctx)
}
func TestRequestMethod(t *testing.T) {
r := NewRepository()
r.Register("ping", func(ctx *RequestCtx) {
if !bytes.Equal(ctx.Method(), []byte("ping")) {
t.Fatalf("unexpected method: `%s`", ctx.Method())
}
})
ctx := new(fasthttp.RequestCtx)
ctx.Request.Header.SetMethod(fasthttp.MethodPost)
ctx.Request.SetBodyString(`{"jsonrpc":"2.0","method":"ping","id":1}`)
r.RequestHandler()(ctx)
}
func TestRequestParams(t *testing.T) {
r := NewRepository()
r.Register("echo", func(ctx *RequestCtx) {
params := ctx.Params()
if !bytes.Equal(params.MarshalTo(nil), []byte(`"ping"`)) {
t.Fatalf("unexpected params: `%s`", ctx.Params())
}
})
ctx := new(fasthttp.RequestCtx)
ctx.Request.Header.SetMethod(fasthttp.MethodPost)
ctx.Request.SetBodyString(`{"jsonrpc":"2.0","method":"echo","params":"ping","id":1}`)
r.RequestHandler()(ctx)
}
func TestRequestParamsBytes(t *testing.T) {
r := NewRepository()
r.Register("echo", func(ctx *RequestCtx) {
params := ctx.ParamsBytes()
if !bytes.Equal(params, []byte(`"ping"`)) {
t.Fatalf("unexpected params: `%s`", ctx.ParamsBytes())
}
})
ctx := new(fasthttp.RequestCtx)
ctx.Request.Header.SetMethod(fasthttp.MethodPost)
ctx.Request.SetBodyString(`{"jsonrpc":"2.0","method":"echo","params":"ping","id":1}`)
r.RequestHandler()(ctx)
}
func TestRequestParamsUnmarshal(t *testing.T) {
r := NewRepository()
r.Register("echo", func(ctx *RequestCtx) {
var param string
err := ctx.ParamsUnmarshal(¶m)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
if param != "ping" {
t.Fatalf("unexpected param: `%s`", param)
}
})
ctx := new(fasthttp.RequestCtx)
ctx.Request.Header.SetMethod(fasthttp.MethodPost)
ctx.Request.SetBodyString(`{"jsonrpc":"2.0","method":"echo","params":"ping","id":1}`)
r.RequestHandler()(ctx)
}
func TestRequestParamsUnmarshalEmpty(t *testing.T) {
r := NewRepository()
r.Register("ping", func(ctx *RequestCtx) {
var param string
err := ctx.ParamsUnmarshal(¶m)
if !reflect.DeepEqual(err, ErrInvalidParams()) {
t.Fatalf("unexpected error: %s", err)
}
})
ctx := new(fasthttp.RequestCtx)
ctx.Request.Header.SetMethod(fasthttp.MethodPost)
ctx.Request.SetBodyString(`{"jsonrpc":"2.0","method":"ping","id":1}`)
r.RequestHandler()(ctx)
}