-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathapi_http.go
110 lines (98 loc) · 2.82 KB
/
api_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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
*
* k6 - a next-generation load testing tool
* Copyright (C) 2016 Load Impact
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package js
import (
"encoding/json"
"github.com/loadimpact/k6/lib"
"github.com/loadimpact/k6/stats"
"io"
"io/ioutil"
"net/http"
"net/http/httptrace"
"strconv"
"strings"
)
type HTTPResponse struct {
Status int
}
type HTTPParams struct {
Headers map[string]string `json:"headers"`
Tags map[string]string `json:"tags"`
}
func (a JSAPI) HTTPRequest(method, url, body string, paramData string) map[string]interface{} {
bodyReader := io.Reader(nil)
if body != "" {
bodyReader = strings.NewReader(body)
}
req, err := http.NewRequest(method, url, bodyReader)
if err != nil {
throw(a.vu.vm, err)
}
var params HTTPParams
if err := json.Unmarshal([]byte(paramData), ¶ms); err != nil {
throw(a.vu.vm, err)
}
for key, value := range params.Headers {
req.Header.Set(key, value)
}
tags := map[string]string{
"vu": a.vu.IDString,
"status": "0",
"method": method,
"url": url,
"group": a.vu.group.Path,
}
for key, value := range params.Tags {
tags[key] = value
}
tracer := lib.Tracer{}
res, err := a.vu.HTTPClient.Do(req.WithContext(httptrace.WithClientTrace(a.vu.ctx, tracer.Trace())))
if err != nil {
a.vu.Samples = append(a.vu.Samples, tracer.Done().Samples(tags)...)
throw(a.vu.vm, err)
}
tags["status"] = strconv.Itoa(res.StatusCode)
resBody, err := ioutil.ReadAll(res.Body)
if err != nil {
a.vu.Samples = append(a.vu.Samples, tracer.Done().Samples(tags)...)
throw(a.vu.vm, err)
}
_ = res.Body.Close()
trail := tracer.Done()
a.vu.Samples = append(a.vu.Samples, trail.Samples(tags)...)
headers := make(map[string]string)
for k, v := range res.Header {
headers[k] = strings.Join(v, ", ")
}
return map[string]interface{}{
"status": res.StatusCode,
"body": string(resBody),
"headers": headers,
"timings": map[string]float64{
"duration": stats.D(trail.Duration),
"blocked": stats.D(trail.Blocked),
"looking_up": stats.D(trail.LookingUp),
"connecting": stats.D(trail.Connecting),
"sending": stats.D(trail.Sending),
"waiting": stats.D(trail.Waiting),
"receiving": stats.D(trail.Receiving),
},
}
}