This repository has been archived by the owner on Nov 5, 2023. It is now read-only.
/
context_rest.go
92 lines (76 loc) · 1.92 KB
/
context_rest.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
package edgetest
import (
"sync/atomic"
"time"
"github.com/ronaksoft/rony"
dummyGateway "github.com/ronaksoft/rony/internal/gateway/dummy"
)
/*
Creation Time: 2020 - Dec - 09
Created by: (ehsan)
Maintainers:
1. Ehsan N. Moosa (E2)
Auditor: Ehsan N. Moosa (E2)
Copyright Ronak Software Group 2020
*/
type restCtx struct {
id uint64
gw *dummyGateway.Gateway
err error
expect CheckFunc
doneCh chan struct{}
kvs []*rony.KeyValue
method string
path string
body []byte
}
func newRESTContext(gw *dummyGateway.Gateway) *restCtx {
c := &restCtx{
id: atomic.AddUint64(&connID, 1),
gw: gw,
doneCh: make(chan struct{}, 1),
}
return c
}
// Request set the request you wish to send to the server
func (c *restCtx) Request(method, path string, body []byte, kvs ...*rony.KeyValue) *restCtx {
c.method = method
c.path = path
c.body = body
c.kvs = kvs
return c
}
// Expect let you set what you expect to receive. If cf is set, then you can do more
// checks on the response and return error if the response was not fully acceptable
func (c *restCtx) Expect(cf CheckFunc) *restCtx {
c.expect = cf
return c
}
func (c *restCtx) RunShort(kvs ...*rony.KeyValue) error {
return c.Run(time.Second*10, kvs...)
}
func (c *restCtx) RunLong(kvs ...*rony.KeyValue) error {
return c.Run(time.Minute, kvs...)
}
func (c *restCtx) Run(timeout time.Duration, kvs ...*rony.KeyValue) error {
// We return error early if we have encountered error before Run
if c.err != nil {
return c.err
}
go func() {
respBody, respHdr := c.gw.REST(c.id, c.method, c.path, c.body, kvs...)
hdr := make([]*rony.KeyValue, 0, len(respHdr))
for k, v := range respHdr {
hdr = append(hdr, &rony.KeyValue{Key: k, Value: v})
}
c.err = c.expect(respBody, hdr...)
c.doneCh <- struct{}{}
}()
// Wait for Response(s)
select {
case <-c.doneCh:
case <-time.After(timeout):
c.err = ErrTimeout
}
return c.err
}