1
00:00:00,450 --> 00:00:06,210
So now we have to write a test for our handlers that take posts, and if we look at our setup underscore

2
00:00:06,210 --> 00:00:08,370
test go, we see there are three.

3
00:00:08,370 --> 00:00:14,530
There's one for search availability, one for search availability, Jassam and one down here for make

4
00:00:14,530 --> 00:00:15,290
a reservation.

5
00:00:15,330 --> 00:00:16,560
So let's set those up.

6
00:00:17,190 --> 00:00:21,720
So the first thing we'll do is set up the test as part of our the tests variable.

7
00:00:22,170 --> 00:00:28,980
So we're going to have a test for first of all, let's do post search availability.

8
00:00:29,550 --> 00:00:30,960
And that's just the name of the test.

9
00:00:31,110 --> 00:00:39,450
You URL is slash search availability bill it t spelled, right?

10
00:00:39,480 --> 00:00:40,110
Yeah, I think so.

11
00:00:40,770 --> 00:00:49,560
And it's going to be a post request and this will take a slice of post data and we need to specify the

12
00:00:49,560 --> 00:00:51,110
necessary information for that.

13
00:00:51,480 --> 00:00:56,360
So each entry in the post data consists of a key and a value.

14
00:00:56,370 --> 00:01:02,520
The key is the name of the form element, the form input and the values, whatever is typed in there.

15
00:01:02,520 --> 00:01:03,750
So we can just go.

16
00:01:05,280 --> 00:01:15,000
Key and for post search availability, I think we have to start as the name and value, which is a date

17
00:01:15,000 --> 00:01:18,300
put 20, 20 year one zero on any validate will do.

18
00:01:19,440 --> 00:01:24,410
And we have another one for and Ellen will just make it a different date.

19
00:01:25,320 --> 00:01:30,810
And finally, we have to specify what we want to get back HDB status.

20
00:01:30,820 --> 00:01:32,660
OK, all right.

21
00:01:32,670 --> 00:01:37,140
So there's one less copy this and paste it.

22
00:01:37,860 --> 00:01:42,110
And the second one is for Jason Post search availability.

23
00:01:42,120 --> 00:01:43,560
Jason, that's my name.

24
00:01:43,560 --> 00:01:46,530
And the actual euro you URL is Jason at the end of that.

25
00:01:47,430 --> 00:01:49,560
And the last one is for reservation.

26
00:01:54,680 --> 00:02:02,840
And we'll call this make reservation post, but it doesn't take start and end, it takes first name,

27
00:02:03,680 --> 00:02:08,840
which will make John and these correspond to the actual inputs on the form.

28
00:02:09,350 --> 00:02:10,790
Last name will be Smith.

29
00:02:15,890 --> 00:02:23,180
And then it takes an email address me at here dot com and it takes a phone number, which can be anything.

30
00:02:25,370 --> 00:02:29,520
And we'll make it Phi Phi Phi Phi Phi Phi Phi Phi Phi Phi Phi.

31
00:02:30,080 --> 00:02:30,520
All right.

32
00:02:30,710 --> 00:02:33,610
So there are the actual test data entries.

33
00:02:33,980 --> 00:02:37,950
Now we have to perform the tests and it's not that difficult.

34
00:02:37,970 --> 00:02:39,020
So we do that here.

35
00:02:39,380 --> 00:02:42,710
If method is not equal to get, then it must be equal to post.

36
00:02:42,710 --> 00:02:51,830
So the first thing we need to do is to construct a kind of variable that is in the format that our test

37
00:02:51,830 --> 00:02:53,490
server expects to receive things in.

38
00:02:53,510 --> 00:02:58,010
And that's a variable that corresponds to what we're posting in the form, and we can do that really

39
00:02:58,010 --> 00:02:58,340
easily.

40
00:02:59,000 --> 00:03:03,890
So first of all, we'll create an empty variable in the form that is required by the method.

41
00:03:03,900 --> 00:03:06,860
We're going to call on our test server and we'll call that values.

42
00:03:07,370 --> 00:03:14,390
And that's going to be an empty euro values, which is a built in type part of the standard library

43
00:03:14,840 --> 00:03:19,070
that holds information as a post request for a variable.

44
00:03:19,400 --> 00:03:22,600
Now, we need to populate that with with actual our actual entries.

45
00:03:22,610 --> 00:03:30,050
So we'll just range over our parameters, variable set in the current test that we're running.

46
00:03:30,050 --> 00:03:34,820
And we do that like this for underscore because we don't care about the index and we'll just a variable

47
00:03:34,820 --> 00:03:37,130
called X is equal to range.

48
00:03:37,700 --> 00:03:38,600
Iida perhaps.

49
00:03:40,640 --> 00:03:47,060
And as we go through that, we'll just populate values into that values variable, which we do use using

50
00:03:47,060 --> 00:03:47,720
a built in method.

51
00:03:47,720 --> 00:03:56,170
Values don't add and values add expects to things the key and the value extract key export value.

52
00:03:57,260 --> 00:04:01,460
And now we have our values variable populated with everything necessary to make our post.

53
00:04:02,390 --> 00:04:06,370
So once we've done that, we now have to call our test client again.

54
00:04:06,380 --> 00:04:07,910
But we use a slightly different version.

55
00:04:07,910 --> 00:04:13,130
This time we still get a response and we still potentially get an error, which will be nil if everything

56
00:04:13,130 --> 00:04:14,270
works the way it's supposed to.

57
00:04:14,840 --> 00:04:18,130
And we call it on our test variable, which is our test server.

58
00:04:18,620 --> 00:04:23,120
And again, we call client, but this time we call post form.

59
00:04:23,840 --> 00:04:30,020
And post form requires two things the URL you want to post to and the values you want to post so that

60
00:04:30,020 --> 00:04:31,100
you are we want to post.

61
00:04:31,100 --> 00:04:32,570
It was exactly the same as it was.

62
00:04:32,570 --> 00:04:42,370
And yet it's the the euro for the test server, which is stored in euro plus idot euro euro.

63
00:04:42,380 --> 00:04:44,240
We want to test and values.

64
00:04:44,270 --> 00:04:46,480
It's just the values that we created just a moment ago.

65
00:04:47,360 --> 00:04:52,430
So now we can just do exactly the same thing we can and we can actually copy what we did in our request

66
00:04:52,430 --> 00:04:58,430
so we can copy this, paste it here and everything should work.

67
00:04:58,440 --> 00:04:59,270
So what are we doing?

68
00:04:59,570 --> 00:05:01,190
We get our response and our error.

69
00:05:01,190 --> 00:05:02,720
If there's an error, fail the test.

70
00:05:02,720 --> 00:05:04,160
If there's no error, keep going.

71
00:05:04,430 --> 00:05:08,930
And we check our expected status code with the status code we actually get.

72
00:05:09,020 --> 00:05:10,910
If they don't match, then we have an error.

73
00:05:11,600 --> 00:05:16,760
So let's go back to our terminal window and clear the screen and try running this test.

74
00:05:17,000 --> 00:05:18,090
I'm in the correct directory.

75
00:05:18,090 --> 00:05:18,620
Yes, I am.

76
00:05:18,620 --> 00:05:23,330
Go test dashboard and let's see if we got it right and it failed.

77
00:05:23,330 --> 00:05:29,530
And one fail is for make reservation post expected 200, but got 400.

78
00:05:29,570 --> 00:05:31,160
Well, what is HDB status?

79
00:05:31,160 --> 00:05:31,730
400.

80
00:05:32,150 --> 00:05:33,980
That's actually bad request.

81
00:05:34,520 --> 00:05:38,510
And we there's one thing we didn't do that we actually have to do and it's pretty easy to fix.

82
00:05:38,780 --> 00:05:46,120
So if we go back to our setup test, you remember when we set up our routes, we said use the no surfed

83
00:05:46,130 --> 00:05:52,970
handler, our middleware and no surf actually checks for cross site request forgery.

84
00:05:53,210 --> 00:05:58,310
And it expects to find a token called CSF token in the post request.

85
00:05:58,610 --> 00:06:00,590
And we actually didn't populate that.

86
00:06:00,950 --> 00:06:02,900
And I'm not going to at this point.

87
00:06:02,910 --> 00:06:08,570
So one way to fix this is to simply comment that because I'm not testing my no serve handler, I've

88
00:06:08,570 --> 00:06:12,200
already done that of my middleware and I can do it elsewhere if I want to.

89
00:06:12,200 --> 00:06:16,310
It's really not critical for testing this particular method by itself.

90
00:06:16,640 --> 00:06:22,880
So I'm going to comment that I go back to my term, my terminal window, clear the screen and run that

91
00:06:22,880 --> 00:06:23,480
test again.

92
00:06:23,480 --> 00:06:24,680
And this time it should pass.

93
00:06:26,260 --> 00:06:32,590
And it does so there is one method of testing postal handlers that handle posts.

94
00:06:32,620 --> 00:06:38,620
Now, obviously, we probably want to check for things other than just the correct status code coming

95
00:06:38,620 --> 00:06:38,920
back.

96
00:06:38,930 --> 00:06:47,020
So, for example, what if in this test someone somehow submits a value that's not a date for this this

97
00:06:47,020 --> 00:06:48,780
post or for this post?

98
00:06:49,240 --> 00:06:53,770
What if they don't put a valid email address in here and we're not checking for any of that right now

99
00:06:53,770 --> 00:06:56,590
and we will will improve these tests as time goes on.

100
00:06:57,220 --> 00:07:01,660
But before we can really do that, it would be useful to be able to connect to a database, store some

101
00:07:01,660 --> 00:07:05,910
information in that database and actually handle that part of our application.

102
00:07:05,920 --> 00:07:11,290
So we'll be coming back to these tests and adding to them and adding additional tests as time goes on.

103
00:07:12,400 --> 00:07:18,490
So in the next section, we'll have a look at testing our render package and then we'll check coverage

104
00:07:18,490 --> 00:07:22,120
and look at what we have to do to actually run our application right now.

105
00:07:22,120 --> 00:07:25,210
Because if we try to run our application, I'll just show you right now.

106
00:07:25,210 --> 00:07:26,350
We'll do it easier way later.

107
00:07:26,740 --> 00:07:30,520
If I try to run this, I'm in my directory and I do what we've been doing right along.

108
00:07:31,000 --> 00:07:34,070
Go run command web, start on, go.

109
00:07:34,120 --> 00:07:39,430
It's not going to work and it's not going to work because we cannot run test go test files.

110
00:07:40,000 --> 00:07:51,730
Now, we can get around that by saying go run command web and then may not go and command web middleware.

111
00:07:52,210 --> 00:07:57,820
Don't go and command web rootstock.

112
00:07:57,820 --> 00:07:59,140
Go and then it will run.

113
00:08:01,400 --> 00:08:06,770
But there's a shorthand we can do this, we can write a little batch file or a shell script that'll

114
00:08:06,770 --> 00:08:09,140
make it easier for us, and we'll do that in a couple of lectures.
