Skip to content

Instantly share code, notes, and snippets.

@beyond-code-github
Last active December 14, 2015 15:18
Show Gist options
  • Save beyond-code-github/5106495 to your computer and use it in GitHub Desktop.
Save beyond-code-github/5106495 to your computer and use it in GitHub Desktop.
We have two different kinds of route at present, /Api/{AppId}/{Controller}/{Id} and /Api/{Controller}/{Id}. The Linked route provider ensures that if we PATCH or PUT to either of these that we also invalidate the cache for the same route without the {Id} parameter
config.MessageHandlers.Add(new CachingHandler
{
UriTrimmer = (uri) => uri.LocalPath,
LinkedRoutePatternProvider = LinkedRoutePatternProvider
});
public static IEnumerable<string> LinkedRoutePatternProvider(string s, HttpMethod httpMethod)
{
var appAware = new Regex("/Api/[0-9]+/([a-z]|[A-Z])+/[0-9]+");
if (appAware.IsMatch(s))
{
var sections = s.Split('/');
var appId = sections[2];
var controller = sections[3];
return new[] { string.Format("/Api/{0}/{1}", appId, controller) };
}
var normal = new Regex("/Api/([a-z]|[A-Z])+/[0-9]+");
if (normal.IsMatch(s))
{
var sections = s.Split('/');
var controller = sections[2];
return new[] { string.Format("/Api/{0}", controller) };
}
return new string[] { };
}
[Subject("Linked route pattern provider")]
public class When_getting_linked_routes_for_a_context_specific_resource
{
private static IEnumerable<string> result;
private Because of = () => result = WebApiConfig.LinkedRoutePatternProvider("/Api/1/Forms/2", null);
private It should_link_to_the_parent_collection = () => result.ShouldContain("/Api/1/Forms");
}
[Subject("Linked route pattern provider")]
public class When_getting_linked_routes_for_a_non_specific_resource
{
private static IEnumerable<string> result;
private Because of = () => result = WebApiConfig.LinkedRoutePatternProvider("/Api/Users/1", null);
private It should_link_to_the_parent_collection = () => result.ShouldContain("/Api/Users");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment