So while I was writing the last remaining tests for our handlers package, I realized that there is
one particular function availability, Jason, where you may not, in fact, have enough information
to complete this one.
So I'll get you started.
So first of all, when you look at the function itself, you'll notice that I don't actually pass the
form and I need to pass the form in order to write a test.
So what I'm going to do is I just wrote this beforehand and I'm just going to paste it in and show you
what I'm doing here, what I'm doing, first of all, saying calling pass form, which I told you is
good practice and it's good practice because when you're testing things, for example, you actually
have to have this or you can't test it.
The function worked just fine without it in the actual application.
But we need to write test, so I'm going to pass it.
And all I do here is pass the form and then I check for an error.
And if there's an error, I'm going to return, not a server error or anything like that.
I'm actually going to return Jason and Jason that says it's false and populates the message.
So I just construct a JSON response which is defined right in this application right here, put in the
appropriate values and write that Jason back to the browser and then I keep going.
And the other thing that I want to do here is I'm going to copy this little bit where I'm writing JSON
as when I hit the database.
I actually need to ensure that I'm not writing a server error because that's just going to, you know,
cause grief for us.
Instead, I'll return again JSON.
But this time I'll change the message to.
Error connecting to database, which is sufficient for my purposes right now.
OK, so that's the change I made to the application itself.
And then down here, I actually removed the error check.
And that's on this one, because when you think about it, I'd get into a situation where I'm trying
to create JSON and have an error.
So I try to create JSON to send the fact that I have an error and it just is a never ending chain there.
So all I've done is to look at the actual code and here I am manually constructing this JSON response.
So I know it's always going to be correct.
There is no situation where this will ever throw an error.
So all I did here was remove the error.
And I added a comment to this, which you'll see in the source code if you choose to download it from
the next lecture.
So now let's go back and get started writing this test, and I will just start writing it for you and
let you finish it on your own.
So I am going to put in a new function here, which I will call func test repository.
And the one I want is availability JSON right there.
So the very first thing I'm going to do is identify my first case.
So what's my first case to first case is rooms are not available.
OK, so all I need to do is create a request body and my request body will be named Recke Body.
It's assign the value of some start date start equals 2050 and fire in the future 01 01.
And then I will, as I've done before, append to that request body equals format, not a sprint F and
then a placeholder that's a string and an ampersand and a placeholder that's a string.
And my two values, the first one being request body, because I'm appending to this I'm actually building
on this string and then I want to put an end equals 250 zero one zero two and then I need a remedy.
So I will now append to that room ID and I will make that one.
OK, so I've got my request body now.
I need to actually create the request, so create a request.
And we do that just like we did before, request and ignore the error or assign the value of HTTP request.
The method is post because this is a handler that takes a post.
The URL is search availability, Jason, and we put in the body of the request using strings dot new
reader and we put in our string called record body.
So now I have my request.
What do I do with it next?
Well, I get the contacts with the session, which I'm not using, but I'm going to I might at some
point in the future, who knows.
So get context with session and I put in I get it this way.
Context is a sign the value of get context and my request.
And then I append that request to put that that context back into my request.
Request equals request with context.
And I put in my context.
All right, what do I do next?
Set the request header.
And if you recall, we do that by saying request dot header, dot set and we want to set oops, I don't
want to do their set.
Content type, and that is of type X dash w w w dash form, dash Eurail encoded.
So I've set the header now.
Next step, make our handler a handler function, make handler handler function.
And we do that very easily by saying handler is assigned the value of handler htp handler func and the
handler we want to call, which is repo dot availability.
Try that again.
Availability Jason.
There it is.
Now we have our handler and we make the request.
To our handler, so I say handler serve HTP and we want a response recorder, which I haven't made yet,
so I'll have to do that and a request.
So let's get our response recorder, get a response recorder.
Are is assigned the value of HTP test.
New recorder.
All right, so now we've made the request to our handler and here's the part where you really don't
ah, you're not sure how to handle this.
You may not be sure how to handle this.
We have covered Jason way back earlier in the course when we were going through the basics of Goaland.
But I need to get what the server is sending back to me and convert that into Jason.
So what I'm going to do is create a new variable, Varg, which is Jason response, because that's the
response.
I'm getting it back in and I have access to that because I'm in the handler's package and then I need
to get what's being handed back to me by the server and put that into Jason.
So I will say error is assigned the value of just like we did at the beginning of the course.
Jason Marshall.
And my data in this case is the body that the response is sending back to me.
So I need to convert that to a slice of bytes because that's what this this particular function needs.
And all I do is call our body, which is the body of the response being sent to us by the test server
and convert that to a string.
And then I'm putting all of that into my variable that I just declared J and now I have let's see if
I got this right.
I did.
I got a check for an error.
So if error is not equal to nil, then I want to fail the test.
Teig error failed to pass, Jason, so that should get you far enough that you can now complete the
actual test you need to do, because you'll have the response from the server in this structure, in
this struct J, which is of type Jason response.
And you can actually look at the responses you're getting back from the server.
So that should be enough to let you finish writing this test.
The rest of them, you have everything you need at your disposal.
So good luck.