asp.net mvc - Correct way to retrieve data from a webapi2.1 header -


I have created a base API controller that allows me to access data from a header.

  Public Virtual Ink GetHeaderData () {if (request! = Null) {var header = request Header; Int Custom Value; If (header "mycustomvalue") {int.TryParse (headers.GetValues ​​("mycustomvalue"). First (), custom value out); Return Custom Value; }} New exception throw (); }   

Therefore calling is very easy with any controller eg. GetHeaderData ()

However, as a subsidiary, I find this unit very difficult to test

request. Headers

Am I really trying to read the header value at the controller level or should I do somewhere else that would be able to do this easy test?

I'm the best way I should be

I would not go away so far to call it a advice, but to consider you just one suggestion First, the unit test should not determine your class design. It is possible to set the request object at the admin level to return the Request.Headers headers to the unit test correctly. However, if I ask myself that this method is related to the controller, then the answer is no, regardless of the purpose of the unit test or not, if I was doing this, then instead of HttpRequestMessage Extension method is just syntactic, but it is fundamentally a stable helper method and I can use the extension method anywhere - controller, message handler, font And so on. And then, if I want, I can do the unit test.

Comments

Popular posts from this blog

c - Mpirun hangs when mpi send and recieve is put in a loop -

python - Apply coupon to a customer's subscription based on non-stripe related actions on the site -

java - Unable to get JDBC connection in Spring application to MySQL -