I am working on a project using WebApi2. In my test project, I use Moq and XUnit.
So far, api testing has been pretty straightforward to make GET like
[Fact()] public void GetCustomer() { var id = 2; _customerMock.Setup(c => c.FindSingle(id)) .Returns(FakeCustomers() .Single(cust => cust.Id == id)); var result = new CustomersController(_customerMock.Object).Get(id); var negotiatedResult = result as OkContentActionResult<Customer>; Assert.NotNull(negotiatedResult); Assert.IsType<OkNegotiatedContentResult<Customer>>(negotiatedResult); Assert.Equal(negotiatedResult.Content.Id,id); }
Now I get to something complicated when I need to access the value from the request header.
I created my own Ok () result by extending IHttpActionResult
public OkContentActionResult(T content,HttpRequestMessage request) { _request = request; _content = content; }
This allows me to have a little helper that reads the value of the header from the request.
public virtual IHttpActionResult Post(Customer customer) { var header = RequestHeader.GetHeaderValue("customerId", this.Request); if (header != "1234")
How am I going to configure Moq using a dummy query?
I spent the last hour or so hunting for an example that allows me to do this using webapi, but I can't find anything.
So far ..... and I'm sure this is wrong for the api, but I have
// arrange var context = new Mock<HttpContextBase>(); var request = new Mock<HttpRequestBase>(); var headers = new NameValueCollection { { "customerId", "111111" } }; request.Setup(x => x.Headers).Returns(headers); request.Setup(x => x.HttpMethod).Returns("GET"); request.Setup(x => x.Url).Returns(new Uri("http://foo.com")); request.Setup(x => x.RawUrl).Returns("/foo"); context.Setup(x => x.Request).Returns(request.Object); var controller = new Mock<ControllerBase>(); _customerController = new CustomerController() { // Request = request, };
I'm not quite sure what I need to do, since I did not need to customize the HttpRequestBase layout in the past.
Can someone suggest a good article or point me in the right direction?
Thank!!!