-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfaker.go
74 lines (64 loc) · 1.76 KB
/
faker.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
package faker
import (
"fmt"
"testing"
taskspb "cloud.google.com/go/cloudtasks/apiv2/cloudtaskspb"
tasksbox "github.com/sinmetalcraft/gcpbox/cloudtasks"
tasksfaker "github.com/sinmetalcraft/gcpfaker/cloudtasks"
"google.golang.org/api/option"
"google.golang.org/protobuf/proto"
)
// Faker is UnitTestのために Fake 実装
type Faker struct {
org *tasksfaker.Faker
}
// NewFaker is Faker を返す
func NewFaker(t *testing.T) *Faker {
return &Faker{tasksfaker.NewFaker(t)}
}
// Stop is Stop
func (f *Faker) Stop() {
f.org.Stop()
}
// ClientOption is cloudtasks.Client に 設定する ClientOption
func (f *Faker) ClientOption() option.ClientOption {
return f.org.ClientOpt
}
// GetCreateTaskCallCount is CreateTask が実行された回数を返す
func (s *Faker) GetCreateTaskCallCount() int {
return s.org.GetCreateTaskCallCount()
}
// GetTask is 作成された Task を取得する
func (s *Faker) GetTask(i int) (*tasksbox.Task, error) {
req, err := s.org.GetCreateTaskRequest(i)
if err != nil {
return nil, err
}
buf, err := proto.Marshal(req)
if err != nil {
return nil, err
}
var tr taskspb.CreateTaskRequest
if err := proto.Unmarshal(buf, &tr); err != nil {
return nil, err
}
t := tr.GetTask()
httpReq := t.GetHttpRequest()
if httpReq == nil {
return nil, fmt.Errorf("http request is required")
}
method, err := tasksbox.HttpMethodProtoToHttpMethod(httpReq.GetHttpMethod())
if err != nil {
return nil, err
}
return &tasksbox.Task{
Audience: "", // TODO AuthorizationHeader
RelativeURI: httpReq.Url,
Headers: httpReq.Headers,
Method: method,
ScheduleTime: t.GetScheduleTime().AsTime(),
Deadline: t.GetDispatchDeadline().AsDuration(),
Body: httpReq.GetBody(),
Name: t.GetName(),
}, nil
}