asp.net web api 服务原生调用

[TestClass]
 
publicclassSampleTests
 
{
   
privateHttpSelfHostServer server =null;
   
privatestring baseAddress =null;

    
//Use TestInitialize to run code before running each test
   
[TestInitialize()]
   
publicvoidMyTestInitialize()
   
{
      baseAddress
=string.Format("http://{0}:9090",Environment.MachineName);

     
HttpSelfHostConfiguration config =newHttpSelfHostConfiguration(this.baseAddress);
      config
.Routes.MapHttpRoute("API Default","api/{controller}/{id}",new{ id =RouteParameter.Optional});

      server
=newHttpSelfHostServer(config);
      server
.OpenAsync().Wait();
   
}
    
    
//Use TestCleanup to run code after each test has run
   
[TestCleanup()]
   
publicvoidMyTestCleanup()
   
{
     
if(server !=null)
     
{
          server
.CloseAsync().Wait();
     
}
   
}

   
[TestMethod]
   
publicvoidGetTest()
   
{
     
string expectedResponse ="{'ID':1,'Name':'Saved Item'}".Replace("'","\"");

     
HttpClient httpClient =newHttpClient();

     
HttpRequestMessage request =newHttpRequestMessage();
      request
.RequestUri=newUri(this.baseAddress +"/api/SampleItems/");
      request
.Headers.Accept.Add(newMediaTypeWithQualityHeaderValue("application/json"));
      request
.Method=HttpMethod.Get;

     
HttpResponseMessage response = httpClient.SendAsync(request).Result;

     
Assert.IsNotNull(response.Content);
     
Assert.IsNotNull(response.Content.Headers.ContentType);
     
Assert.AreEqual<string>("application/json", response.Content.Headers.ContentType.MediaType);
     
Assert.AreEqual<string>(expectedResponse, response.Content.ReadAsStringAsync().Result);
   
}

   
[TestMethod]
   
publicvoidPostAsString()
   
{
     
string expectedResponse ="{'ID':2,'Name':'New Item'}".Replace("'","\"");

     
HttpClient httpClient =newHttpClient();

     
string msgContent ="{'Name':'New Item'}".Replace("'","\"");

     
HttpRequestMessage<string> request =newHttpRequestMessage<string>(msgContent,newMediaTypeHeaderValue("application/json"));
      request
.RequestUri=newUri(this.baseAddress +"/api/SampleItems/");
      request
.Headers.Accept.Add(newMediaTypeWithQualityHeaderValue("application/json"));
      request
.Method=HttpMethod.Post;

     
HttpResponseMessage response = httpClient.SendAsync(request).Result;

     
Assert.IsNotNull(response.Content);
     
Assert.IsNotNull(response.Content.Headers.ContentType);
     
Assert.AreEqual<string>("application/json", response.Content.Headers.ContentType.MediaType);
     
Assert.AreEqual<string>(expectedResponse, response.Content.ReadAsStringAsync().Result);
   
}

   
[TestMethod]
   
publicvoidPostAsClass()
   
{
     
string expectedResponse ="{'ID':2,'Name':'New Item'}".Replace("'","\"");

     
HttpClient httpClient =newHttpClient();

     
SampleItem msgContent =newSampleItem{Name="New Item"};

     
HttpRequestMessage<SampleItem> request =newHttpRequestMessage<SampleItem>(msgContent,newMediaTypeHeaderValue("application/json"));
      request
.RequestUri=newUri(this.baseAddress +"/api/SampleItems/");
      request
.Headers.Accept.Add(newMediaTypeWithQualityHeaderValue("application/json"));
      request
.Method=HttpMethod.Post;

     
HttpResponseMessage response = httpClient.SendAsync(request).Result;

     
Assert.IsNotNull(response.Content);
     
Assert.IsNotNull(response.Content.Headers.ContentType);
     
Assert.AreEqual<string>("application/json", response.Content.Headers.ContentType.MediaType);
     
Assert.AreEqual<string>(expectedResponse, response.Content.ReadAsStringAsync().Result);
   
}


 
}

 
publicclassSampleItemsController:ApiController
 
{
   
publicSampleItemPost(SampleItem newItem)
   
{
      newItem
.ID =2;
     
return newItem;
   
}

   
publicSampleItemGet()
   
{
     
returnnewSampleItem
              
{
                 ID
=1,
                
Name="Saved Item"
              
};
   
}
 
}

 
publicclassSampleItem
 
{
   
publicint ID {get;set;}
   
publicstringName{get;set;}
 
}

你可能感兴趣的:(asp.net)