So now we have to write a test for our handlers that take posts, and if we look at our setup underscore
test go, we see there are three.
There's one for search availability, one for search availability, Jassam and one down here for make
a reservation.
So let's set those up.
So the first thing we'll do is set up the test as part of our the tests variable.
So we're going to have a test for first of all, let's do post search availability.
And that's just the name of the test.
You URL is slash search availability bill it t spelled, right?
Yeah, I think so.
And it's going to be a post request and this will take a slice of post data and we need to specify the
necessary information for that.
So each entry in the post data consists of a key and a value.
The key is the name of the form element, the form input and the values, whatever is typed in there.
So we can just go.
Key and for post search availability, I think we have to start as the name and value, which is a date
put 20, 20 year one zero on any validate will do.
And we have another one for and Ellen will just make it a different date.
And finally, we have to specify what we want to get back HDB status.
OK, all right.
So there's one less copy this and paste it.
And the second one is for Jason Post search availability.
Jason, that's my name.
And the actual euro you URL is Jason at the end of that.
And the last one is for reservation.
And we'll call this make reservation post, but it doesn't take start and end, it takes first name,
which will make John and these correspond to the actual inputs on the form.
Last name will be Smith.
And then it takes an email address me at here dot com and it takes a phone number, which can be anything.
And we'll make it Phi Phi Phi Phi Phi Phi Phi Phi Phi Phi Phi.
All right.
So there are the actual test data entries.
Now we have to perform the tests and it's not that difficult.
So we do that here.
If method is not equal to get, then it must be equal to post.
So the first thing we need to do is to construct a kind of variable that is in the format that our test
server expects to receive things in.
And that's a variable that corresponds to what we're posting in the form, and we can do that really
easily.
So first of all, we'll create an empty variable in the form that is required by the method.
We're going to call on our test server and we'll call that values.
And that's going to be an empty euro values, which is a built in type part of the standard library
that holds information as a post request for a variable.
Now, we need to populate that with with actual our actual entries.
So we'll just range over our parameters, variable set in the current test that we're running.
And we do that like this for underscore because we don't care about the index and we'll just a variable
called X is equal to range.
Iida perhaps.
And as we go through that, we'll just populate values into that values variable, which we do use using
a built in method.
Values don't add and values add expects to things the key and the value extract key export value.
And now we have our values variable populated with everything necessary to make our post.
So once we've done that, we now have to call our test client again.
But we use a slightly different version.
This time we still get a response and we still potentially get an error, which will be nil if everything
works the way it's supposed to.
And we call it on our test variable, which is our test server.
And again, we call client, but this time we call post form.
And post form requires two things the URL you want to post to and the values you want to post so that
you are we want to post.
It was exactly the same as it was.
And yet it's the the euro for the test server, which is stored in euro plus idot euro euro.
We want to test and values.
It's just the values that we created just a moment ago.
So now we can just do exactly the same thing we can and we can actually copy what we did in our request
so we can copy this, paste it here and everything should work.
So what are we doing?
We get our response and our error.
If there's an error, fail the test.
If there's no error, keep going.
And we check our expected status code with the status code we actually get.
If they don't match, then we have an error.
So let's go back to our terminal window and clear the screen and try running this test.
I'm in the correct directory.
Yes, I am.
Go test dashboard and let's see if we got it right and it failed.
And one fail is for make reservation post expected 200, but got 400.
Well, what is HDB status?
400.
That's actually bad request.
And we there's one thing we didn't do that we actually have to do and it's pretty easy to fix.
So if we go back to our setup test, you remember when we set up our routes, we said use the no surfed
handler, our middleware and no surf actually checks for cross site request forgery.
And it expects to find a token called CSF token in the post request.
And we actually didn't populate that.
And I'm not going to at this point.
So one way to fix this is to simply comment that because I'm not testing my no serve handler, I've
already done that of my middleware and I can do it elsewhere if I want to.
It's really not critical for testing this particular method by itself.
So I'm going to comment that I go back to my term, my terminal window, clear the screen and run that
test again.
And this time it should pass.
And it does so there is one method of testing postal handlers that handle posts.
Now, obviously, we probably want to check for things other than just the correct status code coming
back.
So, for example, what if in this test someone somehow submits a value that's not a date for this this
post or for this post?
What if they don't put a valid email address in here and we're not checking for any of that right now
and we will will improve these tests as time goes on.
But before we can really do that, it would be useful to be able to connect to a database, store some
information in that database and actually handle that part of our application.
So we'll be coming back to these tests and adding to them and adding additional tests as time goes on.
So in the next section, we'll have a look at testing our render package and then we'll check coverage
and look at what we have to do to actually run our application right now.
Because if we try to run our application, I'll just show you right now.
We'll do it easier way later.
If I try to run this, I'm in my directory and I do what we've been doing right along.
Go run command web, start on, go.
It's not going to work and it's not going to work because we cannot run test go test files.
Now, we can get around that by saying go run command web and then may not go and command web middleware.
Don't go and command web rootstock.
Go and then it will run.
But there's a shorthand we can do this, we can write a little batch file or a shell script that'll
make it easier for us, and we'll do that in a couple of lectures.