1 00:00:00,480 --> 00:00:04,560 Okay, so let's start out with a little manual test to see if it's working. 2 00:00:04,560 --> 00:00:07,990 Now, we could definitely write up some JavaScript to test it out, but 3 00:00:07,990 --> 00:00:09,330 there are better ways. 4 00:00:09,330 --> 00:00:14,870 A super handy Chrome web app that I use to test my rest APIs is called Postman. 5 00:00:14,870 --> 00:00:17,420 Installation instructions are in the teacher's notes. 6 00:00:17,420 --> 00:00:18,515 Let me show you how to test your APIs. 7 00:00:20,162 --> 00:00:22,960 All right, so I was reviewing the code that we just wrote and 8 00:00:22,960 --> 00:00:25,920 I found a couple mistakes in the same line. 9 00:00:25,920 --> 00:00:29,640 It happens, totally happens, these are mistakes, we all make them. 10 00:00:29,640 --> 00:00:33,600 At the end of this extremely long line that's right here, 11 00:00:33,600 --> 00:00:38,690 the very end of this, I forgot to add the final closing single quote. 12 00:00:38,690 --> 00:00:40,350 Maybe you saw that when I did that. 13 00:00:40,350 --> 00:00:43,770 The other thing that I forgot to do is that there's extra parameters here. 14 00:00:43,770 --> 00:00:46,500 So there's username and password. 15 00:00:46,500 --> 00:00:48,660 And we're not using them, but if we don't put them there, 16 00:00:48,660 --> 00:00:50,390 we have a bit of a problem. 17 00:00:52,360 --> 00:00:54,700 It's a different method signature, and 18 00:00:54,700 --> 00:00:56,300 we're gonna end up calling the wrong thing. 19 00:00:56,300 --> 00:00:59,170 So that is now set up correctly. 20 00:00:59,170 --> 00:01:00,620 Okay, so we're ready to get the server running. 21 00:01:00,620 --> 00:01:04,310 So let's choose Run, and we'll choose Run, and 22 00:01:04,310 --> 00:01:08,140 this will create a new configuration for us called API, so we'll click that. 23 00:01:08,140 --> 00:01:10,160 There we go. It says Spark has ignited. 24 00:01:10,160 --> 00:01:14,120 All right, now, I'm gonna go and open up my Postman, 25 00:01:14,120 --> 00:01:15,330 which you probably have installed. 26 00:01:15,330 --> 00:01:17,720 We might be running different versions, so don't let that freak you out. 27 00:01:17,720 --> 00:01:18,440 It's an awesome tool. 28 00:01:18,440 --> 00:01:20,220 They're always adding new stuff. 29 00:01:20,220 --> 00:01:23,330 So the first thing that I want you to know here about Postman 30 00:01:23,330 --> 00:01:26,090 is that you can do the different types of requests. 31 00:01:26,090 --> 00:01:29,000 So what we wanna do first is we wanna post so that we can add a course. 32 00:01:29,000 --> 00:01:32,425 So we're gonna do a post, and we're gonna post to 33 00:01:32,425 --> 00:01:38,280 localhost4567, and we're gonna post to courses. 34 00:01:39,440 --> 00:01:40,950 I'm going to put an HTTP at the front of that. 35 00:01:43,198 --> 00:01:48,083 So we're gonna post to the URI of courses, and this is our server, right? 36 00:01:48,083 --> 00:01:50,300 It starts on 4567 as the default port. 37 00:01:51,640 --> 00:01:53,960 So, then why don't we add a header, 38 00:01:53,960 --> 00:01:57,580 a specific header that says content type, right? 39 00:01:57,580 --> 00:02:04,040 Because remember, it's only listening to that and I accidentally sent the post. 40 00:02:04,040 --> 00:02:10,487 So, content type and the value here is application/json. 41 00:02:10,487 --> 00:02:10,987 Okay. 42 00:02:12,770 --> 00:02:15,930 In the body, we also wanna pass the json, right? 43 00:02:15,930 --> 00:02:19,200 Remember, that's what we have promised, we had said we're going to send 44 00:02:19,200 --> 00:02:21,860 application json, so we're gonna right some json here. 45 00:02:21,860 --> 00:02:25,230 So, we'll do the name that we wanna send. 46 00:02:25,230 --> 00:02:31,463 So json is a key value and key colon, then the value that you want. 47 00:02:31,463 --> 00:02:37,018 So we'll say REST API Basics and then the URL, 48 00:02:37,018 --> 00:02:43,569 and then again, so if the URL's the key and the value is 49 00:02:43,569 --> 00:02:52,140 http:teamtreehouse.com/Library/rest-api-b- asics, cool. 50 00:02:52,140 --> 00:02:54,080 All right, so that's the body that we're gonna send up. 51 00:02:54,080 --> 00:02:54,800 So let's go ahead. 52 00:02:54,800 --> 00:02:59,580 So we're gonna post to courses, saying that we are application json and 53 00:02:59,580 --> 00:03:00,470 that's what the body looks like. 54 00:03:00,470 --> 00:03:01,130 So let's click Send. 55 00:03:02,670 --> 00:03:04,740 And we can scroll to the response. 56 00:03:04,740 --> 00:03:05,600 And there it is. 57 00:03:05,600 --> 00:03:07,250 There is REST API Basics. 58 00:03:07,250 --> 00:03:08,650 You probably have id 1. 59 00:03:08,650 --> 00:03:11,390 I had just tested this before to make sure that it works. 60 00:03:11,390 --> 00:03:12,960 So it has the id back. 61 00:03:12,960 --> 00:03:13,620 So that's awesome. 62 00:03:13,620 --> 00:03:16,020 And also notice here that is says 201 Created. 63 00:03:16,020 --> 00:03:19,410 That's because we sent that status code back, cuz we're good API citizens. 64 00:03:19,410 --> 00:03:20,810 Isn't that awesome? 65 00:03:20,810 --> 00:03:26,667 And so now, what happens is it stores that over here. 66 00:03:26,667 --> 00:03:29,503 So these are the ones that I did accidentally when I pressed Enter and 67 00:03:29,503 --> 00:03:31,740 here is the good working one that we passed. 68 00:03:31,740 --> 00:03:33,710 So we can make a new request. 69 00:03:33,710 --> 00:03:36,050 So let's make a different one, so let's do name, 70 00:03:36,050 --> 00:03:40,900 let make this will be Java basics, remember that one? 71 00:03:40,900 --> 00:03:43,759 And we'll change this from rest api to be java. 72 00:03:46,255 --> 00:03:49,170 And we'll click Send. 73 00:03:49,170 --> 00:03:51,570 Okay, we'll scroll to the response, and boom, there's a new one. 74 00:03:51,570 --> 00:03:53,700 It's got id number 3, awesome. 75 00:03:53,700 --> 00:03:58,372 So, post works, and now there's two, so let's switch our request to be a GET, 76 00:03:58,372 --> 00:03:58,880 right? 77 00:03:58,880 --> 00:04:02,340 So now we can list all of the courses that are available. 78 00:04:02,340 --> 00:04:06,380 So let's go ahead and send that GET. 79 00:04:06,380 --> 00:04:08,510 And that REST API Basics, that was the one that I tested with. 80 00:04:08,510 --> 00:04:12,090 Here's the two that we added, and on yours there should just be two, right? 81 00:04:12,090 --> 00:04:13,588 And see how there's these hard brackets here. 82 00:04:13,588 --> 00:04:18,290 See how it's showing that there is an array of these different courses. 83 00:04:18,290 --> 00:04:19,750 Awesome! 84 00:04:19,750 --> 00:04:23,630 And we also added a row that lets you see different IDs. 85 00:04:23,630 --> 00:04:25,870 So if I said 2, it should just return the 2. 86 00:04:25,870 --> 00:04:26,520 Awesome, it does. 87 00:04:26,520 --> 00:04:29,020 And if I say 3, does it get the third one? 88 00:04:29,020 --> 00:04:29,520 Awesome. 89 00:04:30,640 --> 00:04:34,760 And what happens if we do a different idea, like what if we do 42? 90 00:04:34,760 --> 00:04:35,432 Null. 91 00:04:35,432 --> 00:04:35,958 Hm? 92 00:04:35,958 --> 00:04:38,790 That's not great, is it? 93 00:04:38,790 --> 00:04:41,520 Because it says 200 OK but it's returning a null. 94 00:04:41,520 --> 00:04:44,890 It really should return a 404 not found, right? 95 00:04:44,890 --> 00:04:45,892 Let's table that for now. 96 00:04:45,892 --> 00:04:50,760 So, we've done our automated testing, but this is manual testing, right? 97 00:04:50,760 --> 00:04:53,580 So we've done automated testing together before where we test 98 00:04:53,580 --> 00:04:55,200 isolated code in small units of work. 99 00:04:55,200 --> 00:04:56,720 That's unit testing. 100 00:04:56,720 --> 00:04:59,570 We can take another approach, which is called functional testing. 101 00:04:59,570 --> 00:05:03,290 What this does is it allows you to test the system more in a way than an end user 102 00:05:03,290 --> 00:05:05,210 using your system might do things. 103 00:05:05,210 --> 00:05:08,250 So let's dive into functional testing, right after this quick break.