Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
We have a working REST API, let's go poke it with a stick. Let me show off one of my favorite REST tools Postman.
Installation Instructions
-
0:00
Okay, so let's start out with a little manual test to see if it's working.
-
0:04
Now, we could definitely write up some JavaScript to test it out, but
-
0:07
there are better ways.
-
0:09
A super handy Chrome web app that I use to test my rest APIs is called Postman.
-
0:14
Installation instructions are in the teacher's notes.
-
0:17
Let me show you how to test your APIs.
-
0:20
All right, so I was reviewing the code that we just wrote and
-
0:22
I found a couple mistakes in the same line.
-
0:25
It happens, totally happens, these are mistakes, we all make them.
-
0:29
At the end of this extremely long line that's right here,
-
0:33
the very end of this, I forgot to add the final closing single quote.
-
0:38
Maybe you saw that when I did that.
-
0:40
The other thing that I forgot to do is that there's extra parameters here.
-
0:43
So there's username and password.
-
0:46
And we're not using them, but if we don't put them there,
-
0:48
we have a bit of a problem.
-
0:52
It's a different method signature, and
-
0:54
we're gonna end up calling the wrong thing.
-
0:56
So that is now set up correctly.
-
0:59
Okay, so we're ready to get the server running.
-
1:00
So let's choose Run, and we'll choose Run, and
-
1:04
this will create a new configuration for us called API, so we'll click that.
-
1:08
There we go. It says Spark has ignited.
-
1:10
All right, now, I'm gonna go and open up my Postman,
-
1:14
which you probably have installed.
-
1:15
We might be running different versions, so don't let that freak you out.
-
1:17
It's an awesome tool.
-
1:18
They're always adding new stuff.
-
1:20
So the first thing that I want you to know here about Postman
-
1:23
is that you can do the different types of requests.
-
1:26
So what we wanna do first is we wanna post so that we can add a course.
-
1:29
So we're gonna do a post, and we're gonna post to
-
1:32
localhost4567, and we're gonna post to courses.
-
1:39
I'm going to put an HTTP at the front of that.
-
1:43
So we're gonna post to the URI of courses, and this is our server, right?
-
1:48
It starts on 4567 as the default port.
-
1:51
So, then why don't we add a header,
-
1:53
a specific header that says content type, right?
-
1:57
Because remember, it's only listening to that and I accidentally sent the post.
-
2:04
So, content type and the value here is application/json.
-
2:10
Okay.
-
2:12
In the body, we also wanna pass the json, right?
-
2:15
Remember, that's what we have promised, we had said we're going to send
-
2:19
application json, so we're gonna right some json here.
-
2:21
So, we'll do the name that we wanna send.
-
2:25
So json is a key value and key colon, then the value that you want.
-
2:31
So we'll say REST API Basics and then the URL,
-
2:37
and then again, so if the URL's the key and the value is
-
2:43
http:teamtreehouse.com/Library/rest-api-b- asics, cool.
-
2:52
All right, so that's the body that we're gonna send up.
-
2:54
So let's go ahead.
-
2:54
So we're gonna post to courses, saying that we are application json and
-
2:59
that's what the body looks like.
-
3:00
So let's click Send.
-
3:02
And we can scroll to the response.
-
3:04
And there it is.
-
3:05
There is REST API Basics.
-
3:07
You probably have id 1.
-
3:08
I had just tested this before to make sure that it works.
-
3:11
So it has the id back.
-
3:12
So that's awesome.
-
3:13
And also notice here that is says 201 Created.
-
3:16
That's because we sent that status code back, cuz we're good API citizens.
-
3:19
Isn't that awesome?
-
3:20
And so now, what happens is it stores that over here.
-
3:26
So these are the ones that I did accidentally when I pressed Enter and
-
3:29
here is the good working one that we passed.
-
3:31
So we can make a new request.
-
3:33
So let's make a different one, so let's do name,
-
3:36
let make this will be Java basics, remember that one?
-
3:40
And we'll change this from rest api to be java.
-
3:46
And we'll click Send.
-
3:49
Okay, we'll scroll to the response, and boom, there's a new one.
-
3:51
It's got id number 3, awesome.
-
3:53
So, post works, and now there's two, so let's switch our request to be a GET,
-
3:58
right?
-
3:58
So now we can list all of the courses that are available.
-
4:02
So let's go ahead and send that GET.
-
4:06
And that REST API Basics, that was the one that I tested with.
-
4:08
Here's the two that we added, and on yours there should just be two, right?
-
4:12
And see how there's these hard brackets here.
-
4:13
See how it's showing that there is an array of these different courses.
-
4:18
Awesome!
-
4:19
And we also added a row that lets you see different IDs.
-
4:23
So if I said 2, it should just return the 2.
-
4:25
Awesome, it does.
-
4:26
And if I say 3, does it get the third one?
-
4:29
Awesome.
-
4:30
And what happens if we do a different idea, like what if we do 42?
-
4:34
Null.
-
4:35
Hm?
-
4:35
That's not great, is it?
-
4:38
Because it says 200 OK but it's returning a null.
-
4:41
It really should return a 404 not found, right?
-
4:44
Let's table that for now.
-
4:45
So, we've done our automated testing, but this is manual testing, right?
-
4:50
So we've done automated testing together before where we test
-
4:53
isolated code in small units of work.
-
4:55
That's unit testing.
-
4:56
We can take another approach, which is called functional testing.
-
4:59
What this does is it allows you to test the system more in a way than an end user
-
5:03
using your system might do things.
-
5:05
So let's dive into functional testing, right after this quick break.
You need to sign up for Treehouse in order to download course files.
Sign up