public void Validate( HttpResponseMessage response, OpenApiDocument openApiDocument, string pathTemplate, OperationType operationType, string expectedStatusCode) { var operationSpec = openApiDocument.GetOperationByPathAndType(pathTemplate, operationType, out OpenApiPathItem pathSpec); if (!operationSpec.Responses.TryGetValue(expectedStatusCode, out OpenApiResponse responseSpec)) { throw new InvalidOperationException($"Response for status '{expectedStatusCode}' not found for operation '{operationSpec.OperationId}'"); } var statusCode = (int)response.StatusCode; if (statusCode.ToString() != expectedStatusCode) { throw new ResponseDoesNotMatchSpecException($"Status code '{statusCode}' does not match expected value '{expectedStatusCode}'"); } ValidateHeaders(responseSpec.Headers, openApiDocument, response.Headers.ToNameValueCollection()); if (responseSpec.Content != null && responseSpec.Content.Keys.Any()) { ValidateContent(responseSpec.Content, openApiDocument, response.Content); } }
public void Validate( HttpRequestMessage request, OpenApiDocument openApiDocument, string pathTemplate, OperationType operationType) { var operationSpec = openApiDocument.GetOperationByPathAndType(pathTemplate, operationType, out OpenApiPathItem pathSpec); var parameterSpecs = ExpandParameterSpecs(pathSpec, operationSpec, openApiDocument); // Convert to absolute Uri as a workaround to limitation with Uri class - i.e. most of it's methods are not supported for relative Uri's. var requestUri = new Uri(new Uri("http://tempuri.org"), request.RequestUri); if (!TryParsePathNameValues(pathTemplate, requestUri.AbsolutePath, out NameValueCollection pathNameValues)) { throw new RequestDoesNotMatchSpecException($"Request URI '{requestUri.AbsolutePath}' does not match specified template '{pathTemplate}'"); } if (request.Method != new HttpMethod(operationType.ToString())) { throw new RequestDoesNotMatchSpecException($"Request method '{request.Method}' does not match specified operation type '{operationType}'"); } ValidateParameters(parameterSpecs.Where(p => p.In == ParameterLocation.Path), openApiDocument, pathNameValues); ValidateParameters(parameterSpecs.Where(p => p.In == ParameterLocation.Query), openApiDocument, HttpUtility.ParseQueryString(requestUri.Query)); ValidateParameters(parameterSpecs.Where(p => p.In == ParameterLocation.Header), openApiDocument, request.Headers.ToNameValueCollection()); if (operationSpec.RequestBody != null) { ValidateContent(operationSpec.RequestBody, openApiDocument, request.Content); } }