public void MoviesUnsupportedMediaTypeJson()
        {
            HttpContent content = HttpContent.Create(new MemoryStream(new byte[] { 1, 2, 3 }), "foo", null);

            content.LoadIntoBuffer();
            using (HttpResponseMessage response = client.Post("Movies", content, "application/json"))
            {
                Assert.AreEqual(HttpStatusCode.UnsupportedMediaType, response.StatusCode);
                Assert.AreEqual("application/json; charset=utf-8", response.Content.ContentType);
            }
        }
示例#2
0
 void DumpContent(HttpContent httpContent)
 {
     if (!HttpContent.IsNullOrEmpty(httpContent))
     {
         string content = null;
         try
         {
             httpContent.LoadIntoBuffer();
             content = httpContent.ReadAsString();
         }
         catch (Exception exception)
         {
             content = "$Exception: " + exception.ToString() + "$";
         }
         if (!string.IsNullOrEmpty(content))
         {
             Console.WriteLine(content);
         }
     }
 }
示例#3
0
 static void WriteOutContent(HttpContent content)
 {
     content.LoadIntoBuffer();
     Console.WriteLine(content.ReadAsString());
     Console.WriteLine();
 }
        public void CrudMoviesAtom()
        {
            string          director     = "Donen";
            string          title        = "Singin' in the Rain";
            DateTime        dateReleased = new DateTime(1952, 4, 11);
            SyndicationFeed originalMovieList;
            SyndicationFeed updatedMovieList;

            using (HttpResponseMessage response = client.Get("Movies", "application/atom+xml"))
            {
                Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
                originalMovieList = response.Content.ReadAsSyndicationFeed();
            }

            Movie movieToInsert = new Movie()
            {
                Director = director, DateReleased = dateReleased, Title = title
            };
            SyndicationItem item = new SyndicationItem()
            {
                Title = new TextSyndicationContent(movieToInsert.Title), Content = new XmlSyndicationContent(null, movieToInsert, new DataContractSerializer(typeof(Movie)))
            };
            HttpContent itemContent = HttpContent.Create((s) => { using (XmlWriter w = XmlWriter.Create(s)) { item.SaveAsAtom10(w); } }, "application/atom+xml;type=entry", null);

            itemContent.LoadIntoBuffer();
            using (HttpResponseMessage response = client.Post("Movies", itemContent))
            {
                Assert.AreEqual(HttpStatusCode.Created, response.StatusCode);
                Assert.IsTrue(response.Headers.Location.ToString().StartsWith("/MovieApp/movies/", StringComparison.OrdinalIgnoreCase));
            }

            using (HttpResponseMessage response = client.Get("Movies", "application/atom+xml"))
            {
                Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
                updatedMovieList = response.Content.ReadAsSyndicationFeed();
            }

            SyndicationItem insertedMovieItem = updatedMovieList.Items.Except(originalMovieList.Items, entriesComparer).SingleOrDefault();
            Movie           insertedMovie     = ((XmlSyndicationContent)insertedMovieItem.Content).ReadContent <Movie>();

            Assert.IsTrue(moviesComparer.Equals(movieToInsert, insertedMovie));

            director = "Kelly"; // multiple director credits
            insertedMovie.Director = director;
            item = new SyndicationItem()
            {
                Title = new TextSyndicationContent(insertedMovie.Title), Content = new XmlSyndicationContent(null, insertedMovie, new DataContractSerializer(typeof(Movie)))
            };
            itemContent = HttpContent.Create((s) => { using (XmlWriter w = XmlWriter.Create(s)) { item.SaveAsAtom10(w); } }, "application/atom+xml;type=entry", null);
            itemContent.LoadIntoBuffer();
            using (HttpResponseMessage response = client.Put("Movies/" + insertedMovie.Id.ToString(), itemContent))
            {
                Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
            }

            using (HttpResponseMessage response = client.Get("Movies", "application/atom+xml"))
            {
                Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
                updatedMovieList = response.Content.ReadAsSyndicationFeed();
            }

            SyndicationItem updatedMovieItem = updatedMovieList.Items.Except(originalMovieList.Items, entriesComparer).SingleOrDefault();
            Movie           updatedMovie     = ((XmlSyndicationContent)updatedMovieItem.Content).ReadContent <Movie>();

            Assert.IsTrue(moviesComparer.Equals(insertedMovie, updatedMovie));

            using (HttpResponseMessage response = client.Delete("Movies/" + insertedMovie.Id.ToString()))
            {
                Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
            }

            using (HttpResponseMessage response = client.Get("Movies", "application/atom+xml"))
            {
                Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
                updatedMovieList = response.Content.ReadAsSyndicationFeed();
            }

            Assert.IsTrue(updatedMovieList.Items.Union(originalMovieList.Items, entriesComparer).Count() == updatedMovieList.Items.Count());
        }
示例#5
0
        private RestContent SendHttpRequest <T>(
            string url,
            RestRequest request,
            T objectToSend)
            where T : class
        {
            if (log.IsDebugEnabled)
            {
                log.DebugFormat(
                    "Calling OE REST (operation='{0}', url='{1}')",
                    request.Operation,
                    url);
            }

            Uri requestUrl = new Uri(url);

            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);

            webRequest.Method      = request.Operation.ToString();
            webRequest.ContentType = "text/xml";

            SetupWebRequest(webRequest);

            try
            {
                if (objectToSend != null)
                {
                    using (Stream requestStream = webRequest.GetRequestStream())
                    {
                        using (HttpContent httpContent =
                                   HttpContentExtensions.CreateXmlSerializable(objectToSend))
                        {
                            if (log.IsDebugEnabled)
                            {
                                httpContent.LoadIntoBuffer();
                                log.DebugFormat("Sending XML: {0}", httpContent.ReadAsString());
                            }

                            httpContent.WriteTo(requestStream);
                        }
                    }
                }

                using (HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse())
                {
                    RaiseOERestExceptionIfNeeded(requestUrl, response);

                    RestContent oecontent;

                    oecontent = new RestContent(
                        ReadResponseAsString(response),
                        requestUrl);

                    if (log.IsDebugEnabled)
                    {
                        log.DebugFormat("Response: '{0}'", oecontent.ContentString);
                    }

                    return(oecontent);
                }
            }
            catch (WebException ex)
            {
                if (ex.Response == null)
                {
                    if (log.IsDebugEnabled)
                    {
                        log.Debug("ex.Response == null");
                    }

                    string message = string.Format(
                        CultureInfo.InvariantCulture,
                        "The call to '{0}' failed",
                        url);

                    throw new RestException(requestUrl, message, ex);
                }

                RaiseOERestExceptionIfNeeded(requestUrl, (HttpWebResponse)ex.Response);
                throw;
            }
            catch (Exception ex)
            {
                string message = string.Format(
                    CultureInfo.InvariantCulture,
                    "Exception while calling '{0}'",
                    url);

                log.Warn(message, ex);
                throw;
            }
        }