public async Task Update_Exercise() { using (var client = new APIClientProvider().Client) { // Create a dummy coffee Exercise moosehead = await CreateDummyExercise(); // Make a new title and assign it to our dummy coffee string newName = "Moosehead"; moosehead.Name = newName; // Convert it to JSON string modifiedmooseheadAsJSON = JsonConvert.SerializeObject(moosehead); // Try to PUT the newly edited coffee var response = await client.PutAsync( $"{url}/{moosehead.Id}", new StringContent(modifiedmooseheadAsJSON, Encoding.UTF8, "application/json") ); // See what comes back from the PUT. Is it a 204? string responseBody = await response.Content.ReadAsStringAsync(); Assert.Equal(HttpStatusCode.NoContent, response.StatusCode); // Get the edited coffee back from the database after the PUT var getModifiedExercise = await client.GetAsync($"{url}/{moosehead.Id}"); getModifiedExercise.EnsureSuccessStatusCode(); // Convert it to JSON string getExerciseBody = await getModifiedExercise.Content.ReadAsStringAsync(); // Convert it from JSON to C# Exercise newlyEditedExercise = JsonConvert.DeserializeObject <Exercise>(getExerciseBody); // Make sure the title was modified correctly Assert.Equal(HttpStatusCode.OK, getModifiedExercise.StatusCode); Assert.Equal(newName, newlyEditedExercise.Name); // Clean up after yourself await deleteDummyExercise(newlyEditedExercise); } }
public async Task Update_Instructor() { using (var client = new APIClientProvider().Client) { // Create a dummy coffee Instructor newLancePenn = await CreateDummyInstructor(); // Make a new title and assign it to our dummy coffee string newLastName = "Pennington"; newLancePenn.LastName = newLastName; // Convert it to JSON string modifiedLancePennAsJSON = JsonConvert.SerializeObject(newLancePenn); // Try to PUT the newly edited coffee var response = await client.PutAsync( $"{url}/{newLancePenn.Id}", new StringContent(modifiedLancePennAsJSON, Encoding.UTF8, "application/json") ); // See what comes back from the PUT. Is it a 204? string responseBody = await response.Content.ReadAsStringAsync(); Assert.Equal(HttpStatusCode.NoContent, response.StatusCode); // Get the edited coffee back from the database after the PUT var getModifiedInstructor = await client.GetAsync($"{url}/{newLancePenn.Id}"); getModifiedInstructor.EnsureSuccessStatusCode(); // Convert it to JSON string getInstructorBody = await getModifiedInstructor.Content.ReadAsStringAsync(); // Convert it from JSON to C# Instructor newlyEditedInstructor = JsonConvert.DeserializeObject <Instructor>(getInstructorBody); // Make sure the title was modified correctly Assert.Equal(HttpStatusCode.OK, getModifiedInstructor.StatusCode); Assert.Equal(newLastName, newlyEditedInstructor.LastName); // Clean up after yourself await deleteDummyInstructor(newlyEditedInstructor); } }
public async Task Delete_Exercise() { // Note: with many of these methods, I'm creating dummy data and then testing to see if I can delete it. I'd rather do that for now than delete something else I (or a user) created in the database, but it's not essential-- we could test deleting anything // Create a new coffee in the db Exercise moosehead = await CreateDummyExercise(); // Delete it await deleteDummyExercise(moosehead); using (var client = new APIClientProvider().Client) { // Try to get it again HttpResponseMessage response = await client.GetAsync($"{url}{moosehead.Id}"); // Make sure it's really gone Assert.Equal(HttpStatusCode.NotFound, response.StatusCode); } }
public async Task Get_All_Exercise() { using (var client = new APIClientProvider().Client) { // Try to get all of the coffees from /api/coffees HttpResponseMessage response = await client.GetAsync(url); response.EnsureSuccessStatusCode(); // Convert to JSON string responseBody = await response.Content.ReadAsStringAsync(); // Convert from JSON to C# List <Exercise> exercises = JsonConvert.DeserializeObject <List <Exercise> >(responseBody); // Make sure we got back a 200 OK Status and that there are more than 0 coffees in our database Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.True(exercises.Count > 0); } }
// Reusable method to create a new coffee in the database and return it public async Task <Exercise> CreateDummyExercise() { using (var client = new APIClientProvider().Client) { // Serialize the C# object into a JSON string string mooseheadAsJSON = JsonConvert.SerializeObject(dummyExercise); // Use the client to send the request and store the response HttpResponseMessage response = await client.PostAsync( url, new StringContent(mooseheadAsJSON, Encoding.UTF8, "application/json") ); // Store the JSON body of the response string responseBody = await response.Content.ReadAsStringAsync(); // Deserialize the JSON into an instance of Exercise Exercise newlyCreatedExercise = JsonConvert.DeserializeObject <Exercise>(responseBody); return(newlyCreatedExercise); } }