Skip to content
This repository has been archived by the owner on Jan 15, 2021. It is now read-only.

Commit

Permalink
Fix #740 - Add ability to specify RuntimePolicy
Browse files Browse the repository at this point in the history
When specifying a custom contentType
  • Loading branch information
avanderhoorn committed Feb 15, 2014
1 parent c64c52a commit c00fd41
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 13 deletions.
14 changes: 14 additions & 0 deletions source/Glimpse.Core/Configuration/ContentTypeElement.cs
@@ -1,4 +1,5 @@
using System.Configuration;
using Glimpse.Core.Extensibility;

namespace Glimpse.Core.Configuration
{
Expand All @@ -21,5 +22,18 @@ public string ContentType
get { return (string)base["contentType"]; }
set { base["contentType"] = value; }
}

/// <summary>
/// Gets or sets the which <see cref="RuntimePolicy"/> should be used for this content type.
/// </summary>
/// <value>
/// The enum representation of any valid <see cref="RuntimePolicy"/>.
/// </value>
[ConfigurationProperty("runtimePolicy", DefaultValue = RuntimePolicy.On)]
public RuntimePolicy RuntimePolicy
{
get { return (RuntimePolicy)base["runtimePolicy"]; }
set { base["runtimePolicy"] = value; }
}
}
}
8 changes: 6 additions & 2 deletions source/Glimpse.Core/EmbeddedResources/glimpse_config.html
Expand Up @@ -83,7 +83,9 @@ <h2>Bookmarklets
&lt;!-- Runtime Policy Management
&lt;runtimePolicies&gt;
&lt;ignoredTypes&gt;
&lt;add type="{Namespace.Type, AssemblyName}"/&gt;
&lt;add type="{Namespace.Type, AssemblyName}"/&gt;
&lt;add contentType="image/png" runtimePolicy="ModifyResponseHeaders" /&gt;
&lt;add contentType="application/msword" runtimePolicy="PersistResults" /&gt;
&lt;/ignoredTypes&gt;
&lt;/runtimePolicies&gt;
--></pre>
Expand All @@ -107,7 +109,9 @@ <h2>Bookmarklets
</div>
<div class="code-runtimePolicies-contentTypes configuration-options-item">
<pre> &lt;contentTypes&gt;
&lt;add contentType="application/xml"/&gt;
&lt;add contentType="application/xml"/&gt;
&lt;add contentType="image/png" runtimePolicy="ModifyResponseHeaders" /&gt;
&lt;add contentType="application/msword" runtimePolicy="PersistResults" /&gt;
&lt;/contentTypes&gt;</pre>
</div>
<pre> &lt;/runtimePolicies&gt;</pre>
Expand Down
15 changes: 9 additions & 6 deletions source/Glimpse.Core/Policy/ContentTypePolicy.cs
Expand Up @@ -14,7 +14,8 @@ public class ContentTypePolicy : IRuntimePolicy, IConfigurable
/// <summary>
/// Initializes a new instance of the <see cref="ContentTypePolicy" /> class with an empty white list.
/// </summary>
public ContentTypePolicy() : this(new List<string>())
public ContentTypePolicy()
: this(new List<Tuple<string, RuntimePolicy>>())
{
}

Expand All @@ -23,7 +24,7 @@ public ContentTypePolicy() : this(new List<string>())
/// </summary>
/// <param name="contentTypeWhiteList">The content type white list to validate against.</param>
/// <exception cref="System.ArgumentNullException">Exception thrown if <paramref name="contentTypeWhiteList"/> is <c>null</c>.</exception>
public ContentTypePolicy(IList<string> contentTypeWhiteList)
public ContentTypePolicy(IList<Tuple<string, RuntimePolicy>> contentTypeWhiteList)
{
if (contentTypeWhiteList == null)
{
Expand All @@ -39,7 +40,7 @@ public ContentTypePolicy(IList<string> contentTypeWhiteList)
/// <value>
/// The content type white list to validate against.
/// </value>
public IList<string> ContentTypeWhiteList { get; set; }
public IList<Tuple<string, RuntimePolicy>> ContentTypeWhiteList { get; set; }

/// <summary>
/// Gets the point in an Http request lifecycle that a policy should execute.
Expand Down Expand Up @@ -67,7 +68,9 @@ public RuntimePolicy Execute(IRuntimePolicyContext policyContext)
var contentType = policyContext.RequestMetadata.ResponseContentType.ToLowerInvariant();

// support for the following content type strings: "text/html" & "text/html; charset=utf-8"
return ContentTypeWhiteList.Any(ct => contentType.Contains(ct.ToLowerInvariant())) ? RuntimePolicy.On : RuntimePolicy.Off;
var match = ContentTypeWhiteList.FirstOrDefault(ct => contentType.Contains(ct.Item1.ToLowerInvariant()));

return match != null ? match.Item2 : RuntimePolicy.Off;
}
catch (Exception exception)
{
Expand All @@ -92,7 +95,7 @@ public RuntimePolicy Execute(IRuntimePolicyContext policyContext)
/// <runtimePolicies>
/// <contentTypes>
/// <!-- <clear /> clear to reset defaults -->
/// <add contentType="{media\type}" />
/// <add contentType="{media\type}" runtimePolicy="on" />
/// </contentTypes>
/// </runtimePolicies>
/// </glimpse>
Expand All @@ -103,7 +106,7 @@ public void Configure(Section section)
{
foreach (ContentTypeElement item in section.RuntimePolicies.ContentTypes)
{
ContentTypeWhiteList.Add(item.ContentType);
ContentTypeWhiteList.Add(new Tuple<string, RuntimePolicy>(item.ContentType, item.RuntimePolicy));
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions source/Glimpse.Mvc3.MusicStore.Sample/web.config
Expand Up @@ -14,6 +14,9 @@
<ignoredTypes>
<add type="Glimpse.Core.Policy.ControlCookiePolicy, Glimpse.Core"/>
</ignoredTypes>
<contentTypes>
<add contentType="image/png" runtimePolicy="ModifyResponseHeaders" />
</contentTypes>
</runtimePolicies>
</glimpse>
<appSettings>
Expand Down
10 changes: 9 additions & 1 deletion source/Glimpse.Test.Core/Policy/ContentTypePolicyShould.cs
Expand Up @@ -46,6 +46,14 @@ public void ReducePolicyOnInvalidContentTypes()
Assert.Equal(RuntimePolicy.Off, Policy.Execute(Policy.ContextMock.Object));
}

[Fact]
public void ReducePolicyWithCustomRuntimPolicy()
{
Policy.RequestMetadataMock.Setup(r => r.ResponseContentType).Returns("application/json");

Assert.Equal(RuntimePolicy.PersistResults, Policy.Execute(Policy.ContextMock.Object));
}

[Fact]
public void ReducePolicyOnError()
{
Expand All @@ -71,7 +79,7 @@ public void ConstructWithDefaultContentTypes()
[Fact]
public void ConstructWithWhitelistArgument()
{
var list = new List<string>{"anything"};
var list = new List<Tuple<string, RuntimePolicy>>{new Tuple<string, RuntimePolicy>("anything", RuntimePolicy.PersistResults)};
var policy = new ContentTypePolicy(list);

Assert.Equal(list, policy.ContentTypeWhiteList);
Expand Down
10 changes: 6 additions & 4 deletions source/Glimpse.Test.Core/Tester/ContentTypePolicyTester.cs
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using Glimpse.Core.Extensibility;
using Glimpse.Core.Framework;
Expand All @@ -12,7 +13,8 @@ public class ContentTypePolicyTester:ContentTypePolicy
public Mock<IRequestMetadata> RequestMetadataMock { get; set; }
public Mock<ILogger> LoggerMock { get; set; }

private ContentTypePolicyTester(IList<string> contentTypes):base(contentTypes)
private ContentTypePolicyTester(IList<Tuple<string, RuntimePolicy>> contentTypes)
: base(contentTypes)
{
RequestMetadataMock = new Mock<IRequestMetadata>();
RequestMetadataMock.Setup(r => r.ResponseContentType).Returns(@"text/html");
Expand All @@ -26,10 +28,10 @@ private ContentTypePolicyTester(IList<string> contentTypes):base(contentTypes)

public static ContentTypePolicyTester Create()
{
return new ContentTypePolicyTester(new List<string>
return new ContentTypePolicyTester(new List<Tuple<string, RuntimePolicy>>
{
@"text/html",
@"application/json"
new Tuple<string, RuntimePolicy>(@"text/html", RuntimePolicy.On),
new Tuple<string, RuntimePolicy>(@"application/json", RuntimePolicy.PersistResults)
});
}
}
Expand Down

0 comments on commit c00fd41

Please sign in to comment.