public void TestCrearRepetido() { JavaScriptSerializer js = new JavaScriptSerializer(); Plato platoNuevo = new Plato() { Nombre = "Lomo Saltado", Descripcion = "Plato que debes probar", Precio = (decimal)20.50, Url = "https://developersonline2019.net/resources/21.png" }; string data = js.Serialize(platoNuevo); byte[] dataByte = Encoding.UTF8.GetBytes(data); HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("http://localhost:5436/Platos.svc/Platos"); webRequest.Method = "POST"; webRequest.ContentLength = dataByte.Length; webRequest.ContentType = "application/json"; var requestStream = webRequest.GetRequestStream(); requestStream.Write(dataByte, 0, dataByte.Length); HttpWebResponse webResponse = null; try { webResponse = (HttpWebResponse)webRequest.GetResponse(); StreamReader streamReader = new StreamReader(webResponse.GetResponseStream()); string resultJSON = streamReader.ReadToEnd(); Plato platoCreado = js.Deserialize <Plato>(resultJSON); Assert.AreEqual(platoNuevo.Nombre, platoCreado.Nombre); Assert.AreEqual(platoNuevo.Descripcion, platoCreado.Descripcion); Assert.AreEqual(platoNuevo.Precio, platoCreado.Precio); Assert.AreEqual(platoNuevo.Url, platoCreado.Url); } catch (WebException ex) { HttpStatusCode code = ((HttpWebResponse)ex.Response).StatusCode; StreamReader reader = new StreamReader(ex.Response.GetResponseStream()); string tramaJSON = reader.ReadToEnd(); ManejoException foundError = js.Deserialize <ManejoException>(tramaJSON); Assert.AreEqual(HttpStatusCode.Conflict, code); Assert.AreEqual("El plato que desea registrar ya existe", foundError.descripcion); } }
public void TestObtenerPlato() { JavaScriptSerializer js = new JavaScriptSerializer(); HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("http://localhost:5436/Platos.svc/Platos/2015"); webRequest.Method = "GET"; HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse(); StreamReader streamReader = new StreamReader(webResponse.GetResponseStream()); string resultJSON = streamReader.ReadToEnd(); Plato platoObtenido = js.Deserialize <Plato>(resultJSON); Assert.AreEqual("Arroz Chaufa", platoObtenido.Nombre); Assert.AreEqual((decimal)20.5, platoObtenido.Precio); Assert.AreEqual("https://developersonline2019.net/resources/21.png", platoObtenido.Url); }
public void TestEliminarPlato() { HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("http://localhost:5436/Platos.svc/Platos/2026"); webRequest.Method = "DELETE"; HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse(); JavaScriptSerializer js = new JavaScriptSerializer(); HttpWebRequest webRequestND = (HttpWebRequest)WebRequest.Create("http://localhost:5436/Platos.svc/Platos/2026"); webRequestND.Method = "GET"; HttpWebResponse webResponseND = (HttpWebResponse)webRequestND.GetResponse(); StreamReader streamReader = new StreamReader(webResponseND.GetResponseStream()); string resultJSON = streamReader.ReadToEnd(); Plato platoObtenido = js.Deserialize <Plato>(resultJSON); Assert.IsNull(platoObtenido); }