Skip to content

Commit

Permalink
feat: WebMetric to support string body responses (#1212)
Browse files Browse the repository at this point in the history
Signed-off-by: Andrii Perenesenko <andrii.perenesenko@gmail.com>
  • Loading branch information
perenesenko authored May 22, 2021
1 parent 60092c3 commit 9a028d1
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 9 deletions.
3 changes: 2 additions & 1 deletion metricproviders/webmetric/webmetric.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ func (p *Provider) parseResponse(metric v1alpha1.Metric, response *http.Response

err = json.Unmarshal(bodyBytes, &data)
if err != nil {
return "", v1alpha1.AnalysisPhaseError, fmt.Errorf("Could not parse JSON body: %v", err)
// non JSON body return as string
return string(bodyBytes), v1alpha1.AnalysisPhaseSuccessful, nil
}

fullResults, err := p.jsonParser.FindResults(data)
Expand Down
58 changes: 50 additions & 8 deletions metricproviders/webmetric/webmetric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ func TestRunSuite(t *testing.T) {
expectedPhase: v1alpha1.AnalysisPhaseError,
expectedErrorMessage: "unsupported protocol scheme",
},
// When_200Response_And_EmptyBody_Then_Error
// When_200Response_And_EmptyBody_Then_Succeed
{
webServerStatus: 200,
webServerResponse: ``,
Expand All @@ -395,11 +395,10 @@ func TestRunSuite(t *testing.T) {
},
},
},
expectedValue: "true",
expectedPhase: v1alpha1.AnalysisPhaseError,
expectedErrorMessage: "Could not parse JSON body",
expectedValue: "",
expectedPhase: v1alpha1.AnalysisPhaseSuccessful,
},
// When_200Response_And_InvalidBody_Then_Error
// When_200Response_And_NonJsonBody_Then_Succeed
{
webServerStatus: 200,
webServerResponse: `test: notJson`,
Expand All @@ -413,9 +412,8 @@ func TestRunSuite(t *testing.T) {
},
},
},
expectedValue: "true",
expectedPhase: v1alpha1.AnalysisPhaseError,
expectedErrorMessage: "Could not parse JSON body",
expectedValue: "test: notJson",
expectedPhase: v1alpha1.AnalysisPhaseSuccessful,
},
// When_200Response_And_JsonPathHasNoMatch_Then_Error
{
Expand All @@ -435,6 +433,50 @@ func TestRunSuite(t *testing.T) {
expectedPhase: v1alpha1.AnalysisPhaseError,
expectedErrorMessage: "Could not find JSONPath in body",
},

// When_200Response_And_NilBody_Then_Succeed
{
webServerStatus: 200,
metric: v1alpha1.Metric{
Name: "foo",
SuccessCondition: "true",
FailureCondition: "true",
Provider: v1alpha1.MetricProvider{
Web: &v1alpha1.WebMetric{},
},
},
expectedPhase: v1alpha1.AnalysisPhaseSuccessful,
},
// When_200Response_And_AnyJson_Then_Succeed
{
webServerStatus: 200,
webServerResponse: `{}`,
metric: v1alpha1.Metric{
Name: "foo",
SuccessCondition: "true",
Provider: v1alpha1.MetricProvider{
Web: &v1alpha1.WebMetric{},
},
},
expectedValue: "{}",
expectedPhase: v1alpha1.AnalysisPhaseSuccessful,
expectedErrorMessage: "",
},
// When_non200Response_And_NoBody_Then_Failure
{
webServerStatus: 400,
webServerResponse: ``,
metric: v1alpha1.Metric{
Name: "foo",
SuccessCondition: "",
Provider: v1alpha1.MetricProvider{
Web: &v1alpha1.WebMetric{},
},
},
expectedValue: "",
expectedPhase: v1alpha1.AnalysisPhaseError,
expectedErrorMessage: "",
},
}

// Run
Expand Down

0 comments on commit 9a028d1

Please sign in to comment.