/// <summary> /// Verify the code rule /// </summary> /// <param name="context">Service context</param> /// <param name="info">out paramater to return violation information when rule fail</param> /// <returns>true if rule passes; false otherwise</returns> public override bool?Verify(ServiceContext context, out ExtensionRuleViolationInfo info) { if (context == null) { throw new ArgumentNullException("context"); } bool?passed = null; info = null; var path = context.DestinationBasePath.Substring(context.ServiceBaseUri.AbsoluteUri.Length); var segments = ResourcePathHelper.GetPathSegments(path).ToArray(); if (segments.Length > 0) { string lastSeg = segments[segments.Length - 1]; if (lastSeg.Equals("$value", StringComparison.Ordinal)) { var ct = context.ResponseHttpHeaders.GetContentTypeValue(); HttpHeaderRegexVerifier verifier = new HttpHeaderRegexVerifier("Content-Type", "\\s*text/plain\\b.*"); TestResult tr; passed = verifier.Verify(context, out tr); if (passed.HasValue && !passed.Value) { info = new ExtensionRuleViolationInfo("unexpected Content-Type header value", context.Destination, ct); } } } return(passed); }
/// <summary> /// Verify the code rule /// </summary> /// <param name="context">Service context</param> /// <param name="info">out paramater to return violation information when rule fail</param> /// <returns>true if rule passes; false otherwise</returns> public override bool?Verify(ServiceContext context, out ExtensionRuleViolationInfo info) { if (context == null) { throw new ArgumentNullException("context"); } bool?passed = null; info = null; var edmxHelper = new EdmxHelper(XElement.Parse(context.MetadataDocument)); var segments = ResourcePathHelper.GetPathSegments(context).ToArray(); UriType uriType; var target = edmxHelper.GetTargetType(segments, out uriType); if (segments.Length > 0) { string lastSeg = segments[segments.Length - 1]; if (lastSeg.Equals("$value", StringComparison.Ordinal) && uriType != UriType.URI17) { string targetType = ((EdmProperty)target).TypeUsage.EdmType.FullName; // to ignore the case of Edm.Binary if (!targetType.Equals("Edm.Binary", StringComparison.Ordinal)) { var ct = context.ResponseHttpHeaders.GetContentTypeValue(); HttpHeaderRegexVerifier verifier = new HttpHeaderRegexVerifier("Content-Type", "\\s*text/plain\\b.*"); TestResult tr; passed = verifier.Verify(context, out tr); if (passed.HasValue && !passed.Value) { info = new ExtensionRuleViolationInfo("unexpected Content-Type header value", context.Destination, ct); } } } } return(passed); }