public void PostVPNAsXMLWithWebRequest() { string baseAddress = "http://*****:*****@"<SendRequest xmlns:i='http://www.w3.org/2001/XMLSchema-instance' xmlns='http://schemas.datacontract.org/2004/07/ContractLib'> <VPNKey>{0}</VPNKey> <Topic>{1}</Topic> <Message>{2}</Message> <CorrelationID>{3}</CorrelationID> </SendRequest>"; var payload_text = string.Format(payload_template, request_payload.VPNKey, request_payload.Topic, request_payload.Message, request_payload.CorrelationID); //var serial = new System.Xml.Serialization.XmlSerializer(typeof(SendRequest)); //var w = new StringWriter(); //serial.Serialize(w, request_payload); w.Flush(); //var payload_text = w.GetStringBuilder().ToString(); //var serial = new System.Runtime.Serialization.DataContractSerializer(typeof(SendRequest)); //var w = new StringWriter(); //var ww = System.Xml.XmlWriter.Create(w); //serial.WriteObject(ww, request_payload); w.Flush(); //var payload_text = w.GetStringBuilder().ToString(); var payload = System.Text.Encoding.UTF8.GetBytes(payload_text); //Assert.AreEqual<string>("", payload_text); using (var bodystream = request.GetRequestStream()) { bodystream.Write(payload, 0, payload.Length); } using (var response = request.GetResponse()) { //Assert.AreEqual<string>("vpn2", VpnController.received_request.VPNKey); //Assert.AreEqual<string>("B/C", VpnController.received_request.Topic); //Assert.AreEqual<string>("msg2", VpnController.received_request.Message); //Assert.AreEqual<uint>(456, VpnController.received_request.CorrelationID); var http_response = response as System.Net.HttpWebResponse; Assert.AreEqual<System.Net.HttpStatusCode>(System.Net.HttpStatusCode.OK, http_response.StatusCode); Assert.AreEqual<string>("OK", http_response.StatusDescription); using (var response_stream = response.GetResponseStream()) using (var reader = new StreamReader(response_stream)) { string response_xml = reader.ReadToEnd(); //Assert.AreEqual<string>("", response_xml); var response_doc = XDocument.Parse(response_xml); XNamespace ns = "http://schemas.datacontract.org/2004/07/ContractLib"; Assert.IsNotNull(response_doc.Root); Assert.IsTrue(response_doc.Root.HasElements); Assert.AreEqual<int>(3, response_doc.Root.Elements().Count()); var correlationID = response_doc.Root.Element(ns + "CorrelationID"); Assert.IsNotNull(correlationID); var status = response_doc.Root.Element(ns + "Status"); Assert.IsNotNull(status); var description = response_doc.Root.Element(ns + "Description"); Assert.IsNotNull(description); //Assert.AreEqual<string>(request_payload.CorrelationID.ToString(), correlationID.Value); //Assert.AreEqual<string>("102", status.Value); //Assert.AreEqual<string>(request_payload.VPNKey, description.Value); //Assert.AreEqual<string>(request_payload.Topic, description.Value); //Assert.IsTrue(description.Value.StartsWith("--")); //Assert.AreEqual<string>(string.Format("{0}|{1}|{2}", request_payload.VPNKey, request_payload.Topic, request_payload.Message), description.Value); } } } }
// POST api/vpn public SendResponse Post([FromBody]SendRequest request) { //async System.Threading.Tasks.Task< //var payload = new MemoryStream(); //System.Threading.Tasks.Task task = Request.Content.CopyToAsync(payload); //var line = Encoding.UTF8.GetString(payload.GetBuffer()); //var line = Request.Content.ReadAsStringAsync().Result; //var line = Request.ToString(); //Request.Content.LoadIntoBufferAsync() //System.Diagnostics.Trace.WriteLine("Request Payload:"); //System.Diagnostics.Trace.WriteLine(string.Format("[{0}]",line)); received_request = request; return new SendResponse() { CorrelationID = request.CorrelationID, Status = 102, Description = request.VPNKey + "|" + request.Topic + "|" + request.Message }; //return new SendResponse() { CorrelationID = request.CorrelationID, Status = 102, Description = request.Topic }; }
public void PostVPN() { string baseAddress = "http://localhost:6000/"; var request_payload = new SendRequest() { VPNKey = "vpn1", Topic = "A/B", Message = "msg1", CorrelationID = 123 }; using (Microsoft.Owin.Hosting.WebApp.Start<VpnStartup>(url: baseAddress)) { using (var client = new System.Net.Http.HttpClient()) { var response = client.PostAsXmlAsync(baseAddress + "api/vpn", request_payload).Result; Assert.AreEqual<string>("vpn1", VpnController.received_request.VPNKey); Assert.AreEqual<string>("A/B", VpnController.received_request.Topic); Assert.AreEqual<string>("msg1", VpnController.received_request.Message); Assert.AreEqual<uint>(123, VpnController.received_request.CorrelationID ); Assert.IsTrue(response.IsSuccessStatusCode); Assert.AreEqual<System.Net.HttpStatusCode>(System.Net.HttpStatusCode.OK, response.StatusCode); Assert.AreEqual<string>("OK", response.ReasonPhrase); string response_xml = response.Content.ReadAsStringAsync().Result; Assert.IsTrue(response_xml.Length > 0); var response_doc = XDocument.Parse(response_xml); XNamespace ns = "http://schemas.datacontract.org/2004/07/ContractLib"; Assert.IsNotNull(response_doc.Root); Assert.IsTrue(response_doc.Root.HasElements); Assert.AreEqual<int>(3, response_doc.Root.Elements().Count()); var correlationID = response_doc.Root.Element(ns + "CorrelationID"); Assert.IsNotNull(correlationID); var status = response_doc.Root.Element(ns + "Status"); Assert.IsNotNull(status); var description = response_doc.Root.Element(ns + "Description"); Assert.IsNotNull(description); Assert.AreEqual<string>("123", correlationID.Value); Assert.AreEqual<string>("102", status.Value); Assert.AreEqual<string>(string.Format("{0}|{1}|{2}", request_payload.VPNKey, request_payload.Topic, request_payload.Message), description.Value); } } }