0

Please ignore the spelling mistake, I cannot copy code so I have typed the whole thing and changed name of controller and method.

WEB API 2

Controller:

// Controller name is Test
public HttpResponseMessage Method1(int param1)   // Post method
{
    // return string
}

If I create an object of controller in test case then it is working fine. But if I want to test in localhost using following code:

Unit Test:

public void Method1Test()
{
    HttpResponseMessage response;
    HttpConfiguration config = new HttpConfiguration();
    config.Routes.MapHttpRoute("DefaultApi", "api/{controller}/{id}");
    HttpServer server = new HttpServer(config);
    using(var client = new HttpClient(server))
    {
        HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "http://localhost:5022/api/test?param1=1");
        request.Content = new ObjectContent<int>(param1, new JsonMediaTypeFormatter());
        response = client.SendAsync(request, CancellationToken.None).Result;
    };
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
}

Now, my test case is failing. I used the same code in different project and it worked. May be it is the way I am trying to call Post method. Is this the right way to call post method with Int parameter in URL?

In help page, under API column it shows:

POST api/test/param1={param1}

Also I have put some stop point in actual service I am cursor is not stopping at that point. Why?

If I want to call the same service from browser, what URL should I pass? Is it -

http://localhost:5022/api/test?param1=1

Or something else?

2
  • what error are you getting when you are saying test case is failing? Commented Feb 4, 2015 at 19:47
  • In Test Case: Message: Assert.AreEqual failed. Expected:<OK>. Acutal:<NotFound>. And when I pass the link in Browser: The requested resource does not support http method 'GET' Commented Feb 9, 2015 at 15:26

1 Answer 1

1

I figured it out. Following is the correct unit test method but this has some extra information which I have not provided earlier i.e., passing object as an input for the service.

private void Method1Test(ObjectClass obj)
{
    HttpResponseMessage response = null;
    HttpConfiguration config = new HttpConfiguration();
    config.Routes.MapHttpRoute("DefaultApi", "api/{controller}/{id}");
    HttpServer server = new HttpServer(config);
    using (var client = new HttpClient(server))
    {
        HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "http://localhost:5022/api/test/1");
        request.Content = new ObjectContent<ObjectClass>(obj, new JsonMediaTypeFormatter());
        response = client.SendAsync(request, CancellationToken.None).Result;
    };
    Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
}

So the correct URL that I was looking for was

http://localhost:5022/api/test/1

Sorry, It took long to post this answer. This method is working like a charm for more then 2 years.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.