Now that we have a completed REST API, let's test it in Postman to make sure that it works as we expected it would.
Now that we've got a completed Rest API, we can test it in person. 0:00 This can be the fun part of the process because we get to see 0:05 everything in action. 0:08 First make sure all of your code is saved. 0:09 Next fire up the express server in your terminal. 0:14 Now that it's running, we can start hitting it with requests from Postman. 0:22 Open up Postman so we can test our application. 0:29 Let's get all the questions in our app by selecting 0:33 the get method, entering, localhost:3000/questions. 0:38 Hit send. 0:43 And if you've created questions on your own your values will be here. 0:48 But as you see I have an empty array. 0:53 If you'd like to start with an empty collection the way I am, 0:56 you can switch over to another terminal window and enter mongo. 1:00 Use qa. 1:07 db.questions.drop. 1:10 If you had questions, the drop method would say true. 1:18 Since I don't have any questions, it just says false, quit 1:22 and then close the window and 1:30 go back of it's a Postman while we'll create a question. 1:33 We'll need to change the HTTP method to post. 1:38 Make sure the content type is set to application/Json in the headers. 1:44 Then in the body, we need to edit the request. 1:51 Selects row and change this to Json in the dropdown menu. 1:57 In the text editor we can type the Jason we want. 2:04 Which came first, the chicken or the egg. 2:14 Now when we hit send Our 2:24 request will be sent to the API and is processed by the router. 2:29 Which in hand takes it to the route handler and the route handler 2:34 will store the JSON in the database which will then return the saved document. 2:39 The handler will then send the document out of the API and 2:46 back to the client, which in this case is Postman. 2:50 The document will be shown in this window below. 2:54 And there's the document we just created. 2:59 You can see that Mongo has created an underscore ID, for 3:01 the document a created at time, 3:07 an empty array of answers, and 3:13 there's also __v property which Mongoose uses for internal use. 3:17 We won't need to worry about that for our purposes. 3:22 Great. 3:25 Now let's double click on the underscore id to select it. 3:26 Copy and paste into the URL after slash questions. 3:31 Choose get from the dropdown. 3:37 And when we send the request, 3:41 we might see nothing changes in the results window, and that's a good thing. 3:43 A change we might see is that the properties change their order. 3:51 But this is fine. 3:55 This request is hitting a different route in the app. 3:57 And if we saw an error we'd know something went wrong. 4:01 Now let's submit an answer to our question by posting it to the answers collection. 4:05 We'll type answers at the end of the URL, select post from the dropdown. 4:12 And since Postman holds onto the contents of the body here. 4:22 We'll need to modify that. 4:28 We can type in the answer to the document the chicken. 4:30 When we hit Send, we should see the question 4:37 document which holds the array of answers with our new answer. 4:43 The id the votes, 4:50 its updated app and created app which were all specified by the answer schema. 4:55 Let's add one more answer. 5:02 All the section should be correct from the requests we just made. 5:04 Let's just edit the text the egg 5:07 hit send and 5:13 we should see two answers in the document below. 5:18 Notice how the new answer The egg arrives at the top of the list. 5:22 The function we wrote to use in our static methods 5:29 is working correctly, and that the answers are being sorted by that updated date. 5:34 Now let's vote on the first answer we submitted to make sure that the answers 5:40 with the most votes rise to the top copy the id 5:44 from the lower answer and paste it to the end of the URL. 5:50 And then type vote-up. 5:59 Know that these ids are unique. 6:05 Yours won't match mine. 6:08 Now to vote we need to keep the HTTP method as post. 6:10 Send that and we should see that the first answer rises above the other answer 6:18 because it's votes are more than the other answers. 6:23 Click Send again and 6:27 you should see that the vote count has been incremented to two. 6:30 Now double click on the other answers id. 6:34 Copy it and paste over the other answers id, 6:39 clicking send now should increment the others answer to one. 6:45 Let's click it one more time and watch what happens. 6:52 It goes to the top, this is because both vote counts match, 6:57 the answers are now being sorted by the updated app properties. 7:02 Each answers up data app changes when they are voted on. 7:09 Let's click send one more time to give it three votes. 7:13 Now let's change the end of the URL to vote dash down 7:21 Clicking Send, the votes match again. 7:30 Now let's erase the URL back to the answer id. 7:34 Paste in the lower answers object id into the aID portion of the URL. 7:39 By changing the answers text property we can test the answers update 7:51 route as well as the sorting behavior we want. 7:56 Namely that when the votes match, the most recent updated answer rises. 8:00 So all we need to edit is the JSON we are sending. 8:05 It was the chicken. 8:14 We'll need to change the HTTP method to put. 8:18 Clicking Send should see this answer rise and change. 8:24 Now we see that works, 8:34 let's delete both of these answers since this argument will never be won anyway. 8:35 Changing the verb to delete. 8:42 And then clicking send. 8:47 We only have one answer now. 8:51 Double clicking the answers id and pacing that to the end of the URL. 8:55 And hitting send again, we see we have no answers. 9:03 Let's just add one more question to ensure that it gets sorted to the top 9:09 based on its created up property. 9:13 Delete the URL down to slash questions. 9:15 Select post. 9:20 And then let's edit the JSON' s text to, Why is the sky blue? 9:25 And we should see this single question here. 9:39 So now let's change it to the get verb and retrieve all the questions. 9:42 Hit Send and the newest question is at the top. 9:50 Now that we can see all of API features working correctly. 9:55 Let's test for some error handling. 9:59 Let's start by trying to edit a question. 10:01 We can take this new answers questions id and paste at the end of the URL. 10:04 Then change the verb to put. 10:11 And change the body to something like Why the sky yellow? 10:15 Clicking Send, we see that this is the error we wanted which is a 404 10:27 because the route is not found or in other words it's not implemented. 10:32 If we attempted to use an HTTP verb 10:37 on any of our routes that haven't been implemented. 10:40 We'll get a 404 or missing. 10:43 The functionality isn't there. 10:46 A 404 doesn't mean that it will never be there, though. 10:49 We could implement these on our roots in time. 10:53 If we choose to update our applications functionality or based on user feedback. 10:56 Let's test our answer's URLs. 11:02 Let's issue a get request to the question collection and 11:05 select the id of a question. 11:10 Then 11:15 Copy the id and paste it at the end 11:25 of the URL and type /answers. 11:29 Set the verb to post. 11:34 And for the text property of the JSON I'm going to paste in 11:39 an answer I found on Wikipedia but feel free to enter whatever you like, hit Send. 11:44 And the response comes back. 11:54 Copy and paste the answer id at the end of the URL. 11:56 And type about up. 12:05 And let's change this to the put. 12:12 Click Send. 12:15 And we get 404. 12:18 Let's delete the up, just leaving the vote and select pos. 12:19 Hit Send and see what happens. 12:30 It 404s too. 12:32 Let's type -dowwn, click Send and 12:33 we get the same error. 12:40 Sending votes UUP gives 12:44 us the same error too. 12:49 Great work. 12:54 Manual testing can get cumbersome. 12:55 Often when developing an API, we would want to implement automated tests. 12:58 Automated testing is beyond the scope of this course. 13:02 Find a link to more content on this subject in the teacher's notes. 13:05
You need to sign up for Treehouse in order to download course files.Sign up