I am currently trying to mock a media request and the media item I am requesting using FakeDb but for some reason my request is returning an empty MediaPath rather than the MediaPath I am expecting.
Here is the method:
public class RequestMediaEXT
{
public bool getImage(HttpContext context)
{
MediaRequest request = MediaManager.ParseMediaRequest(context.Request);
string mediaPath = request?.MediaUri.MediaPath;
if (mediaPath == null | MediaPath == string.empty)
{
return false;
}
return true;
}
}
Unit test for the method:
[Theory]
[CustomAutoData("http://image.here/", "~/media/myimage.ashx")]
public void getImage_imageFound_ReturnsTrue(string url, string media)
{
string myImageUrl = url + media;
Sitecore.Data.ID mediaItemId = Sitecore.Data.ID.NewID;
HttpRequest httpRequest = new HttpRequest(string.Empty, myImageUrl, string.Empty);
HttpResponse httpResponse = new HttpResponse(new StringWriter());
HttpContext httpContext = new HttpContext(httpRequest, httpResponse);
var processor = new RequestMediaEXT()
// create some media item. Location, fields and template are not important
using (Db db = new Db())
{
var mediaFakeItem = new DbItem("myimage")
{
ParentID = Sitecore.ItemIDs.MediaLibraryRoot
};
mediaFakeItem.FullPath = myImageUrl;
db.Add(mediaFakeItem);
// create media provider mock and configure behaviour
MediaProvider mediaProvider =
Substitute.For<MediaProvider>();
mediaProvider
.GetMediaUrl(Arg.Is<Sitecore.Data.Items.MediaItem>(i => i.ID == mediaItemId))
.Returns(myImageUrl);
// substitute the original provider with the mocked one
using (new Sitecore.FakeDb.Resources.Media.MediaProviderSwitcher(mediaProvider))
{
//Act
processor.getImage(httpContext);
//Assert
Assert.False(!processor.getImage(httpContext));
}
}
}
How do I mock the HttpContext such that the MediaPath doesn't become an empty string but the actual MediaPath?
I am using Sitecore 8.1 and therefore have to use the now obsolete MediaProviderSwitcher.