Openstatus www.openstatus.dev

๐Ÿš€ Body string assertion (#740)

* ๐Ÿ”ฅ body string assertion

* ๐Ÿš€ string assertions

* ๐Ÿš€ string assertions

authored by

Thibault Le Ouay and committed by
GitHub
326ca317 4f74e6bf

+16 -6
+9 -1
apps/checker/cmd/main.go
··· 123 123 isSuccessfull = isSuccessfull && target.HeaderEvaluate(res.Headers) 124 124 125 125 case request.AssertionTextBody: 126 - fmt.Println("assertion type", assert.AssertionType) 126 + var target assertions.StringTargetType 127 + if err := json.Unmarshal(a, &target); err != nil { 128 + return fmt.Errorf("unable to unmarshal IntTarget: %w", err) 129 + } 130 + isSuccessfull = isSuccessfull && target.StringEvaluate(res.Body) 131 + 127 132 case request.AssertionStatus: 128 133 var target assertions.StatusTarget 129 134 if err := json.Unmarshal(a, &target); err != nil { ··· 148 153 // it's in error if not successful 149 154 if isSuccessfull { 150 155 res.Error = 0 156 + // Small trick to avoid sending the body at the moment to TB 157 + res.Body = "" 151 158 } else { 152 159 res.Error = 1 153 160 } ··· 194 201 WorkspaceID: req.WorkspaceID, 195 202 Error: 1, 196 203 Assertions: assertionAsString, 204 + Body: "", 197 205 }); err != nil { 198 206 log.Ctx(ctx).Error().Err(err).Msg("failed to send event to tinybird") 199 207 }
+3 -1
apps/checker/ping.go
··· 32 32 Headers string `json:"headers,omitempty"` 33 33 Error uint8 `json:"error"` 34 34 Assertions string `json:"assertions"` 35 + Body string `json:"body,omitempty"` 35 36 } 36 37 37 38 type Timing struct { ··· 126 127 } 127 128 defer response.Body.Close() 128 129 129 - _, err = io.ReadAll(response.Body) 130 + body, err := io.ReadAll(response.Body) 130 131 if err != nil { 131 132 return PingData{ 132 133 Latency: latency, ··· 167 168 URL: inputData.URL, 168 169 Timing: string(timingAsString), 169 170 Headers: string(headersAsString), 171 + Body: string(body), 170 172 }, nil 171 173 } 172 174
+4 -4
apps/checker/pkg/assertions/assertions.go
··· 28 28 } 29 29 30 30 type StringTargetType struct { 31 - Comparator request.StringComparator 32 - Target string 31 + Comparator request.StringComparator `json:"compare"` 32 + Target string `json:"target"` 33 33 } 34 34 35 35 func (target StringTargetType) StringEvaluate(s string) bool { 36 36 switch target.Comparator { 37 37 case request.StringContains: 38 - return strings.Contains(target.Target, s) 38 + return strings.Contains(s, target.Target) 39 39 case request.StringNotContains: 40 - return !strings.Contains(target.Target, s) 40 + return !strings.Contains(s, target.Target) 41 41 case request.StringEmpty: 42 42 return s == "" 43 43 case request.StringNotEmpty: