• 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;}
     
    }

  • 相关阅读:
    POJ 1269 Intersecting Lines --计算几何
    URAL 2014 Zhenya moves from parents --线段树
    HDU 4122 Alice's mooncake shop --RMQ
    HDU 4121 Xiangqi --模拟
    HDU 4045 Machine scheduling --第二类Strling数
    HDU 4041 Eliminate Witches! --模拟
    HDU 5105 Math Problem --数学,求导
    2014-2015 Codeforces Trainings Season 2 Episode 7 G Gophers --线段树
    HDU 4419 Colourful Rectangle --离散化+线段树扫描线
    HDU 5102 The K-th Distance
  • 原文地址:https://www.cnblogs.com/fx2008/p/2817513.html
Copyright © 2020-2023  润新知