I'm putting together unit tests for item resolver running under httpRequest pipeline.
One of the test is checking if the item is properly resolved using display name with asian characters (Japanese).
The test looks like this:
[Test]
public void GetItem_UrlItem_DisplayName_Asian_ShouldSetContextItem()
{
using (var db = new Db {
new DbItem("Home") {
new DbField("Title")
{
{ "en", 1, "Welcome!" }
}, new DbField(Sitecore.FieldIDs.DisplayName)
{
{ "en", 1, "ホーム" }
}
}
})
{
Sitecore.Context.Item = null;
var request = CreateHttpRequestArgs("http://mywebsite/sitecore/content/ホーム");
var homeItem = db.GetItem("/sitecore/content/Home");
_itemResolver.Process(request);
Sitecore.Context.Item.Should().Be(homeItem);
}
}
For setting up HttpRequestArgs
I'm using method described by jwsadler in his blog post: http://blog.jwsadler.guru/2016/01/sitecore-fakedb-and-real-world-testing.html
private HttpRequestArgs CreateHttpRequestArgs(string url)
{
_httpResponseBuilder = new StringBuilder();
var response = new HttpResponse(new StringWriter(_httpResponseBuilder));
var httpRequest = new System.Web.HttpRequest("", url, "");
var httpContext = new HttpContext(httpRequest, response);
var request =
new HttpRequestArgs(
httpContext, HttpRequestType.End);
var dynMethod = request.GetType().GetMethod("Initialize", BindingFlags.NonPublic | BindingFlags.Instance);
dynMethod.Invoke(request, null);
return request;
}
The problem I'm facing is that when the HttpRequest is created with URL: http://mywebsite/sitecore/content/ホーム
, ConfigurationPath
on HttpContext, path looks like this: /sitecore/content/%E3%83%9B%E3%83%BC%E3%83%A0
and when it comes to comparing the display names, it's obviously not matching ホーム.
However, when I run the code on live instance and access the same URL, debug and inspect the HttpContext, ConfigurationPath
is not encoded (or is already decoded?), looks like this: /sitecore/content/ホーム
and code works fine.
How to workaround the encoding on the URL while creating HTTP context for unit testing?