2

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?

2 Answers 2

2

I'd suggest not to use FakeDb for (unit) testing such scenario. If I unrerstand your intention correctly, you do not test any logic of your application. You're trying to test a piece of Sitecore functionality which is a perfect place for an integration test (e.g. using Postman). In this particular scenario it could be more robust and unlike unit test it's able to verify the functionality in case of configuration changes.

0

As a workaround you can try to create URI value with non escaped characters. This method is obsolete but you can still use it https://msdn.microsoft.com/en-us/library/9zh9wcb3.aspx . Then after creating HttpRequest you'll need to set private field _url with unescaped version. Not sure if that will work but worth a try.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.