/
request.go
90 lines (76 loc) · 2.22 KB
/
request.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
package request
import (
"bytes"
"fmt"
"github.com/5sigma/spyder/config"
"github.com/5sigma/spyder/endpoint"
"io/ioutil"
"net/http"
"path"
"time"
)
// Do - Performs the request for a given endpoint configuration. This will
// expand all variables, fake data, and execute any transforms and onComplete
// scripts specified.
func Do(epConfig *endpoint.EndpointConfig) (*Response, error) {
var (
client = &http.Client{}
err error
req *http.Request
requestData = []byte{}
scriptEngine = NewScriptEngine(epConfig)
)
if epConfig.RequestMethod() == "POST" || epConfig.RequestMethod() == "HEAD" || epConfig.RequestMethod() == "PUT" {
requestData = epConfig.RequestData()
if err != nil {
return nil, err
}
}
scriptEngine.SetPayload(requestData)
// Perform transformation
// executes scripts with the payload set so that they are capable of modifying
// it before the request is made.
if len(epConfig.Transform) > 0 {
for _, transform := range epConfig.Transform {
scriptPath := path.Join(config.ProjectPath, "scripts", transform) + ".js"
scriptEngine.ExecuteFile(scriptPath)
requestData = scriptEngine.Payload
}
}
req, err = http.NewRequest(epConfig.RequestMethod(),
epConfig.RequestURL(), bytes.NewReader(requestData))
req.Header = epConfig.Headers
scriptEngine.Request = req
// Make the request and calculate its flight time.
start := time.Now()
rawResponse, err := client.Do(req)
elapsed := time.Since(start)
if err != nil {
return nil, err
}
// Read response data
contentBytes, err := ioutil.ReadAll(rawResponse.Body)
if err != nil {
return nil, err
}
res := &Response{
Response: rawResponse,
RequestTime: elapsed,
Content: contentBytes,
Request: rawResponse.Request,
Payload: requestData,
}
// Execute any post request scripts that are listed in the config
if len(epConfig.OnComplete) > 0 {
scriptEngine.SetResponse(res)
for _, onComplete := range epConfig.OnComplete {
scriptPath := path.Join(config.ProjectPath, "scripts", onComplete) + ".js"
err := scriptEngine.ExecuteFile(scriptPath)
if err != nil {
return res, fmt.Errorf("Error parsing script (%s): %s",
scriptPath, err.Error())
}
}
}
return res, nil
}