So we need to fix the problem that we have with our tests in our handler package and let's run those
tests again and this time I'll run them right in the handlers directory.
So those are the only tests that I'm running go test.
And if I look at the beginning of this right with the very top, just after I ran the tests.
Which is right here, you'll see that it's calling telling us we have an error in handlers don't go
line 65.
So let's go look at handlers, don't go and see what's going on.
Handlers don't go line 65.
There are Salin, 65 is Helper's server error cannot get reservation from session.
And if you look at all of the errors that were generated there, you'll quickly discover that every
single one of them has to do with the fact that it can't get information from the session.
So clearly we have a problem here.
So let's go look at the actual tests themselves and see how they work so handlers don't test.
The one that we're dealing with is calling the reservation.
And that's this one, right?
Here, make reservation, so make reservation is the route that is failing and it's not passing any
post data.
It's looking for an HTTP status.
OK, and that's the URL that it's calling as a get request.
So if we actually look at the handler itself, again, this is the function that is causing us the problem.
And the problem is it can't get the session.
Well, these tests are set up using setup test, this file right here in the handler's folder.
Let's see if we're doing anything with the section there.
So it looks like we have our imports, we declare some variables and then we have this function get
roots.
And that's what's called right in the tests.
That's what's called before these tests run.
So if I look at the handlers test, what I'm doing is calling get rates, setting up a server and then
just running my tests.
So it's clearly it's not getting the session.
And why not?
Because at no point do we ever put anything in the session.
We need to have access to our request variable.
Well, we have a request.
There's no problem there, but we're not actually putting anything in that request to pull out in that
handler.
So we need some means of doing that.
So what I'm going to do right now is just comment all of this stuff out and get a comment about just
so these tests don't run at all because this is now an empty slice.
It will never run anything in test handlers.
And what I'll do instead is come down here and write a test specifically for the availability.
So how are we going to do that?
Create a new function func and it will be called Test Repository Reservation.
So that's the test I'm going to run.
But before I do that, I need to have access to a request variable that I can put a sessional variable
in before I call that handler.
So I'm going to go back to my setup tests and I'm going to make some changes here.
First of all, I'm not going to use just to get route's function.
Instead, we'll take advantage of that test main function that we used in our other tests.
So I'll create a function called Test Me, which is what it has to be called in this instance, because
this is part of the testing package available to us in the standard library.
And it takes one parameter, which I'll call em, and it's a type testing program and it doesn't return
anything.
And then I'll grab the things out of routes that really belong in that test main function anyway.
And that's everything up to and including the renderers.
So I'll cut them out of here, I'll paste them into here.
And then, of course, we need to have that OCR exit and run the tests just before you die, before
it exits running.
So they should be OS.
All right, so now we have a test main function, and it seems that I have an error here.
Oh, yes, Godbolt Register.
Let's just.
Yeah, it doesn't have the it didn't do the import for us, so I'll just force it to do the ghab dot
register that'll force it to reimported into our imports and the air should go away.
And it does.
So now I've functionally I've changed nothing.
I've just made it run the the set up in a test main function and we still have this get route's which
we may or may not use.
We'll find out in a little while.
So let's go back to our tests and see what we can do to make this test actually passed.
So what I want to do, first of all, is look at the reservation handler itself.
So let's go back to handlers.
It's expecting to pull a model's reservation out of the session and start in this variable.
OK, so we need to have a model reservation to put in the session.
Well, we know how to do that.
That's easy.
Let's just create a new variable called reservation.
It's going to be assigned the value of models DOT Reservation, and I'll put just enough information
in this variable to be able to actually deal with it in the reservation function in our handlers.
So we need a remedy and we'll just use one, which is the general's quarters.
And we also need the room, which is of type models room.
We need the name and the ID in that.
So that's going to be of type models that room.
And inside of that, we will populate its ID, which is one, and it's Ramnath, which is Generals'
quarters.
When you come there and I need a comma there, and this should be a colon, not a comma.
So now we have a reservation variable and I we need to put that in the session somehow.
So let's get a request.
We know how to get a request, request and potentially an error, which I'll ignore or assign the value
of a new request.
And the method is going to be get just as just like it is in the roots and the euro will be the same
one we used in the Ritz.
Make the reservation and we'll pass an antibody.
So that's not HIV test.
We're going to use HTTP request.
There we are.
So now we have a request and we need to put that in to the request.
We need to put our reservation variable as a special variable into the session of the request.
And of course, you do that using the context.
Now, how am I going to do that?
What I'm going to do is create a new function down here, which I'll just call get and it will take
a request, which is a pointer to an HTTP request, and it's going to return a context, context, context.
So what we're going to do is essentially get a context, put our sectional variable in it and then store
that in our request.
In order to do that, the context has to have a particular value in there, a key, something that is
that these our session package knows is the session.
Fortunately, that's pretty easy to do.
So what I'm going to do is in this function, I'm going to create a context and potentially an error
by calling session load, which is built right into our section session package.
And we've done this before session that load.
It requires a context which will be from the request that we've just passed this request context and
it requires a header.
So what we're going to do is get a header from that context, from that request request, that header
doget and the header key is exactly like this and it uppercase and lowercase matter in this, in this.
I think we're going to do it that way just to be absolutely safe.
We'll check for an error.
If error is not equal to nil, we'll just log the error.
So log error log print line the error.
That's all we'll do there.
And then we'll return the context, but this context, we actually know about the header excession,
which we need in order to read, to read from or write to the session.
So back in this function test repository reservation, we have our request now.
We need to get our context into that request so we'll get a context like this is a sign the value of
get context and will pass at the request we just made.
So now we have a context that we can actually add to our request.
And we do that the same way we did the last time we wrote this sort of test.
Request is equal to request with context the context we just created.
So now we have a request that knows about the extra session header, so now we can do something with
it.
So what's the next step?
The next step is a little different, something you haven't seen yet.
We're going to take advantage of something that's built into the testing package of goal called a new
recorder, an HIV test, new recorder.
So what I'm going to do is create a new variable called R for request recorder that's going to be assigned
the value of HTP test Dort new recorder.
And this is exactly what it sounds like.
We can actually look at the description of it.
A new recorder is it returns an initialized response recorder.
So this is basically simulating what we get from the request response lifecycle.
When someone fires up a Web browser, hits our Web site, gets to a handler, passes that request,
gets a response writer and the response writer writes the response to the Web browser.
This fakes that entire process or the part of it that we need for a recorder.
So we have a response recorder here.
We're not using it yet.
So we have to what we're going to do now is put our reservation in the session.
Session put.
Just like we do in a in our actual handlers, but instead of using the context from our request, we're
going to use the context we've built X name and reservation and pass in our reservation.
So now we've added that reservation variable to find up here with all of these values to our context,
we've put it in the session in effect.
And what we want to do now is actually call our reservation function, which is a handler, while we
can't call it directly unless we do something to it.
I'm going to take that handler reservation and turn it into a handler function so I can do that really
easily.
So I'm going to say handler, which is going to be just a variable that is assigned the value of our
reservation.
But I'm going to cast it to an HDTV handler function, htp dot handler func and I'm just going to put
in repo dot reservation.
So what I've done now is take our reservation variable or function, which is right here.
It takes a response writer and a pointer to a request, and I've turned it into a function that I can
call directly.
So let's go back to our handlers test and actually call that function.
And we do that simply by saying handler dot serve HTP.
And what I'm going to put in there for its function, for its request and response are our request recorder
are sort of response recorder and request that we've built.
So when I do that, I can actually call that directly.
I'm actually calling Serve HTP on this, just as I call it, on all of our roots in the main function
of our application, simply because I've built a request, put that sessional variable reservation into
the session, cast my reservation handler into a handler func.
Now it has served HTTP because that is one of the things that you this satisfies all the the functions
necessary to become something that actually acts as a Web server.
So I don't even need my roots for this test.
So I don't never call get roots at all.
Instead I'm building it manually, calling this directly by calling handler serve htp, passing it my
response recorder and passing it my request, which has the necessary sessional information in it.
So at this point I can actually see if this test passed by saying if my are my response recorder, if
its code is equal to http dot status, ok.
Then it's past if it's not true, so this should be a not equal, if it's not equal to true, then I
fail my test teig error.
And I'll use Şeref in this case, and I put my string in here, reservation handler returned.
Wrong response, quote.
Got percent, Dean wanted percent, Dean, and then I replaced those placeholders with got our code
wanted HTP status, OK?
OK, so this will probably work.
Let's find out.
Let's go back to our terminal, clear the screen and run the tests, go test.
OK, it passed, let's make sure it's actually passing, let's change this back to an equal, this should
fail.
Now let's clear our screen, run the test again.
And it failed, so now we have a means of putting something in the session and pulling something out
of our session.
And this is a good start.
So what we'll do in the next lecture or two is begin to create tests for all of the all of the routes
in our handlers that use the session.
And then we'll improve those tests by changing our test database repo to respond differently based upon
the kind of information we pass it in our tests.
And we'll do that in the next few lectures.