public static void UpdateResponseWithError(HttpRequest request, HttpResponse response, JsonMediaTypeFormatter jsonFormatter, XmlMediaTypeFormatter xmlFormatter, string errorMessage) { response.Clear(); response.StatusCode = (int)HttpStatusCode.BadRequest; var error = new HttpError(errorMessage); var acceptTypes = request.AcceptTypes?.Select(at => at.ToLowerInvariant()) ?? new string[] { }; if (xmlFormatter != null && xmlFormatter.SupportedMediaTypes.Select(mt => mt.MediaType).Intersect(acceptTypes).Any()) { xmlFormatter.CreateXmlSerializer(error.GetType()).Serialize(response.OutputStream, error); response.AddHeader("Content-Type", "application/xml"); } // JSON is the default format else if (jsonFormatter != null) { // Use UTF8Encoding contractor that does not provide Unicode byte order mark (BOM) // that in turn causes a deserialization error performed by Newtonsoft json formatter in integration tests. jsonFormatter.WriteToStream(error.GetType(), error, response.OutputStream, new UTF8Encoding()); response.AddHeader("Content-Type", "application/json"); } else { response.Write(errorMessage); response.AddHeader("Content-Type", "text/plain"); } }
public string WriteToStreamTestHelper(HttpError error) { var ms = new MemoryStream(); var fixture = new Fixture(); fixture.Customize(new AutoNSubstituteCustomization()); var content = fixture.Create <HttpContent>(); var transport = fixture.Create <TransportContext>(); var formatter = new ProblemJsonMediaTypeFormatter(); var task = formatter.WriteToStreamAsync(error.GetType(), error, ms, content, transport, new CancellationToken(false) ); task.Wait(); ms.Seek(0, SeekOrigin.Begin); return((new StreamReader(ms)).ReadToEnd()); }
public void Serialize_HttpError() { // Arrange var httpError = new HttpError(new Exception("This is the exception message"), true); var mockErrorDocumentBuilder = new Mock <IErrorDocumentBuilder>(MockBehavior.Strict); var mockErrorDocument = new Mock <IErrorDocument>(MockBehavior.Strict); mockErrorDocumentBuilder.Setup(b => b.BuildFromHttpError(httpError, HttpStatusCode.InternalServerError)) .Returns(mockErrorDocument.Object); var mockErrorDocumentFormatter = new Mock <IErrorDocumentFormatter>(MockBehavior.Strict); mockErrorDocumentFormatter.Setup(s => s.Serialize(mockErrorDocument.Object, It.IsAny <JsonWriter>())) .Returns((IErrorDocument errorDocument, JsonWriter writer) => { writer.WriteValue("HttpError document"); return(Task.FromResult(0)); }); var stream = new MemoryStream(); // Act var formatter = BuildFormatter(errorDocumentBuilder: mockErrorDocumentBuilder.Object, errorDocumentFormatter: mockErrorDocumentFormatter.Object); formatter.WriteToStreamAsync(httpError.GetType(), httpError, stream, null, null).Wait(); // Assert TestHelpers.StreamContentsMatchFixtureContents(stream, "Json/Fixtures/JsonApiFormatter/Serialize_HttpError.json"); }
public HttpResponseMessage GetBalance(string version, string appId, string customerId, string AccountNumber) { DataSet ds = new DataSet(); getBalanceRequest.version = version; getBalanceRequest.appID = appId; getBalanceRequest.customerID = customerId; getBalanceRequest.AccountNumber = AccountNumber; try { // DataSet ds = new DataSet(); string URL = "Balance/GetBalance&version?" + version + "&customerId?" + customerId + "&AccountNumber?" + AccountNumber + ""; ds = c.getInserlogrequest(URL); getBalanceResponse = APIBanking.DomesticRemittanceClient.getBalance(env, getBalanceRequest); StringWriter sw = new StringWriter(); XmlTextWriter tw = null; XmlSerializer serializer = new XmlSerializer(getBalanceResponse.GetType()); tw = new XmlTextWriter(sw); serializer.Serialize(tw, getBalanceResponse); string tes = sw.ToString(); c.updatelogrequest(Convert.ToInt32(ds.Tables[0].Rows[0]["KMR_Slno"]), tes.ToString()); // c.writelog(e.Message, "FaultException", DateTime.Now, "", ""); return(this.Request.CreateResponse(HttpStatusCode.OK, getBalanceResponse)); } catch (MessageSecurityException e) { Fault fault = new Fault(new APIBanking.Fault(e)); HttpError myCustomError = new HttpError(fault.Message); c.writelog(e.Message, "MessageSecurityException", DateTime.Now, "", ""); return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, myCustomError)); } catch (TimeoutException ex) { HttpError myCustomError = new HttpError(); myCustomError.Add("ErrorCode", 500); myCustomError.Add("Errormsg", ex.Message); myCustomError.Add("Ihno", AccountNumber); StringWriter sw = new StringWriter(); XmlTextWriter tw = null; XmlSerializer serializer = new XmlSerializer(myCustomError.GetType()); tw = new XmlTextWriter(sw); serializer.Serialize(tw, myCustomError); string tes = sw.ToString(); c.updatelogrequest(Convert.ToInt32(ds.Tables[0].Rows[0]["KMR_Slno"]), tes); //c.InsertResponse("500", ex.Message, requestReferenceNo, ""); c.writelog(ex.Message, "TimeoutException", DateTime.Now, "", ""); return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, myCustomError)); } catch (FaultException ex) { String faultCode = ex.Code.SubCode.Name; String FaultReason = ex.Message; message = faultCode + " - " + FaultReason; HttpError myCustomError = new HttpError(); myCustomError.Add("ErrorCode", faultCode); myCustomError.Add("Errormsg", FaultReason); myCustomError.Add("Ihno", AccountNumber); StringWriter sw = new StringWriter(); XmlTextWriter tw = null; XmlSerializer serializer = new XmlSerializer(myCustomError.GetType()); tw = new XmlTextWriter(sw); serializer.Serialize(tw, myCustomError); string tes = sw.ToString(); c.updatelogrequest(Convert.ToInt32(ds.Tables[0].Rows[0]["KMR_Slno"]), tes); //c.InsertResponse(faultCode, FaultReason, requestReferenceNo, ""); c.writelog(ex.Message, "FaultException", DateTime.Now, "", ""); return(Request.CreateResponse(HttpStatusCode.ExpectationFailed, myCustomError)); } catch (CommunicationException ex) { HttpError myCustomError = new HttpError(); myCustomError.Add("ErrorCode", 500); myCustomError.Add("Errormsg", ex.Message); myCustomError.Add("Ihno", AccountNumber); StringWriter sw = new StringWriter(); XmlTextWriter tw = null; XmlSerializer serializer = new XmlSerializer(myCustomError.GetType()); tw = new XmlTextWriter(sw); serializer.Serialize(tw, myCustomError); string tes = sw.ToString(); c.updatelogrequest(Convert.ToInt32(ds.Tables[0].Rows[0]["KMR_Slno"]), tes); c.writelog(ex.Message, "CommunicationException", DateTime.Now, "", ""); //c.InsertResponse("500", ex.Message, requestReferenceNo, ""); return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, myCustomError)); } catch (Exception ex) { HttpError myCustomError = new HttpError(); myCustomError.Add("ErrorCode", 500); myCustomError.Add("Errormsg", "InternerlServer Error"); myCustomError.Add("Ihno", AccountNumber); StringWriter sw = new StringWriter(); XmlTextWriter tw = null; XmlSerializer serializer = new XmlSerializer(myCustomError.GetType()); tw = new XmlTextWriter(sw); serializer.Serialize(tw, myCustomError); string tes = sw.ToString(); c.updatelogrequest(Convert.ToInt32(ds.Tables[0].Rows[0]["KMR_Slno"]), tes); //c.InsertResponse("500", ex.Message, requestReferenceNo, ""); c.writelog(ex.Message, "Exception", DateTime.Now, "", ""); return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, myCustomError)); } //return getBalanceResponse; }
public HttpResponseMessage TransferBal(string beneficiaryAccountNo, string beneficiaryIFSC, string beneficiaryMMID, string beneficiaryMobileNo, string Name, string address1, string emailID, string mobileNo, string uniqueRequestNo, string appID, string customerID, string debitAccountNo, float transferAmount) { DataSet ds = new DataSet(); string URL = "Transfer/TransferBal&beneficiaryAccountNo?" + beneficiaryAccountNo + "&beneficiaryIFSC?" + beneficiaryIFSC + "&beneficiaryMMID?" + beneficiaryMMID + "&beneficiaryMobileNo?" + beneficiaryMobileNo + "&Name?" + Name + "&address1?" + address1 + "&emailID?" + emailID + "&mobileNo?" + mobileNo + "&uniqueRequestNo?" + uniqueRequestNo + "&appID?" + appID + "&customerID?" + customerID + "&debitAccountNo?" + debitAccountNo + "&transferAmount?" + transferAmount + ""; ds = c.getInserlogrequest(URL); APIBanking.Environment env = new APIBanking.Environments.YBL.UAT("2449810", "Yesbank1", "7a7a26d8-1679-436b-854a-a2b5682bbf11", "nP8oE0tO5wR5kI1qD3aA6aR6wD6hR7hB8oP6qW5vU0hN0wE4sD", null); // APIBanking.Environment env = new APIBanking.Environments.YBL.UAT(ConfigurationManager.AppSettings["customerId"].ToString(), ConfigurationManager.AppSettings["Password"].ToString(), ConfigurationManager.AppSettings["clientId"].ToString(), ConfigurationManager.AppSettings["clientSecret"].ToString(), ConfigurationManager.AppSettings["CertificatePath"].ToString(), "123"); com.transfer gTransfer = new transfer(); com.transferRequest gTransferRequest = new transferRequest(); com.transferResponse gTransferResponse = new transferResponse(); beneficiaryDetailType b = new beneficiaryDetailType(); b.beneficiaryAccountNo = beneficiaryAccountNo; b.beneficiaryIFSC = beneficiaryIFSC; b.beneficiaryMMID = beneficiaryMMID; b.beneficiaryMobileNo = beneficiaryMobileNo; beneficiaryType bt = new beneficiaryType(); nameType nm = new nameType(); nm.Item = Name; AddressType ad = new AddressType(); ad.address1 = address1; //ad.address2 = ""; // ad.address3 = ""; // ad.city = ""; ad.country = "IN"; //ad.postalCode = ""; contactType ct = new contactType(); ct.emailID = emailID; ct.mobileNo = mobileNo; b.beneficiaryName = nm; b.beneficiaryAddress = ad; b.beneficiaryContact = ct; gTransfer.beneficiary = bt; gTransfer.beneficiary.Item = b; gTransfer.version = "2"; gTransfer.uniqueRequestNo = uniqueRequestNo; //Ihno gTransfer.appID = appID; gTransfer.customerID = customerID; gTransfer.debitAccountNo = debitAccountNo; gTransfer.transferAmount = transferAmount; //gTransfer.transferType = transferTypeType.IMPS; gTransfer.transferType = transferTypeType.IMPS; gTransfer.transferCurrencyCode = currencyCodeType.INR; gTransfer.remitterToBeneficiaryInfo = "FUND TRANSFER"; try { gTransferResponse = APIBanking.DomesticRemittanceClient.getTransfer(env, gTransfer); //return Request.CreateResponse(HttpStatusCode.OK, getBalanceResponse); StringWriter sw = new StringWriter(); XmlTextWriter tw = null; XmlSerializer serializer = new XmlSerializer(gTransferResponse.GetType()); tw = new XmlTextWriter(sw); serializer.Serialize(tw, gTransferResponse); string tes = sw.ToString(); c.updatelogrequest(Convert.ToInt32(ds.Tables[0].Rows[0]["KMR_Slno"]), tes.ToString()); c.InsertResponse(gTransferResponse.transactionStatus.subStatusCode, gTransferResponse.transactionStatus.statusCode.ToString(), gTransferResponse.requestReferenceNo, gTransferResponse.transactionStatus.bankReferenceNo); return(this.Request.CreateResponse(HttpStatusCode.OK, gTransferResponse)); } catch (FaultException ex) { String faultCode = ex.Code.SubCode.Name; String FaultReason = ex.Message; message = faultCode + " - " + FaultReason; HttpError myCustomError = new HttpError(); myCustomError.Add("ErrorCode", faultCode); myCustomError.Add("Errormsg", FaultReason); myCustomError.Add("Ihno", uniqueRequestNo); StringWriter sw = new StringWriter(); XmlTextWriter tw = null; XmlSerializer serializer = new XmlSerializer(myCustomError.GetType()); tw = new XmlTextWriter(sw); serializer.Serialize(tw, myCustomError); string tes = sw.ToString(); c.updatelogrequest(Convert.ToInt32(ds.Tables[0].Rows[0]["KMR_Slno"]), tes); c.InsertResponse(faultCode, FaultReason, uniqueRequestNo, ""); c.writelog(ex.Message, "FaultException", DateTime.Now, "", ""); return(Request.CreateResponse(HttpStatusCode.ExpectationFailed, myCustomError)); } //catch (TimeoutException ex) //{ // message = ex.Message; // HttpError myCustomError = new HttpError(message); // // return Request.CreateErrorResponse(HttpStatusCode.BadRequest, myCustomError); // //return this.Request.CreateResponse(HttpStatusCode.OK, gTransferResponse); //} //catch (CommunicationException ex) //{ // message = ex.Message; // HttpError myCustomError = new HttpError(message); // // return Request.CreateErrorResponse(HttpStatusCode.BadRequest, myCustomError); // //return this.Request.CreateResponse(HttpStatusCode.OK, gTransferResponse); //} catch (Exception ex) { c.writelog(ex.Message, "TransferBal", DateTime.Now, "", ""); HttpError myCustomError = new HttpError(); myCustomError.Add("ErrorCode", 500); myCustomError.Add("Errormsg", "InternerlServer Error"); myCustomError.Add("Ihno", uniqueRequestNo); StringWriter sw = new StringWriter(); XmlTextWriter tw = null; XmlSerializer serializer = new XmlSerializer(myCustomError.GetType()); tw = new XmlTextWriter(sw); serializer.Serialize(tw, myCustomError); string tes = sw.ToString(); c.updatelogrequest(Convert.ToInt32(ds.Tables[0].Rows[0]["KMR_Slno"]), tes); c.InsertResponse("500", ex.Message, uniqueRequestNo, ""); c.writelog(ex.Message, "TransferBal", DateTime.Now, "", ""); return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, myCustomError)); } }
public void Serialize_HttpError() { // Arrange var httpError = new HttpError(new Exception("This is the exception message"), true); var mockErrorDocumentBuilder = new Mock<IErrorDocumentBuilder>(MockBehavior.Strict); var mockErrorDocument = new Mock<IErrorDocument>(MockBehavior.Strict); mockErrorDocumentBuilder.Setup(b => b.BuildFromHttpError(httpError, HttpStatusCode.InternalServerError)) .Returns(mockErrorDocument.Object); var mockErrorDocumentFormatter = new Mock<IErrorDocumentFormatter>(MockBehavior.Strict); mockErrorDocumentFormatter.Setup(s => s.Serialize(mockErrorDocument.Object, It.IsAny<JsonWriter>())) .Returns((IErrorDocument errorDocument, JsonWriter writer) => { writer.WriteValue("HttpError document"); return Task.FromResult(0); }); var stream = new MemoryStream(); // Act var formatter = BuildFormatter(errorDocumentBuilder: mockErrorDocumentBuilder.Object, errorDocumentFormatter: mockErrorDocumentFormatter.Object); formatter.WriteToStreamAsync(httpError.GetType(), httpError, stream, null, null).Wait(); // Assert TestHelpers.StreamContentsMatchFixtureContents(stream, "Json/Fixtures/JsonApiFormatter/Serialize_HttpError.json"); }
public HttpResponseMessage GetStatus(string appId, string customerId, string requestReferenceNo) { DataSet ds = new DataSet(); string URL = "Status/GetStatus&appId?" + appId + "&customerId?" + customerId + "&requestReferenceNo?" + requestReferenceNo + ""; ds = c.getInserlogrequest(URL); com.getStatus gStatus = new com.getStatus(); com.getStatusRequest gStatusRequest = new com.getStatusRequest(); com.getStatusResponse gStatusResponse = new com.getStatusResponse(); gStatus.version = "2.0"; gStatus.appID = appId; gStatus.customerID = customerId; gStatus.requestReferenceNo = requestReferenceNo; try { gStatusResponse = APIBanking.DomesticRemittanceClient.getStatus(env, gStatus); StringWriter sw = new StringWriter(); XmlTextWriter tw = null; XmlSerializer serializer = new XmlSerializer(gStatusResponse.GetType()); tw = new XmlTextWriter(sw); serializer.Serialize(tw, gStatusResponse); string tes = sw.ToString(); c.updatelogrequest(Convert.ToInt32(ds.Tables[0].Rows[0]["KMR_Slno"]), tes.ToString()); //c.updatelogrequest(Convert.ToInt32(ds.Tables[0].Rows[0]["KMR_Slno"]), gStatusResponse.ToString()); return(this.Request.CreateResponse(HttpStatusCode.OK, gStatusResponse)); } //catch (TimeoutException ex) //{ // message = ex.Message; // HttpError myCustomError = new HttpError(message); // return Request.CreateErrorResponse(HttpStatusCode.BadRequest, myCustomError); //} catch (FaultException ex) { String faultCode = ex.Code.SubCode.Name; String FaultReason = ex.Message; message = faultCode + " - " + FaultReason; HttpError myCustomError = new HttpError(); myCustomError.Add("ErrorCode", faultCode); myCustomError.Add("Errormsg", FaultReason); myCustomError.Add("Ihno", requestReferenceNo); StringWriter sw = new StringWriter(); XmlTextWriter tw = null; XmlSerializer serializer = new XmlSerializer(myCustomError.GetType()); tw = new XmlTextWriter(sw); serializer.Serialize(tw, myCustomError); string tes = sw.ToString(); c.updatelogrequest(Convert.ToInt32(ds.Tables[0].Rows[0]["KMR_Slno"]), tes); //c.InsertResponse(faultCode, FaultReason, requestReferenceNo, ""); return(Request.CreateResponse(HttpStatusCode.ExpectationFailed, myCustomError)); } //catch (CommunicationException ex) //{ // message = ex.Message; // HttpError myCustomError = new HttpError(message); // return Request.CreateErrorResponse(HttpStatusCode.BadRequest, myCustomError); //} catch (Exception ex) { HttpError myCustomError = new HttpError(); myCustomError.Add("ErrorCode", 500); myCustomError.Add("Errormsg", "InternerlServer Error"); myCustomError.Add("Ihno", requestReferenceNo); StringWriter sw = new StringWriter(); XmlTextWriter tw = null; XmlSerializer serializer = new XmlSerializer(myCustomError.GetType()); tw = new XmlTextWriter(sw); serializer.Serialize(tw, myCustomError); string tes = sw.ToString(); c.updatelogrequest(Convert.ToInt32(ds.Tables[0].Rows[0]["KMR_Slno"]), tes); //c.InsertResponse("500", ex.Message, requestReferenceNo, ""); return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, myCustomError)); } }