/// <summary> /// Verify that the expectations in the scenario are met by the response /// </summary> /// <param name="scenario"></param> /// <param name="actualResponse"></param> /// <param name="detectedErrors"></param> public static void ValidateExpectations(this ScenarioDefinition scenario, HttpResponse actualResponse, IssueLogger issues) { if (scenario == null) { throw new ArgumentNullException("scenario"); } if (actualResponse == null) { throw new ArgumentNullException("actualResponse"); } if (issues == null) { throw new ArgumentNullException("issues"); } var expectations = scenario.Expectations; if (null == expectations || expectations.Count == 0) { return; } foreach (string key in expectations.Keys) { string keyIndex; var type = BasicRequestDefinition.LocationForKey(key, out keyIndex); object expectedValues = expectations[key]; switch (type) { case PlaceholderLocation.Body: ExpectationSatisfied(key, actualResponse.Body, expectedValues, issues); break; case PlaceholderLocation.HttpHeader: ExpectationSatisfied(key, actualResponse.Headers[keyIndex], expectedValues, issues); break; case PlaceholderLocation.Json: try { object value = JsonPath.ValueFromJsonPath(actualResponse.Body, keyIndex); ExpectationSatisfied(key, value, expectedValues, issues); } catch (Exception ex) { issues.Error(ValidationErrorCode.JsonParserException, string.Empty, ex); } break; case PlaceholderLocation.Invalid: case PlaceholderLocation.StoredValue: case PlaceholderLocation.Url: issues.Warning(ValidationErrorCode.InvalidExpectationKey, $"The expectation key {key} is invalid. Supported types are Body, HttpHeader, and JsonPath."); break; } } }
/// <summary> /// Verify that the expectations in the scenario are met by the response /// </summary> /// <param name="scenario"></param> /// <param name="actualResponse"></param> /// <param name="detectedErrors"></param> public static void ValidateExpectations(this ScenarioDefinition scenario, HttpResponse actualResponse, List <ValidationError> detectedErrors) { if (scenario == null) { throw new ArgumentNullException("scenario"); } if (actualResponse == null) { throw new ArgumentNullException("actualResponse"); } if (detectedErrors == null) { throw new ArgumentNullException("detectedErrors"); } var expectations = scenario.Expectations; if (null == expectations || expectations.Count == 0) { return; } foreach (string key in expectations.Keys) { string keyIndex; var type = BasicRequestDefinition.LocationForKey(key, out keyIndex); object expectedValues = expectations[key]; switch (type) { case PlaceholderLocation.Body: ExpectationSatisfied(key, actualResponse.Body, expectedValues, detectedErrors); break; case PlaceholderLocation.HttpHeader: ExpectationSatisfied(key, actualResponse.Headers[keyIndex].FirstOrDefault(), expectedValues, detectedErrors); break; case PlaceholderLocation.Json: try { object value = JsonPath.ValueFromJsonPath(actualResponse.Body, keyIndex); ExpectationSatisfied(key, value, expectedValues, detectedErrors); } catch (Exception ex) { detectedErrors.Add(new ValidationError(ValidationErrorCode.JsonParserException, null, ex.Message)); } break; case PlaceholderLocation.Invalid: case PlaceholderLocation.StoredValue: case PlaceholderLocation.Url: detectedErrors.Add(new ValidationWarning(ValidationErrorCode.InvalidExpectationKey, null, "The expectation key {0} is invalid. Supported types are Body, HttpHeader, and JsonPath.", key)); break; } } }