Advertisement
Guest User

MockHost.cs

a guest
May 10th, 2016
1,054
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.45 KB | None | 0 0
  1. using System;
  2. using System.Globalization;
  3. using System.IO;
  4. using System.Reflection;
  5. using System.Runtime.Remoting.Messaging;
  6. using System.Web;
  7. using System.Web.Hosting;
  8.  
  9. namespace Standardikeskus.Tests
  10. {
  11.     public class MockHost
  12.     {
  13.         private string pdir;
  14.         private string vdir;
  15.         private string page = "/";
  16.  
  17.         public MockHost(string pdir, string vdir)
  18.         {
  19.             this.pdir = pdir;
  20.             this.vdir = vdir;
  21.         }
  22.  
  23.         private static HttpRuntime GetTheRuntimeInstance()
  24.         {
  25.             Type HttpRuntimeType = typeof(HttpRuntime);
  26.             BindingFlags flags = BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.Static;
  27.             return HttpRuntimeType.InvokeMember(
  28.                         "_theRuntime",
  29.                         flags,
  30.                         null,
  31.                         null,
  32.                         null) as HttpRuntime;
  33.         }
  34.  
  35.         private static void SetHttpRuntimePath(HttpRuntime instance, string pdir, string vdir)
  36.         {
  37.             Type httpRuntimeType = typeof(HttpRuntime);
  38.  
  39.             BindingFlags flags = BindingFlags.NonPublic | BindingFlags.SetField | BindingFlags.Instance;
  40.  
  41.             httpRuntimeType.InvokeMember(
  42.                     "_appDomainAppPath",
  43.                     flags,
  44.                     null,
  45.                     instance,
  46.                     new object[] { pdir }
  47.                 );
  48.  
  49.             HostingEnvironment en = new HostingEnvironment();
  50.             FieldInfo _appPhysicalPath = typeof(HostingEnvironment).GetField("_appPhysicalPath", BindingFlags.Instance | BindingFlags.NonPublic);
  51.             _appPhysicalPath.SetValue(en, pdir);
  52.  
  53.             FieldInfo _appVirtualPath = typeof(HostingEnvironment).GetField("_appVirtualPath", BindingFlags.Instance | BindingFlags.NonPublic);
  54.  
  55.             Type vtype = httpRuntimeType.Assembly.GetType("System.Web.VirtualPath");
  56.             object virtualPath = vtype.InvokeMember("Create", BindingFlags.InvokeMethod, null, null, new object[] { vdir });
  57.             _appVirtualPath.SetValue(en, virtualPath);
  58.  
  59.             FieldInfo fi = typeof(HostingEnvironment).GetField("_theHostingEnvironment", BindingFlags.NonPublic | BindingFlags.Static);
  60.             fi.SetValue(null, en);
  61.  
  62.             httpRuntimeType.InvokeMember(
  63.                     "_appDomainAppVPath",
  64.                     flags,
  65.                     null,
  66.                     instance,
  67.                     new object[] { virtualPath }
  68.                 );
  69.  
  70.         }
  71.  
  72.         public void Setup()
  73.         {
  74.  
  75.             HttpRuntime instance = GetTheRuntimeInstance();
  76.  
  77.             //set call context for base folder
  78.             SetCallContext(pdir, vdir);
  79.  
  80.             SetHttpRuntimePath(instance, pdir, vdir);
  81.  
  82.             SimpleWorkerRequest request = new SimpleWorkerRequest(vdir, pdir, page, null, new StringWriter());
  83.             FieldInfo fInfo = request.GetType().GetField("_hasRuntimeInfo", BindingFlags.Instance | BindingFlags.NonPublic);
  84.             fInfo.SetValue(request, true);
  85.             HttpContext.Current = new HttpContext(request);
  86.  
  87.         }
  88.  
  89.         private static void SetCallContext(string pdir, string relativedir)
  90.         {
  91.             string rdir = relativedir;
  92.  
  93.             // remove the starting slash
  94.             if (rdir[0] == '/')
  95.             {
  96.                 rdir = rdir.Substring(1);
  97.             }
  98.  
  99.             // change the virtual dir structure.
  100.             rdir = rdir.Replace('/', '\\');
  101.  
  102.             string fdir = Path.Combine(pdir, rdir);
  103.             DirectoryInfo fdirInfo = new DirectoryInfo(fdir);
  104.  
  105.             foreach (DirectoryInfo dir in fdirInfo.GetDirectories())
  106.             {
  107.                 string sName = string.Format("{0}{1}", relativedir, dir.Name);
  108.  
  109.                 CallContext.SetData(GetFixedMappingSlotName(sName), dir.FullName);
  110.                 SetCallContext(pdir, string.Format("{0}{1}/", relativedir, dir));
  111.             }
  112.  
  113.             foreach (var fileInfo in fdirInfo.GetFiles())
  114.             {
  115.                 string sName = string.Format("{0}{1}", relativedir, fileInfo.Name);
  116.  
  117.                 CallContext.SetData(GetFixedMappingSlotName(sName), fileInfo.FullName);
  118.             }
  119.         }
  120.  
  121.         private static string GetFixedMappingSlotName(string virtualPathString)
  122.         {
  123.             return ("MapPath_" + virtualPathString.ToLowerInvariant().GetHashCode().ToString(CultureInfo.InvariantCulture));
  124.         }
  125.  
  126.     }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement