asp.net mvc 3 - How do I inject something into request header for testing? -


i implementing siteminder site, looks key called sm_user in request header. retrieve using function below:

 public string readuser()  {     return httpcontext.current.request.headers["sm_user"];  } 

i wish test if functionality releated function work; have tried unit testing using mock class looking create key sm_user in request header. how can that?

i implementing application mvc3.

as long using httpcontext.current not able test unit test not have httpcontext.current.

try use interface method returning string, readuser(). implement interface in class in application. use interface variable whichever class using method in. in class' default constructor set interface variable value 'new' implementer class. add overload of constructor take parameter of type interface , set parameter interface variable.

now in unittest project implement same interface in class. in implementation can pass whatever mock value want test.

public interface ireaduserinfo {  string readuser();     }  public class readuserinfo: ireaduserinfo {     public string readuser()     {        return httpcontext.current.request.headers["sm_user"];     } }   public class userclass {     ireaduserinfo userinfo;      public userclass()     {         userinfo = new readuserinfo();     }      public userclass(ireaduserinfo newuserinfo)     {         userinfo = newuserinfo;     } }  public class testreaduserinfo : ireaduserinfo {      public string readuser()      { return "testvalue";  } } 

if readuser value using request header, approach solve problem. however, if using more values request object, might want mock entire request object in similar way.


Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -