/
mocks.go
89 lines (84 loc) · 2.1 KB
/
mocks.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
package testutils
import (
"encoding/json"
"net/http"
"net/url"
"github.com/bot-api/telegram"
"github.com/m0sth8/httpmock"
"golang.org/x/net/context"
)
// UpdatesResponder provides a useful responder that returns updates from ch
func UpdatesResponder(ctx context.Context, ch <-chan []telegram.Update) httpmock.Responder {
return func(req *http.Request) (*http.Response, error) {
select {
case <-ctx.Done():
return nil, ctx.Err()
case update := <-ch:
data, err := json.Marshal(update)
if err != nil {
return httpmock.NewJsonResponse(200,
telegram.APIResponse{
Ok: false,
Description: err.Error(),
},
)
}
raw := json.RawMessage(data)
return httpmock.NewJsonResponse(200,
telegram.APIResponse{
Ok: true,
Result: &raw,
},
)
}
}
}
// ReceivedMessage contains information about message that are sent by api
type ReceivedMessage struct {
Values url.Values
URL url.URL
}
// SendResponder helps to test telegram api.
// Returns received message to out channel,
// Waits for result channel with object to return in API.
// If result channel is nil, then returns empty telegram.Message.
// If result channel has error, then returns APIResponse with Ok False and Description.
func SendResponder(ctx context.Context,
result <-chan interface{}) (httpmock.Responder, <-chan ReceivedMessage) {
out := make(chan ReceivedMessage)
return func(req *http.Request) (*http.Response, error) {
req.ParseForm()
select {
case out <- ReceivedMessage{
Values: req.Form,
URL: *req.URL,
}:
case <-ctx.Done():
return nil, ctx.Err()
}
select {
case data := <-result:
if err, casted := data.(error); casted {
return httpmock.NewJsonResponse(200,
telegram.APIResponse{
Ok: false,
Description: err.Error(),
},
)
}
rawData, err := json.Marshal(data)
if err != nil {
return nil, err
}
raw := json.RawMessage(rawData)
return httpmock.NewJsonResponse(200,
telegram.APIResponse{
Ok: true,
Result: &raw,
},
)
case <-ctx.Done():
return nil, ctx.Err()
}
}, out
}