private async Task When_the_user_sends_the_restcall() { var clientProxyFactory = _container.GetInstance <IHttpClientProxyFactory>(); var clientProxy = clientProxyFactory.Create(); _fetchCommentsResult = await clientProxy.SendAsync <List <Comment> >(_restCall); }
public void Authentication_HrApi_Fail_Return_Not200() { //Arrage string empid = "TEST"; string password = "******"; HttpCallResult httpCallResult = new HttpCallResult { StatusCode = "401", FaultInfo = null, ReturnContent = "" }; _restfulApiClient.Post(Arg.Any <string>(), Arg.Any <Dictionary <string, string> >(), Arg.Any <string>()).Returns(httpCallResult); HrIdentityResponse expectedResponse = new HrIdentityResponse { Name_Ac = "", Z_Site_ID = "", Status = false }; //Act HrIdentityResponse actualResponse = _targetObj.Authentication(empid, password); //Assert Assert.AreEqual(expectedResponse.Name_Ac, actualResponse.Name_Ac); Assert.AreEqual(expectedResponse.Z_Site_ID, actualResponse.Z_Site_ID); Assert.AreEqual(expectedResponse.Status, actualResponse.Status); }
public void Authentication_HrApi_Pass_Return200() { //Arrage string empid = "TEST"; string password = "******"; HttpCallResult httpCallResult = new HttpCallResult { StatusCode = "200", FaultInfo = null, ReturnContent = "[{\"NAME_AC\": \"ESURENA RACHEL DOMINGUEZ\",\"Z_SITE_ID\": \"WIH\",\"status\": true}]" }; _restfulApiClient.Post(Arg.Any <string>(), Arg.Any <Dictionary <string, string> >(), Arg.Any <string>()).Returns(httpCallResult); HrIdentityResponse expectedResponse = new HrIdentityResponse { Name_Ac = "ESURENA RACHEL DOMINGUEZ", Z_Site_ID = "WIH", Status = true }; //Act HrIdentityResponse actualResponse = _targetObj.Authentication(empid, password); //Assert Assert.AreEqual(expectedResponse.Name_Ac, actualResponse.Name_Ac); Assert.AreEqual(expectedResponse.Z_Site_ID, actualResponse.Z_Site_ID); Assert.AreEqual(expectedResponse.Status, actualResponse.Status); }
public void Authentication_HrApi_Throw_Exception() { //Arrage string empid = "TEST"; string password = "******"; //讓JsonConvert 出現exception HttpCallResult httpCallResult = new HttpCallResult { StatusCode = "200", FaultInfo = null, ReturnContent = "#@!#$%^" }; _restfulApiClient.Post(Arg.Any <string>(), Arg.Any <Dictionary <string, string> >(), Arg.Any <string>()).Returns(httpCallResult); var expected = Assert.Throws <Exception>( () => { throw new Exception("Invoke HR API Exception:::Authentication", new Exception("Unexpected character encountered while parsing value: #. Path '', line 0, position 0.")); }); //Act Exception actual = Assert.Throws <Exception>(() => { _targetObj.Authentication(empid, password); }); //Assert Assert.AreEqual(expected.Message, actual.Message); Assert.AreEqual(expected.InnerException.Message, actual.InnerException.Message); }
/// <summary> /// Makes http request with the passed in parameters /// </summary> /// <param name="_url"></param> /// <param name="_authenticationHeaderValue"></param> /// <param name="_contentType"></param> /// <param name="_requestBodyString"></param> /// <returns></returns> public static HttpCallResult MakeHttpPostRequest(string _url, string _authenticationHeaderValue, string _contentType, string _requestBodyString) { HttpCallResult retVal = new HttpCallResult(); // Create web request var post = WebRequest.Create(_url) as HttpWebRequest; // Set the mthod type, content and authentication post.Method = "POST"; post.ContentType = _contentType; post.Headers[HttpRequestHeader.Authorization] = _authenticationHeaderValue; // Ste the body of the request var reqbody = Encoding.UTF8.GetBytes(_requestBodyString); post.ContentLength = reqbody.Length; using (var req = post.GetRequestStream()) { req.Write(reqbody, 0, reqbody.Length); } // Make the request // ansd set the status flags accordingly try { using (var response = post.GetResponse() as HttpWebResponse) { if (post.HaveResponse && response != null) { using (var reader = new StreamReader(response.GetResponseStream())) { retVal.ReplyStatusCode = HttpStatusCode.OK; retVal.ResponseBody = reader.ReadToEnd(); } } } } catch (WebException wex) { if (wex.Response != null) { using (var errorResponse = (HttpWebResponse)wex.Response) { retVal.ReplyStatusCode = errorResponse.StatusCode; using (var reader = new StreamReader(errorResponse.GetResponseStream())) { retVal.ResponseBody = reader.ReadToEnd(); } } } } return(retVal); }
/// <summary> /// 已知格式 status 200 登入成功: [{"NAME_AC":"ESURENA RACHEL DOMINGUEZ","Z_SITE_ID":"WIH","status":true}] /// status 200 登入失敗 : [{"status":false}] /// status 401 : {"error":{"statusCode":401,"name":"Error","message":"Authorization Required","code":"AUTHORIZATION_REQUIRED"}} /// </summary> /// <param name="empid"></param> /// <param name="password"></param> /// <returns></returns> public HrIdentityResponse Authentication(string empid, string password) { try { //http://{endpoint}/hrapi_external/portal_dlemp string url = $"{_config.Hr.BaseUrl}{"/"}{_config.Hr.IdentityCertificationPath}"; //none header attbibutes Dictionary <string, string> headers = new Dictionary <string, string>(); HrIdentityRequest hrIdentityRequest = new HrIdentityRequest { account = _config.Hr.Account, password = _config.Hr.PassWord, emplid = empid, user_password = password }; string jsonStr = JsonConvert.SerializeObject(hrIdentityRequest, Formatting.Indented); HttpCallResult result = _restfulApiClient.Post(url, headers, jsonStr); if (result.StatusCode == "200" || result.StatusCode == "201") { List <HrIdentityResponse> hrIdentityResponses = JsonConvert.DeserializeObject <List <HrIdentityResponse> >(result.ReturnContent); if (hrIdentityResponses != null) { //已知格式 return(hrIdentityResponses.FirstOrDefault()); } else { //如果拿到不適預期的格式一律給驗證錯誤 return new HrIdentityResponse { Name_Ac = "", Z_Site_ID = "", Status = false } }; } else//status 401 or another { return new HrIdentityResponse { Name_Ac = "", Z_Site_ID = "", Status = false } }; } catch (Exception ex) { var exception = new Exception("Invoke HR API Exception:::Authentication", ex); throw exception; } }
public async Task <HttpCallResult <T> > SendAsync <T>(HttpCall restCall) { try { var requestMessage = _requestMessageAdapter.Adapt(restCall); using var httpResponseMessage = await _httpClient.SendAsync(requestMessage); await LogResponseAsync(httpResponseMessage, restCall.RequestUri); return(await _resultAdapter.AdaptResultAsync <T>(httpResponseMessage)); } catch (HttpRequestException httpRequestException) { return(HttpCallResult <T> .CreateFailure(httpRequestException.Message)); } }
public void RestfulApiClient_Get_Return_StatusCode_200() { //Arrange string url = "https://portalapp-dev.wistron.com/api/Test/GetRuntimeError2"; HttpCallResult expectedResponse = new HttpCallResult { FaultInfo = null, ReturnContent = "{\"data\":null,\"rtnCode\":5000,\"rtnMsg\":\"伺服器/資料庫錯誤\"}", StatusCode = "200" }; //Act HttpCallResult actualResponse = _targetObj.Get(url, _header); //Assert Assert.AreEqual(expectedResponse.FaultInfo, actualResponse.FaultInfo); Assert.AreEqual(expectedResponse.ReturnContent, actualResponse.ReturnContent); Assert.AreEqual(expectedResponse.StatusCode, actualResponse.StatusCode); }
public void RestfulApiClient_Post_Return_StatusCode_200() { //Arrange string url = "https://portalapp-dev.wistron.com/api/Test/FakeAPComment"; string contentBody = "{}"; HttpCallResult expectedResponse = new HttpCallResult { FaultInfo = null, ReturnContent = null, StatusCode = "200" }; //Act HttpCallResult actualResponse = _targetObj.Post(url, _header, contentBody); //Assert Assert.AreEqual(expectedResponse.FaultInfo, actualResponse.FaultInfo); Assert.AreEqual(expectedResponse.StatusCode, actualResponse.StatusCode); }
public void RestfulApiClient_Post_Return_StatusCode_Not200() { //Arrange string url = $"{_externalConfigContext.Hr.BaseUrl}{"/"}{_externalConfigContext.Hr.IdentityCertificationPath}"; _header = new Dictionary <string, string>(); HrIdentityRequest hrIdentityRequest = new HrIdentityRequest { account = _externalConfigContext.Hr.Account, password = "******",//故意錯誤密碼 回401 emplid = "1071002P", user_password = "******" }; string contentBody = JsonConvert.SerializeObject(hrIdentityRequest); HttpCallResult expectedResponse = new HttpCallResult { FaultInfo = new ApiFault { Code = "401", Message = "{\"error\":{\"statusCode\":401,\"name\":\"Error\",\"message\":\"Authorization Required\",\"code\":\"AUTHORIZATION_REQUIRED\"}}" }, ReturnContent = "", StatusCode = "401" }; //Act HttpCallResult actualResponse = _targetObj.Post(url, _header, contentBody); //Assert Assert.AreEqual(expectedResponse.FaultInfo.Code, actualResponse.FaultInfo.Code); Assert.AreEqual(expectedResponse.FaultInfo.Message, actualResponse.FaultInfo.Message); Assert.AreEqual(expectedResponse.ReturnContent, actualResponse.ReturnContent); Assert.AreEqual(expectedResponse.StatusCode, actualResponse.StatusCode); }