public void EditMoviesWithInvalidAuth() { string director = "Beresford"; string title = "Driving Miss Daisy"; DateTime dateReleased = new DateTime(1990, 1, 26); client.DefaultHeaders.Add(psuedoAuthHeaderForPhil); Movie movieToEdit; using (HttpResponseMessage response = client.Get("AuthHome/details/1", "application/json")) { Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); movieToEdit = response.Content.ReadAsJsonDataContract <Movie>(); } movieToEdit.DateReleased = dateReleased; movieToEdit.Director = director; movieToEdit.Title = title; using (HttpResponseMessage response = client.Put("AuthHome/edit/1", HttpContentExtensions.CreateJsonDataContract(movieToEdit))) { Assert.AreEqual(HttpStatusCode.Unauthorized, response.StatusCode); } client.DefaultHeaders.Remove(psuedoAuthHeaderForPhil); }
static void InsertLocation(HttpClient client, Location location) { Console.WriteLine("Inserting location"); Console.WriteLine(); string insertUri = "Locations"; HttpContent content = HttpContentExtensions.CreateJsonDataContract(location); using (HttpResponseMessage response = client.Post(insertUri, content)) { response.EnsureStatusIsSuccessful(); } }
public void PostInvalidMovieNoTitleJson() { Movie noTitle = new Movie() { Director = "The Director", DateReleased = DateTime.Now }; using (HttpResponseMessage response = client.Post("Movies", HttpContentExtensions.CreateJsonDataContract(noTitle))) { Assert.AreEqual(HttpStatusCode.ExpectationFailed, response.StatusCode); Assert.IsTrue(response.Content.ReadAsString().Contains("The Title field is required.")); } }
public HttpContent CreateResourceDataContract(TResource resource, WebContentFormat format = WebContentFormat.Xml) { switch (format) { case WebContentFormat.Xml: return(HttpContentExtensions.CreateDataContract <TResource>(resource)); case WebContentFormat.Json: return(HttpContentExtensions.CreateJsonDataContract <TResource>(resource)); default: throw new NotImplementedException("Unsupported WebContentFormat"); } }
public void PostMoviesWithoutAuth() { string director = "Wachowski"; string title = "The Matrix"; DateTime dateReleased = new DateTime(1999, 3, 31); Movie movieToInsert = new Movie() { Director = director, DateReleased = dateReleased, Title = title }; using (HttpResponseMessage response = client.Post("AuthHome/create", HttpContentExtensions.CreateJsonDataContract(movieToInsert))) { Assert.AreEqual(HttpStatusCode.Unauthorized, response.StatusCode); } }
static Location UpdateLocation(HttpClient client, Location location) { Console.WriteLine("Updating location '{0}':", location.id); Console.WriteLine(); string updateUri = "Locations/" + location.id.ToString(); HttpContent content = HttpContentExtensions.CreateJsonDataContract(location); Console.WriteLine("Request"); WriteOutContent(content); using (HttpResponseMessage response = client.Put(updateUri, content)) { response.EnsureStatusIsSuccessful(); return(response.Content.ReadAsJsonDataContract <Location>()); } }
static Task UpdateTask(HttpClient client, Task task) { Console.WriteLine("Updating task '{0}':", task.Id); Console.WriteLine(); string updateUri = "Tasks/" + task.Id.ToString(); HttpContent content = HttpContentExtensions.CreateJsonDataContract(task); Console.WriteLine("Request:"); WriteOutContent(content); using (HttpResponseMessage response = client.Put(updateUri, content)) { response.EnsureStatusIsSuccessful(); Console.WriteLine("Response:"); WriteOutContent(response.Content); return(response.Content.ReadAsJsonDataContract <Task>()); } }
public void PostMoviesWithInvalidAuth() { string director = "Allen"; string title = "Annie Hall"; DateTime dateReleased = new DateTime(1977, 4, 30); client.DefaultHeaders.Add(psuedoAuthHeaderForPhil); Movie movieToInsert = new Movie() { Director = director, DateReleased = dateReleased, Title = title }; using (HttpResponseMessage response = client.Post("AuthHome/create", HttpContentExtensions.CreateJsonDataContract(movieToInsert))) { Assert.AreEqual(HttpStatusCode.Unauthorized, response.StatusCode); } client.DefaultHeaders.Remove(psuedoAuthHeaderForPhil); }
public void EnsureKeyCanAccessPackages(IEnumerable <string> packageIds, string key) { using (IHttpClient client = _httpClientAdapter.GetHttpClient(_configSettings.FrontEndWebSiteRoot)) { string uri = string.Format("{0}/{1}", _configSettings.AuthorizePackageIdsUri, key); HttpContent content = HttpContentExtensions.CreateJsonDataContract(packageIds); using (HttpResponseMessage response = client.Post(uri, content)) { if (response.StatusCode != HttpStatusCode.OK) { _logger.Error("External call returned non-OK status of '{0}'. Authorization for Packages failed.", response.StatusCode); throw new Exception("An error occurred when trying to authorize access to PackagesIds."); } bool accessAllowed; if (!bool.TryParse(response.Content.ReadAsString(), out accessAllowed) || !accessAllowed) { _logger.Error("The given userkey was refused access one or more of the given Package IDs."); throw new PackageAuthorizationException(); } } } }
public void EditMoviesWithoutAuth() { string director = "Crichton"; string title = "A Fish Called Wanda"; DateTime dateReleased = new DateTime(1988, 7, 15); Movie movieToEdit; using (HttpResponseMessage response = client.Get("AuthHome/details/1", "application/json")) { Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); movieToEdit = response.Content.ReadAsJsonDataContract <Movie>(); } movieToEdit.DateReleased = dateReleased; movieToEdit.Director = director; movieToEdit.Title = title; using (HttpResponseMessage response = client.Put("AuthHome/edit/1", HttpContentExtensions.CreateJsonDataContract(movieToEdit))) { Assert.AreEqual(HttpStatusCode.Unauthorized, response.StatusCode); } }
public void CrudMoviesJson() { string director = "Donen"; string title = "Singin' in the Rain"; DateTime dateReleased = new DateTime(1952, 4, 11); List <Movie> originalMovieList; List <Movie> updatedMovieList; client.DefaultHeaders.Add(psuedoAuthHeaderForBob); using (HttpResponseMessage response = client.Get("AuthHome", "application/json")) { Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); originalMovieList = response.Content.ReadAsJsonDataContract <List <Movie> >(); } Movie movieToInsert = new Movie() { Director = director, DateReleased = dateReleased, Title = title }; using (HttpResponseMessage response = client.Post("AuthHome/create", HttpContentExtensions.CreateJsonDataContract(movieToInsert))) { Assert.AreEqual(HttpStatusCode.Created, response.StatusCode); } using (HttpResponseMessage response = client.Get("AuthHome", "application/json")) { Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); updatedMovieList = response.Content.ReadAsJsonDataContract <List <Movie> >(); } Movie insertedMovie = updatedMovieList.Except(originalMovieList, moviesComparer).SingleOrDefault(); Assert.IsTrue(moviesComparer.Equals(movieToInsert, insertedMovie)); director = "Kelly"; // multiple director credits insertedMovie.Director = director; using (HttpResponseMessage response = client.Put("AuthHome/Edit/" + insertedMovie.Id.ToString(), HttpContentExtensions.CreateJsonDataContract(insertedMovie))) { Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); } using (HttpResponseMessage response = client.Get("AuthHome", "application/json")) { Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); updatedMovieList = response.Content.ReadAsJsonDataContract <List <Movie> >(); } Movie updatedMovie = updatedMovieList.Except(originalMovieList, moviesComparer).SingleOrDefault(); Assert.IsTrue(moviesComparer.Equals(insertedMovie, updatedMovie)); using (HttpResponseMessage response = client.Delete("AuthHome/delete/" + insertedMovie.Id.ToString())) { Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); } using (HttpResponseMessage response = client.Get("AuthHome", "application/json")) { Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); updatedMovieList = response.Content.ReadAsJsonDataContract <List <Movie> >(); } Assert.IsTrue(updatedMovieList.Union(originalMovieList, moviesComparer).Count() == updatedMovieList.Count()); client.DefaultHeaders.Remove(psuedoAuthHeaderForBob); }
public void CrudMoviesJsonDC() { string director = "Donen"; string title = "Singin' in the Rain"; DateTime dateReleased = new DateTime(1952, 4, 11); List <MovieDC> originalMovieList; List <MovieDC> updatedMovieList; MovieDC.MoviesComparerDC moviesComparer = new MovieDC.MoviesComparerDC(); using (HttpResponseMessage response = client.Get("EdmMovies", "application/json2")) { Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); originalMovieList = response.Content.ReadAsJsonDataContract <List <MovieDC> >(); } MovieDC movieToInsert = new MovieDC() { Director = director, DateReleased = dateReleased, Title = title }; using (HttpResponseMessage response = client.Post("EdmMovies", "application/json2", HttpContentExtensions.CreateJsonDataContract(movieToInsert))) { Assert.AreEqual(HttpStatusCode.Created, response.StatusCode); Assert.IsTrue(response.Headers.Location.ToString().StartsWith("/MovieApp/EdmMovies/", StringComparison.OrdinalIgnoreCase)); } using (HttpResponseMessage response = client.Get("EdmMovies", "application/json2")) { Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); updatedMovieList = response.Content.ReadAsJsonDataContract <List <MovieDC> >(); } MovieDC insertedMovie = updatedMovieList.Except(originalMovieList, moviesComparer).SingleOrDefault(); Assert.IsTrue(moviesComparer.EqualsIgnoreTimeSkew(movieToInsert, insertedMovie)); director = "Kelly"; // multiple director credits insertedMovie.Director = director; using (HttpResponseMessage response = client.Put("EdmMovies/" + insertedMovie.Id.ToString(), "application/json2", HttpContentExtensions.CreateJsonDataContract(insertedMovie))) { Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); } using (HttpResponseMessage response = client.Get("EdmMovies", "application/json2")) { Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); updatedMovieList = response.Content.ReadAsJsonDataContract <List <MovieDC> >(); } MovieDC updatedMovie = updatedMovieList.Except(originalMovieList, moviesComparer).SingleOrDefault(); Assert.IsTrue(moviesComparer.EqualsIgnoreTimeSkew(insertedMovie, updatedMovie)); using (HttpResponseMessage response = client.Delete("EdmMovies/" + insertedMovie.Id.ToString())) { Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); } using (HttpResponseMessage response = client.Get("EdmMovies", "application/json2")) { Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); updatedMovieList = response.Content.ReadAsJsonDataContract <List <MovieDC> >(); } Assert.IsTrue(updatedMovieList.Union(originalMovieList, moviesComparer).Count() == updatedMovieList.Count()); }