Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit dd48ed0

Browse files
committedNov 28, 2024··
feat: - Use "error" in the expected HTTP status code field to verify that requests are returning an error
1 parent 181f86b commit dd48ed0

7 files changed

+30
-24
lines changed
 

‎CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## v1.0.23 (next release)
44

55
- Location selection for http checks (can be enabled via STEADYBIT_EXTENSION_ENABLE_LOCATION_SELECTION env var, requires platform => 2.1.27)
6+
- Use "error" in the expected HTTP status code field to verify that requests are returning an error
67

78
## v1.0.22
89

‎exthttpcheck/check.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ var (
3737
)
3838

3939
type HTTPCheckState struct {
40-
ExpectedStatusCodes []int
40+
ExpectedStatusCodes []string
4141
DelayBetweenRequestsInMS int64
4242
Timeout time.Time
4343
ResponsesContains string
@@ -221,11 +221,11 @@ func requestWorker(executionRunData *ExecutionRunData, state *HTTPCheckState, ch
221221
Value: float64(now.Sub(started).Milliseconds()),
222222
Timestamp: now,
223223
}
224-
responseStatusWasExpected = false
224+
responseStatusWasExpected = slices.Contains(state.ExpectedStatusCodes, "error")
225225
} else {
226226
responseTimeValue := float64(ended.Sub(requestWritten).Milliseconds())
227227
log.Debug().Msgf("Got response %s", response.Status)
228-
responseStatusWasExpected = slices.Contains(state.ExpectedStatusCodes, response.StatusCode)
228+
responseStatusWasExpected = slices.Contains(state.ExpectedStatusCodes, strconv.Itoa(response.StatusCode))
229229
metricMap := map[string]string{
230230
"url": req.URL.String(),
231231
"http_status": strconv.Itoa(response.StatusCode),

‎exthttpcheck/check_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func TestAction_Prepare(t *testing.T) {
5151
}),
5252

5353
wantedState: &HTTPCheckState{
54-
ExpectedStatusCodes: []int{200, 201, 202, 203, 204, 205, 206, 207, 208, 209},
54+
ExpectedStatusCodes: []string{"200", "201", "202", "203", "204", "205", "206", "207", "208", "209"},
5555
DelayBetweenRequestsInMS: 1000,
5656
Timeout: time.Now(),
5757
ResponsesContains: "test",

‎exthttpcheck/fixAmount_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func TestNewHTTPCheckActionFixedAmount_Prepare(t *testing.T) {
5252
}),
5353

5454
wantedState: &HTTPCheckState{
55-
ExpectedStatusCodes: []int{200, 201, 202, 203, 204, 205, 206, 207, 208, 209},
55+
ExpectedStatusCodes: []string{"200", "201", "202", "203", "204", "205", "206", "207", "208", "209"},
5656
DelayBetweenRequestsInMS: 1000,
5757
Timeout: time.Now(),
5858
ResponsesContains: "test",

‎exthttpcheck/periodically_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func TestNewHTTPCheckActionPeriodically_Prepare(t *testing.T) {
5353
}),
5454

5555
wantedState: &HTTPCheckState{
56-
ExpectedStatusCodes: []int{200, 201, 202, 203, 204, 205, 206, 207, 208, 209},
56+
ExpectedStatusCodes: []string{"200", "201", "202", "203", "204", "205", "206", "207", "208", "209"},
5757
DelayBetweenRequestsInMS: 500,
5858
Timeout: time.Now(),
5959
ResponsesContains: "test",

‎exthttpcheck/util.go

+19-14
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ import (
1313
)
1414

1515
// resolveStatusCodeExpression resolves the given status code expression into a list of status codes
16-
func resolveStatusCodeExpression(statusCodes string) ([]int, *action_kit_api.ActionKitError) {
17-
result := make([]int, 0)
16+
func resolveStatusCodeExpression(statusCodes string) ([]string, *action_kit_api.ActionKitError) {
17+
result := make([]string, 0)
1818
for _, code := range strings.Split(strings.Trim(statusCodes, " "), ";") {
1919
if strings.Contains(code, "-") {
2020
rangeParts := strings.Split(code, "-")
@@ -45,7 +45,7 @@ func resolveStatusCodeExpression(statusCodes string) ([]int, *action_kit_api.Act
4545
Title: fmt.Sprintf("Invalid status code '%d'. Status code should be between 100 and 599.", i),
4646
}
4747
}
48-
result = append(result, i)
48+
result = append(result, strconv.Itoa(i))
4949
}
5050
} else {
5151
if len(code) == 0 {
@@ -54,20 +54,25 @@ func resolveStatusCodeExpression(statusCodes string) ([]int, *action_kit_api.Act
5454
Title: "Status code is required.",
5555
}
5656
}
57-
parsed, err := strconv.Atoi(code)
58-
if err != nil {
59-
log.Error().Msgf("Invalid status code '%s'", code)
60-
return nil, &action_kit_api.ActionKitError{
61-
Title: fmt.Sprintf("Invalid status code '%s'. Please use '-' for ranges and ';' for enumerations. Example: '200-399;429'", code),
57+
if code == "error" {
58+
result = append(result, "error")
59+
} else {
60+
61+
parsed, err := strconv.Atoi(code)
62+
if err != nil {
63+
log.Error().Msgf("Invalid status code '%s'", code)
64+
return nil, &action_kit_api.ActionKitError{
65+
Title: fmt.Sprintf("Invalid status code '%s'. Please use '-' for ranges and ';' for enumerations. Example: '200-399;429'", code),
66+
}
6267
}
63-
}
64-
if parsed < 100 || parsed > 599 {
65-
log.Error().Msgf("Invalid status code '%d'", parsed)
66-
return nil, &action_kit_api.ActionKitError{
67-
Title: fmt.Sprintf("Invalid status code '%d'. Status code should be between 100 and 599.", parsed),
68+
if parsed < 100 || parsed > 599 {
69+
log.Error().Msgf("Invalid status code '%d'", parsed)
70+
return nil, &action_kit_api.ActionKitError{
71+
Title: fmt.Sprintf("Invalid status code '%d'. Status code should be between 100 and 599.", parsed),
72+
}
6873
}
74+
result = append(result, strconv.Itoa(parsed))
6975
}
70-
result = append(result, parsed)
7176
}
7277
}
7378
return result, nil

‎exthttpcheck/util_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,23 @@ func Test_resolveStatusCodeExpression(t *testing.T) {
1313
tests := []struct {
1414
name string
1515
args args
16-
want []int
16+
want []string
1717
error *action_kit_api.ActionKitError
1818
}{
1919
{
2020
name: "Should return status codes with range",
2121
args: args{
2222
statusCodes: "200-209",
2323
},
24-
want: []int{200, 201, 202, 203, 204, 205, 206, 207, 208, 209},
24+
want: []string{"200", "201", "202", "203", "204", "205", "206", "207", "208", "209"},
2525
error: nil,
2626
},
2727
{
2828
name: "Should return status codes with range and enum",
2929
args: args{
30-
statusCodes: "201-202;209",
30+
statusCodes: "201-202;error;209",
3131
},
32-
want: []int{201, 202, 209},
32+
want: []string{"201", "202", "error", "209"},
3333
error: nil,
3434
},
3535
{

0 commit comments

Comments
 (0)
Please sign in to comment.