Skip to content

Commit

Permalink
Merge pull request #1360 from flanksource/pr/fix-json-array
Browse files Browse the repository at this point in the history
fix: json array responses
  • Loading branch information
moshloop authored Oct 18, 2023
2 parents 9ee0ccd + 3486268 commit 6455210
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 43 deletions.
2 changes: 1 addition & 1 deletion api/v1/checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ type HTTPCheck struct {
ResponseCodes []int `yaml:"responseCodes,omitempty" json:"responseCodes,omitempty"`
// Exact response content expected to be returned by the endpoint.
ResponseContent string `yaml:"responseContent,omitempty" json:"responseContent,omitempty"`
// Path and value to of expect JSON response by the endpoint
// Deprecated, use expr and jsonpath function
ResponseJSONContent *JSONCheck `yaml:"responseJSONContent,omitempty" json:"responseJSONContent,omitempty"`
// Maximum number of days until the SSL Certificate expires.
MaxSSLExpiry int `yaml:"maxSSLExpiry,omitempty" json:"maxSSLExpiry,omitempty"`
Expand Down
54 changes: 14 additions & 40 deletions checks/http.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package checks

import (
"encoding/json"
"fmt"
"net/url"
"strconv"
"strings"
"time"

"github.com/PaesslerAG/jsonpath"
"github.com/flanksource/canary-checker/api/context"
"github.com/flanksource/commons/http"
"github.com/flanksource/duty/models"
Expand Down Expand Up @@ -210,29 +210,29 @@ func (c *HTTPChecker) Check(ctx *context.Context, extConfig external.Check) pkg.
"json": make(map[string]any),
}

responseBody, err := response.AsString()
if err != nil {
return results.ErrorMessage(err)
}
data["content"] = responseBody

if response.IsJSON() {
json, err := response.AsJSON()
data["json"] = json
if err == nil {
data["json"] = json
if check.ResponseJSONContent != nil && check.ResponseJSONContent.Path != "" {
err := checkJSONContent(json, check.ResponseJSONContent)
if err != nil {
return results.ErrorMessage(err)
}
}
var jsonContent interface{}
if err := json.Unmarshal([]byte(responseBody), &jsonContent); err == nil {
data["json"] = jsonContent
} else if check.Test.IsEmpty() {
return results.Failf("invalid json response: %v", err)
} else {
ctx.Tracef("ignoring invalid json response %v", err)
}
} else {
responseBody, _ := response.AsString()
data["content"] = responseBody
}

result.AddData(data)

if check.ResponseJSONContent != nil {
ctx.Tracef("jsonContent is deprecated")
}

if ok := response.IsOK(check.ResponseCodes...); !ok {
return results.Failf("response code invalid %d != %v", status, check.ResponseCodes)
}
Expand Down Expand Up @@ -272,29 +272,3 @@ func statusCodeToClass(statusCode int) string {
return "unknown"
}
}

func checkJSONContent(jsonContent map[string]any, jsonCheck *v1.JSONCheck) error {
if jsonCheck == nil {
return nil
}

jsonResult, err := jsonpath.Get(jsonCheck.Path, jsonContent)
if err != nil {
return fmt.Errorf("error getting jsonPath: %w", err)
}

switch s := jsonResult.(type) {
case string:
if s != jsonCheck.Value {
return fmt.Errorf("%v not equal to %v", s, jsonCheck.Value)
}
case fmt.Stringer:
if s.String() != jsonCheck.Value {
return fmt.Errorf("%v not equal to %v", s.String(), jsonCheck.Value)
}
default:
return fmt.Errorf("json response could not be parsed back to string")
}

return nil
}
2 changes: 1 addition & 1 deletion config/deploy/crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3208,7 +3208,7 @@ spec:
description: Exact response content expected to be returned by the endpoint.
type: string
responseJSONContent:
description: Path and value to of expect JSON response by the endpoint
description: Deprecated, use expr and jsonpath function
properties:
path:
type: string
Expand Down
2 changes: 1 addition & 1 deletion config/deploy/manifests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3208,7 +3208,7 @@ spec:
description: Exact response content expected to be returned by the endpoint.
type: string
responseJSONContent:
description: Path and value to of expect JSON response by the endpoint
description: Deprecated, use expr and jsonpath function
properties:
path:
type: string
Expand Down

0 comments on commit 6455210

Please sign in to comment.