-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
92 lines (74 loc) · 2.15 KB
/
http.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 fasthttp
import (
"errors"
"fmt"
"sync"
"github.com/grafana/sobek"
"go.k6.io/k6/js/common"
"go.k6.io/k6/js/modules"
"go.k6.io/k6/lib/netext/httpext"
)
type RootModule struct{}
// ModuleInstance represents an instance of the HTTP module for every VU.
type ModuleInstance struct {
vu modules.VU
exports *sobek.Object
}
var (
_ modules.Module = &RootModule{}
_ modules.Instance = &ModuleInstance{}
)
func init() {
modules.Register("k6/x/fasthttp", New())
}
// New returns a pointer to a new HTTP RootModule.
func New() *RootModule {
return &RootModule{}
}
// NewModuleInstance returns an HTTP module instance for each VU.
func (r *RootModule) NewModuleInstance(vu modules.VU) modules.Instance {
rt := vu.Runtime()
mi := &ModuleInstance{
vu: vu,
exports: rt.NewObject(),
}
mustExport := func(name string, value interface{}) {
if err := mi.exports.Set(name, value); err != nil {
common.Throw(rt, err)
}
}
mustExport("FileStream", mi.FileStream)
mustExport("Client", mi.Client)
mustExport("Request", mi.Request)
mustExport("checkstatus", mi.CheckStatus)
return mi
}
// RequestWrapper Create new request with New RequestWrapper({})
func (mi *ModuleInstance) Request(call sobek.ConstructorCall, rt *sobek.Runtime) *sobek.Object {
if len(call.Arguments) > 2 || len(call.Arguments) == 0 {
common.Throw(mi.vu.Runtime(), errors.New("req constructor expects 1 or 2 args"))
}
var req RequestWrapper
req.Url = call.Arguments[0].String()
req.reqPool = &sync.Pool{}
if len(call.Arguments) > 1 {
err := rt.ExportTo(call.Argument(1), &req)
if err != nil {
common.Throw(rt, fmt.Errorf("request constructor expects first argument to be RequestWrapper got error %v", err))
}
if req.ResponseType != "" {
responseType, err := httpext.ResponseTypeString(req.ResponseType)
if err != nil {
common.Throw(mi.vu.Runtime(), fmt.Errorf("Invalid response type %v", err))
}
req.responseType = responseType
}
}
return mi.vu.Runtime().ToValue(&req).ToObject(rt)
}
// Exports returns the JS values this module exports.
func (mi *ModuleInstance) Exports() modules.Exports {
return modules.Exports{
Default: mi.exports,
}
}