/
k6.check_http.go
106 lines (98 loc) · 3.05 KB
/
k6.check_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
package worker
import (
"encoding/json"
"fmt"
"time"
)
func (checker *httpChecker) checkHTTPMultiStepsRequest(c SyntheticCheck) testStatus {
start := time.Now()
testStatus := testStatus{
status: testStatusOK,
}
isCheckTestReq := c.CheckTestRequest.URL != ""
scriptSnippet := CreateScriptSnippet(c)
respValue, exeErr := checker.k6Scripter.execute(scriptSnippet)
checker.timers["duration"] = timeInMs(time.Since(start))
response := make(map[string]interface{}, 0)
err := json.Unmarshal([]byte(respValue), &response)
if err != nil {
testStatus.status = testStatusError
testStatus.msg = fmt.Sprintf("error while parsing response: %v", err)
return testStatus
}
if isCheckTestReq {
resSteps := response["steps"]
checker.testBody = map[string]interface{}{
"multiStepPreview": true,
"body": resSteps,
}
// finishTestRequest(c, _testBody)
return testStatus
}
if exeErr != nil {
testStatus.status = testStatusError
testStatus.msg = fmt.Sprintf("error while executing script: %v", exeErr)
checker.assertions = append(checker.assertions, map[string]string{
"type": "status_code",
"reason": "Error while executing script",
"actual": "N/A",
"status": testStatusFail,
})
checker.assertions = append(checker.assertions, map[string]string{
"type": "response_time",
"reason": "Error while executing script",
"actual": "N/A",
"status": testStatusFail,
})
} else {
if allAssertions, ok := response["assertions"].(map[string]interface{}); ok {
isfail := false
stepNumber := 1
for _, assertions := range allAssertions {
isFirst := true
if assertList, ok := assertions.(map[string]interface{}); ok {
for _, assert := range assertList {
newAsrt := make(map[string]string)
if asrt, ok1 := assert.(map[string]interface{}); ok1 {
for k, v := range asrt {
newAsrt[k] = fmt.Sprintf("%v", v)
}
} else {
if asrt, ok1 := assert.(map[string]string); ok1 {
for k, v := range asrt {
newAsrt[k] = fmt.Sprintf("%v", v)
}
}
}
if isFirst {
newAsrt["step"] = fmt.Sprintf("Step%v", stepNumber)
isFirst = false
stepNumber++
}
checker.assertions = append(checker.assertions, newAsrt)
if newAsrt["status"] == testStatusFail && !isfail {
isfail = true
testStatus.status = testStatusFail
testStatus.msg = "one or more assertions failed, " + newAsrt["reason"]
}
}
}
}
} else {
for _, allallAssertions := range c.Request.HTTPMultiSteps {
for _, assert := range allallAssertions.Request.Assertions.HTTP.Cases {
checker.assertions = append(checker.assertions, map[string]string{
"type": assert.Type,
"reason": "should be " + assert.Config.Operator + " " + assert.Config.Value,
"actual": "N/A",
"status": testStatusFail,
})
}
}
}
}
resultStr, _ := json.Marshal(checker.assertions)
checker.attrs.PutStr("assertions", string(resultStr))
// finishCheckRequest(c, testStatus, checker.timers, checker.attrs)
return testStatus
}