From 9a028d124dbe612f29d361cb3efa27ea84bfbbf4 Mon Sep 17 00:00:00 2001 From: Andrii Perenesenko Date: Fri, 21 May 2021 18:05:37 -0700 Subject: [PATCH] feat: WebMetric to support string body responses (#1212) Signed-off-by: Andrii Perenesenko --- metricproviders/webmetric/webmetric.go | 3 +- metricproviders/webmetric/webmetric_test.go | 58 ++++++++++++++++++--- 2 files changed, 52 insertions(+), 9 deletions(-) diff --git a/metricproviders/webmetric/webmetric.go b/metricproviders/webmetric/webmetric.go index 8aea0d17c1..82c577d57c 100644 --- a/metricproviders/webmetric/webmetric.go +++ b/metricproviders/webmetric/webmetric.go @@ -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) diff --git a/metricproviders/webmetric/webmetric_test.go b/metricproviders/webmetric/webmetric_test.go index 5d663516da..148123e9dd 100644 --- a/metricproviders/webmetric/webmetric_test.go +++ b/metricproviders/webmetric/webmetric_test.go @@ -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: ``, @@ -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`, @@ -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 { @@ -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