|
| 1 | +////////////////////////////////////////////////////////////////////////////////////// |
| 2 | +// Copyright (c) 2014, Electric Power Research Institute (EPRI) |
| 3 | +// All rights reserved. |
| 4 | +// |
| 5 | +// oadr2b-ven, oadrlib, and oadr-test ("this software") are licensed under the |
| 6 | +// BSD 3-Clause license. |
| 7 | +// |
| 8 | +// Redistribution and use in source and binary forms, with or without modification, |
| 9 | +// are permitted provided that the following conditions are met: |
| 10 | +// |
| 11 | +// * Redistributions of source code must retain the above copyright notice, this |
| 12 | +// list of conditions and the following disclaimer. |
| 13 | +// |
| 14 | +// * Redistributions in binary form must reproduce the above copyright notice, |
| 15 | +// this list of conditions and the following disclaimer in the documentation |
| 16 | +// and/or other materials provided with the distribution. |
| 17 | +// |
| 18 | +// * Neither the name of EPRI nor the names of its contributors may |
| 19 | +// be used to endorse or promote products derived from this software without |
| 20 | +// specific prior written permission. |
| 21 | +// |
| 22 | +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 23 | +// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 24 | +// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 25 | +// IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
| 26 | +// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 27 | +// NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 28 | +// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 29 | +// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 30 | +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY |
| 31 | +// OF SUCH DAMAGE. |
| 32 | + |
| 33 | +using System; |
| 34 | +using System.Text; |
| 35 | +using System.Collections.Generic; |
| 36 | +using System.Linq; |
| 37 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 38 | + |
| 39 | +using System.Net.Http; |
| 40 | + |
| 41 | +using System.Threading.Tasks; |
| 42 | + |
| 43 | +using System.IO; |
| 44 | + |
| 45 | +using oadrlib.lib.oadr2a; |
| 46 | +using oadrlib.lib.http; |
| 47 | + |
| 48 | +namespace oadr_test.CSharpLib |
| 49 | +{ |
| 50 | + [TestClass] |
| 51 | + public class UnitTestHttp |
| 52 | + { |
| 53 | + private String m_url = "http://192.168.2.117:8080/oadr2a-vtn/OpenADR2/Simple"; |
| 54 | + |
| 55 | + [TestMethod] |
| 56 | + public void httpClientGet() |
| 57 | + { |
| 58 | + |
| 59 | + HttpClient client = new HttpClient(); |
| 60 | + |
| 61 | + HttpResponseMessage response = client.GetAsync("http://www.slashdot.org/").Result; |
| 62 | + response.EnsureSuccessStatusCode(); |
| 63 | + |
| 64 | + string responseBody = response.Content.ReadAsStringAsync().Result; |
| 65 | + } |
| 66 | + |
| 67 | + // http://msdn.microsoft.com/en-us/library/system.net.webrequest.aspx |
| 68 | + // DOES NOT WORK! won't post |
| 69 | + // also having an issue with both httpclient and httpwebrequest hanging on occasion |
| 70 | + [TestMethod] |
| 71 | + public void webRequest() |
| 72 | + { |
| 73 | + System.Net.ServicePointManager.Expect100Continue = false; |
| 74 | + |
| 75 | + RequestEvent requestEvent = new RequestEvent(); |
| 76 | + |
| 77 | + string message = requestEvent.createOadrRequestEvent("TH_VEN"); |
| 78 | + |
| 79 | + System.Net.HttpWebRequest wr = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(m_url + "/EiEvent"); |
| 80 | + |
| 81 | + wr.Method = "POST"; |
| 82 | + System.Net.NetworkCredential nc = new System.Net.NetworkCredential("admin", "password"); |
| 83 | + |
| 84 | + wr.ContentType = "application/xml"; |
| 85 | + Stream s = wr.GetRequestStream(); |
| 86 | + |
| 87 | + s.Write(Encoding.UTF8.GetBytes(message), 0, message.Length); |
| 88 | + |
| 89 | + wr.Credentials = nc.GetCredential(wr.RequestUri, "Basic"); |
| 90 | + |
| 91 | + System.Net.WebResponse response = wr.GetResponse(); |
| 92 | + |
| 93 | + Stream stream = response.GetResponseStream(); |
| 94 | + |
| 95 | + StreamReader streamReader = new StreamReader(stream); |
| 96 | + |
| 97 | + string responseBody = streamReader.ReadToEnd(); |
| 98 | + |
| 99 | + System.Console.WriteLine(responseBody); |
| 100 | + } |
| 101 | + |
| 102 | + /***********************************************************************/ |
| 103 | + |
| 104 | + [TestMethod] |
| 105 | + public void venWebRequest() |
| 106 | + { |
| 107 | + VEN2a ven = new VEN2a(new HttpWebRequestWrapper(false, System.Net.SecurityProtocolType.Tls12), m_url, "TH_VEN", "password"); |
| 108 | + |
| 109 | + RequestEvent requestEvent = ven.requestEvent(); |
| 110 | + } |
| 111 | + |
| 112 | + /***********************************************************************/ |
| 113 | + |
| 114 | + [TestMethod] |
| 115 | + public void invalidBody() |
| 116 | + { |
| 117 | + // VEN2a ven = new VEN2a(new HttpWebRequestWrapper(), m_url, "TH_VEN", "password"); |
| 118 | + |
| 119 | + HttpWebRequestWrapper wr = new HttpWebRequestWrapper(false, System.Net.SecurityProtocolType.Tls12); |
| 120 | + |
| 121 | + wr.setAuthHeader("asdf"); |
| 122 | + |
| 123 | + wr.post(m_url + "/EiEvent", "application/xml", ""); |
| 124 | + } |
| 125 | + } |
| 126 | +} |
0 commit comments