1
00:00:01,200 --> 00:00:06,210
So it's time to test our handlers and testing handlers can always be a little tricky, but it's not

2
00:00:06,210 --> 00:00:08,370
that bad and we'll go through the entire process.

3
00:00:09,150 --> 00:00:11,090
So I'm looking at my handlers right now.

4
00:00:11,100 --> 00:00:16,110
And when you look at these, you will realize pretty quickly that we need a fair bit of information

5
00:00:16,110 --> 00:00:19,020
before we can ever test a given handler.

6
00:00:19,050 --> 00:00:21,240
For example, I'm looking at the home handler.

7
00:00:21,540 --> 00:00:23,130
I'm going to need a response writer.

8
00:00:23,160 --> 00:00:24,450
I'm going to need a request.

9
00:00:24,720 --> 00:00:30,990
I'm going to have to have access to the session because I'm putting something in here and, you know,

10
00:00:30,990 --> 00:00:32,490
I'm sure they're not going to be using that later.

11
00:00:32,520 --> 00:00:36,680
I'll take that out of the home handler at some point, but I'm definitely using it in other places.

12
00:00:36,930 --> 00:00:43,820
So, for example, when I do a post request, I store the reservation in the session.

13
00:00:44,190 --> 00:00:47,430
So there's a fair bit of things that I need to actually have set up.

14
00:00:47,880 --> 00:00:50,430
And what I'm going to do, of course, is create two files.

15
00:00:50,430 --> 00:00:52,260
One of them is where I'm going to set things up.

16
00:00:52,260 --> 00:00:57,390
So when the handlers folder, I'll create a set up, underscore Testori, go or create that.

17
00:00:57,780 --> 00:01:00,090
That's where we're going to set up our testing environment.

18
00:01:00,450 --> 00:01:05,310
And then I need a place for the actual test themselves, which of course needs to be called handlers.

19
00:01:05,310 --> 00:01:07,110
Underscore Test Dutko.

20
00:01:07,110 --> 00:01:12,840
And again, just to remind you, if it doesn't end in underscore test go, it's not considered a test.

21
00:01:13,140 --> 00:01:15,810
So I've created both of those files.

22
00:01:15,840 --> 00:01:20,080
And let's start with the setup and let's think about what we need to have in here.

23
00:01:20,100 --> 00:01:24,230
Well, it's pretty much the same sort of thing that we have in our main function.

24
00:01:24,750 --> 00:01:32,400
So on Mango, I have this run function and I definitely want most of this stuff.

25
00:01:32,400 --> 00:01:33,930
So I'm going to copy all of this.

26
00:01:35,190 --> 00:01:37,170
I shall copy all of it right down to here.

27
00:01:37,680 --> 00:01:42,370
And I'll go to setup test and I'll create a function.

28
00:01:42,420 --> 00:01:47,160
Now, I could use the test main function that we talked about last time, and we'll do that in the next

29
00:01:47,160 --> 00:01:49,680
step when we get to testing or render functions.

30
00:01:50,070 --> 00:01:53,270
But for right now, I'll just demonstrate that you can use any function you want to.

31
00:01:53,280 --> 00:01:55,740
So I'll create a function, I'll call it.

32
00:01:56,040 --> 00:01:56,820
What shall I call it?

33
00:01:57,210 --> 00:01:58,650
I'll call it func.

34
00:02:02,920 --> 00:02:07,570
Get roots, because I know I'm going to have to have roots at some point, otherwise I can't call the

35
00:02:07,570 --> 00:02:08,260
handlers at all.

36
00:02:08,920 --> 00:02:14,350
And since it's, you know, going to be giving us roots, it'll hand back exactly what our roots application

37
00:02:14,350 --> 00:02:14,560
does.

38
00:02:14,580 --> 00:02:18,600
So if I look at Ritsuko go, it returns and it should be handled.

39
00:02:18,610 --> 00:02:19,570
So I'm going to need that.

40
00:02:19,570 --> 00:02:22,780
So I know I'm going to be returning an HSDPA handler at some point.

41
00:02:24,670 --> 00:02:25,810
And a better spell funk.

42
00:02:25,810 --> 00:02:26,030
Right.

43
00:02:27,550 --> 00:02:27,940
All right.

44
00:02:28,630 --> 00:02:31,330
And then I'll just paste in what I got from my main function.

45
00:02:31,360 --> 00:02:32,970
Now, we have some errors here right away.

46
00:02:33,040 --> 00:02:34,330
I don't have an app variable.

47
00:02:34,340 --> 00:02:35,620
I don't have a session variable.

48
00:02:35,860 --> 00:02:37,380
So we can create those.

49
00:02:37,390 --> 00:02:38,210
That's no problem.

50
00:02:38,410 --> 00:02:46,210
Let's create our app variable, which will be var app config, app config that will hold our application

51
00:02:46,210 --> 00:02:49,540
configuration and let's create a special variable too and we'll call that session.

52
00:02:49,780 --> 00:02:55,510
And that's a pointer to export session manager because we're using our Alex Edwards ESCs package and

53
00:02:55,510 --> 00:02:58,570
that's what this session variable is for that given package.

54
00:02:59,740 --> 00:03:02,470
So great that that fixed up most of the errors.

55
00:03:02,470 --> 00:03:03,700
But we still have one here.

56
00:03:04,120 --> 00:03:06,370
Log fatele cannot create template cache.

57
00:03:06,400 --> 00:03:09,790
Well, we're going to have to change that template cache in a minute, but we just won't return it here.

58
00:03:10,840 --> 00:03:13,780
And down here we have Handler's New Republic.

59
00:03:13,840 --> 00:03:15,190
We're in the handler's package.

60
00:03:15,370 --> 00:03:19,710
So I don't need to actually specify that the same thing here for new handlers.

61
00:03:20,980 --> 00:03:26,100
OK, and then we have render new templates in app and then we actually need our roots.

62
00:03:26,110 --> 00:03:34,250
So let's open up our Ritvo and we'll copy all of this right down to here.

63
00:03:34,300 --> 00:03:34,910
Copy that.

64
00:03:35,740 --> 00:03:39,060
Go back to our setup test and we'll pasted in.

65
00:03:39,520 --> 00:03:40,990
And again, we have some errors.

66
00:03:41,180 --> 00:03:45,280
Now the errors are once again because we're actually in the handler's package.

67
00:03:45,610 --> 00:03:46,780
So I don't have to specify.

68
00:03:46,870 --> 00:03:48,700
I don't have to specify the final package.

69
00:03:48,700 --> 00:03:54,130
So what I can just do is edit, find, search and replace.

70
00:03:55,300 --> 00:04:04,960
And I will look for handlers thought repo and just change it to repo replace old and that fixes those

71
00:04:04,960 --> 00:04:05,320
errors.

72
00:04:06,040 --> 00:04:07,510
Now we also need our middleware.

73
00:04:07,660 --> 00:04:10,240
Now our middleware is stored in the main package.

74
00:04:10,240 --> 00:04:13,630
That means we can't import because you can't import your main package.

75
00:04:14,170 --> 00:04:18,820
But what we can do is open the middleware file and just copy those

76
00:04:22,090 --> 00:04:24,950
and close this and come back here and we'll just paste.

77
00:04:25,090 --> 00:04:25,840
I'll put them down here.

78
00:04:25,840 --> 00:04:30,930
It doesn't matter where they are, but I'll paste them in here and suddenly all of our errors go away.

79
00:04:31,480 --> 00:04:34,150
So this probably set up our environment.

80
00:04:34,150 --> 00:04:36,160
Let's just check everything and make sure we've got it right.

81
00:04:36,550 --> 00:04:42,190
So we come in here and we say, get roots, we register the reservation, we're going to store the section.

82
00:04:42,190 --> 00:04:45,220
We have to have that because one of our handlers does precisely that.

83
00:04:46,150 --> 00:04:47,920
We set in production defaults.

84
00:04:47,920 --> 00:04:48,670
That's fine.

85
00:04:48,910 --> 00:04:55,150
We set up our session exactly as we do in our our main function.

86
00:04:55,720 --> 00:04:57,790
Then we store a session in the application.

87
00:04:57,820 --> 00:04:58,380
That's good.

88
00:04:58,690 --> 00:05:02,110
Next, we have render create template cache.

89
00:05:02,710 --> 00:05:06,310
So let's look at the create template cache function that's in the render package.

90
00:05:07,630 --> 00:05:10,870
So this looks like it should work, but it won't.

91
00:05:10,870 --> 00:05:14,350
And the reason why it won't work is not immediately obvious.

92
00:05:14,380 --> 00:05:20,380
And it's right here when I load the templates like the page templates, for example, I'm hard coding

93
00:05:20,380 --> 00:05:25,960
the path to the templates folder and that's fine when we start our application from the root level of

94
00:05:25,960 --> 00:05:28,420
our directory, as we've been doing all along.

95
00:05:29,170 --> 00:05:32,530
But when we run our tests, our handlers underscore test.

96
00:05:32,530 --> 00:05:36,880
When we write those tests and run it, we're not actually running the application from the root level

97
00:05:36,880 --> 00:05:37,510
of our folder.

98
00:05:37,870 --> 00:05:44,200
We're running it from instead the handler's folder, which is inside the internal folder, which is

99
00:05:44,200 --> 00:05:46,360
inside the root level of our application.

100
00:05:47,260 --> 00:05:48,340
So we need to change that.

101
00:05:48,610 --> 00:05:50,740
And it's really easy to do what I'm going to do.

102
00:05:50,740 --> 00:05:56,440
It's go to the top of this file and create a new variable, which I will call path to templates, and

103
00:05:56,440 --> 00:06:00,190
I will initialize that with the default value of doggy templates.

104
00:06:00,910 --> 00:06:09,250
Then I'll go back down to my create template cache and I'll just make this a format as print F and get

105
00:06:09,250 --> 00:06:16,390
rid of the DOT templates, put in a string placeholder and then put in our path the templates.

106
00:06:17,320 --> 00:06:21,370
Now when this application runs, I can actually set that variable.

107
00:06:21,400 --> 00:06:25,150
Now this isn't going to be terribly useful right now, but it will be useful when we get to the render

108
00:06:25,150 --> 00:06:25,490
parts.

109
00:06:25,520 --> 00:06:26,680
We may as well do it right now.

110
00:06:27,550 --> 00:06:33,970
I can actually substitute whatever I need at the first part of this path to the templates using this

111
00:06:33,970 --> 00:06:34,360
variable.

112
00:06:34,450 --> 00:06:35,700
So do the same thing here.

113
00:06:35,920 --> 00:06:46,930
This will be a placeholder for S and then format as print F and put in our variable path the templates

114
00:06:47,860 --> 00:06:48,910
and the same thing here.

115
00:06:49,510 --> 00:06:49,990
This will be A.

116
00:06:50,410 --> 00:06:59,110
S and we'll put in format as print f path to templates and closer parentheses.

117
00:06:59,320 --> 00:06:59,710
All right.

118
00:06:59,710 --> 00:07:00,070
So that.

119
00:07:00,070 --> 00:07:06,220
We solve that problem, so that's something that's going to be helpful in the next section when we start

120
00:07:06,220 --> 00:07:10,000
testing our vendor handlers, so we may as well do it right now because that hard coating is going to

121
00:07:10,000 --> 00:07:10,900
be a bit of an issue.

122
00:07:11,440 --> 00:07:16,300
But in our setup tests, I actually don't want to call this template cash directly.

123
00:07:17,590 --> 00:07:24,220
What I'm going to do instead is copy that function, the entire thing, just so I have complete control

124
00:07:24,220 --> 00:07:25,290
over my testing environment.

125
00:07:25,300 --> 00:07:26,410
And there are many ways to do this.

126
00:07:26,410 --> 00:07:28,900
But this one works and it's pretty straightforward.

127
00:07:29,530 --> 00:07:32,650
And I'll put another function in here, a pastebin.

128
00:07:33,220 --> 00:07:38,380
But instead of calling it create template Carse, I'm going to call it create a test template cache.

129
00:07:40,510 --> 00:07:44,620
And as usual, the idee can't find its template.

130
00:07:44,620 --> 00:07:47,560
So I have to import that and we need to create a path to templates variable.

131
00:07:47,560 --> 00:07:48,370
So we'll do that up here.

132
00:07:48,880 --> 00:07:54,650
First of all, let's tell Goaland that we're using the HTML template and that'll solve that import problem.

133
00:07:55,270 --> 00:07:55,670
Good.

134
00:07:56,710 --> 00:07:57,680
So those are gone now.

135
00:07:57,700 --> 00:07:58,020
Yeah.

136
00:07:58,060 --> 00:08:00,220
All I have to do is create that path, the templates.

137
00:08:00,220 --> 00:08:11,350
So her path to templates is going to be equal to DOT and then up one level, which gets us to the internal

138
00:08:11,350 --> 00:08:15,910
folder, then up another level, which gets us to the root level of our application, which is where

139
00:08:15,910 --> 00:08:18,100
we want to be, and then the templates folder.

140
00:08:18,910 --> 00:08:19,380
All right.

141
00:08:19,390 --> 00:08:21,540
So we have one more error here functions.

142
00:08:22,330 --> 00:08:27,310
So let's go back to our render package right here and find out what functions is.

143
00:08:27,310 --> 00:08:35,080
And this is a variable that holds all of the functions that we want to put into our make available to

144
00:08:35,080 --> 00:08:36,650
our our goaland templates.

145
00:08:37,300 --> 00:08:42,190
So let's go back to set up test and just create that variable and we can leave it empty and it should

146
00:08:42,190 --> 00:08:43,640
solve our problems.

147
00:08:44,170 --> 00:08:44,530
Good.

148
00:08:45,160 --> 00:08:47,620
So there it looks like we've got things set up.

149
00:08:47,650 --> 00:08:54,970
Let's make sure when I call the get root functions, it registers the models reservation into the sessions.

150
00:08:54,970 --> 00:08:56,560
So we know that we can use that in the session.

151
00:08:57,070 --> 00:08:59,470
We set our in production default, which is fine.

152
00:09:00,070 --> 00:09:02,490
We create our session, which is great.

153
00:09:02,830 --> 00:09:09,400
Then we store a session in the variable, then we create our template cache right here and we say app

154
00:09:09,400 --> 00:09:14,950
template cache equals T.C. And then we have app use cash equal to false.

155
00:09:15,670 --> 00:09:16,880
And that could be a bit of a problem.

156
00:09:17,650 --> 00:09:19,150
Let's think about that for a minute.

157
00:09:19,160 --> 00:09:25,960
Why wouldn't that be a problem if we don't if we have used cash that defaults, it's going to rebuild

158
00:09:25,960 --> 00:09:27,970
the page on every request.

159
00:09:28,270 --> 00:09:32,890
But when it actually rebuilds the page in render, let's see this.

160
00:09:32,890 --> 00:09:36,310
If app use cash is to true, then get it from the template cache.

161
00:09:36,310 --> 00:09:38,380
Otherwise create template cache.

162
00:09:38,650 --> 00:09:39,550
And that's a problem.

163
00:09:39,550 --> 00:09:44,800
That's a problem because when it calls this function, it's going to be using this as the past of the

164
00:09:44,800 --> 00:09:45,350
templates.

165
00:09:45,370 --> 00:09:50,200
So let's go back to our setup test and make app use cash equal to true.

166
00:09:50,950 --> 00:09:52,720
And that should work for us.

167
00:09:53,410 --> 00:09:58,030
OK, so we've got this set up and let's write just one test just to get things out of the way and see

168
00:09:58,030 --> 00:09:58,960
if we can make this work.

169
00:09:58,960 --> 00:10:02,370
And what I'm going to do is think about how I'm going to do this.

170
00:10:02,380 --> 00:10:06,700
Well, first of all, I have a whole bunch of pages I want to test or a whole bunch of handlers I want

171
00:10:06,700 --> 00:10:07,090
to test.

172
00:10:07,480 --> 00:10:12,790
I can write each test individually, but it seems to me it makes a lot more sense to run table tests.

173
00:10:13,660 --> 00:10:18,310
And when I call a given handler, I need to know certain things.

174
00:10:18,310 --> 00:10:24,010
First of all, what's the path to it which matches the things in our resolve and our routes file in

175
00:10:24,010 --> 00:10:26,830
this case are in the the set up underscore test.

176
00:10:27,190 --> 00:10:31,900
So I need to know that for some of the routes I'm going to be posting information.

177
00:10:31,900 --> 00:10:38,440
For example, when I post to make Dasch reservation, I'm filling out a form and it has first name,

178
00:10:38,440 --> 00:10:41,860
last name, phone, email, and eventually it'll have more information.

179
00:10:42,070 --> 00:10:45,280
So I'm going to need some method of of holding post data.

180
00:10:45,880 --> 00:10:46,290
All right.

181
00:10:46,300 --> 00:10:47,830
So let's think about what we're going to do here.

182
00:10:47,860 --> 00:10:50,290
What I'm going to do, first of all, is create a type.

183
00:10:50,470 --> 00:10:55,930
And this type is going to be something that holds whatever we're posting to a page.

184
00:10:55,930 --> 00:10:58,720
So I'll call it post data and it's just going to be a struct.

185
00:10:59,590 --> 00:11:03,040
And inside of that, I'm going to have two things, just like we have in our forms.

186
00:11:03,040 --> 00:11:09,400
For example, on the input for first name, we have an input with the name of first name and the value

187
00:11:09,400 --> 00:11:11,610
of whatever the person typed into that form.

188
00:11:11,620 --> 00:11:17,740
So I'll call the first part key, which I will spell with an E and on an array, which is a string,

189
00:11:18,430 --> 00:11:23,030
and then whatever they type into it will be a value, which is also a string.

190
00:11:23,380 --> 00:11:24,940
OK, so I've created that.

191
00:11:25,570 --> 00:11:28,690
Now I'm going to create a variable for the actual tests.

192
00:11:28,690 --> 00:11:31,000
We'll create a bar and I'll call it the tests.

193
00:11:31,330 --> 00:11:36,760
It will be a slice of struct because we're going to have more than one or potentially more than one

194
00:11:36,760 --> 00:11:40,720
test we want to run in our table test instructs is not plural.

195
00:11:41,710 --> 00:11:44,530
Inside of that, I have to define the actual struct.

196
00:11:45,160 --> 00:11:46,090
So what are we going to have?

197
00:11:46,120 --> 00:11:47,980
Well, first of all, I'm going to call name.

198
00:11:48,010 --> 00:11:52,300
That'll just be the name for the test and it'll be whatever I want to call an individual test.

199
00:11:52,990 --> 00:11:57,280
Then we have to have the URL, the path which is matched by our roots.

200
00:11:57,310 --> 00:12:00,040
And that's a string then I need.

201
00:12:00,070 --> 00:12:07,450
To have the method, is it a get or a post that's just a string, then I need to have possibly Prem's

202
00:12:08,290 --> 00:12:13,150
those would be the things that are being posted and that's going to be, you know, a form can have

203
00:12:13,150 --> 00:12:14,250
more than one input.

204
00:12:14,260 --> 00:12:15,040
Most do.

205
00:12:15,340 --> 00:12:20,080
So it has to be a slice of post data, which we just defined right up above here.

206
00:12:20,890 --> 00:12:25,720
And then finally, we need something to test, something to say whether or not a test has passed.

207
00:12:25,720 --> 00:12:31,900
And the easiest thing to test for right now, at least, is what kind of response code are we getting

208
00:12:31,900 --> 00:12:35,560
back from the Web server if it's 200, everything worked the way it's supposed to.

209
00:12:35,860 --> 00:12:38,410
If it's 404, page is not found.

210
00:12:38,620 --> 00:12:41,490
If it's in the 300 range, it's a redirect.

211
00:12:41,500 --> 00:12:46,730
So I'm just going to say expected status code and that will be an end.

212
00:12:47,720 --> 00:12:52,120
OK, so we've defined our structure, but it doesn't have any values.

213
00:12:52,120 --> 00:12:53,800
So I'm going to put an equal sign here.

214
00:12:53,800 --> 00:12:54,790
Just make the shorthand.

215
00:12:54,790 --> 00:12:58,270
I'm going to define my struct and populate with data all in one step.

216
00:12:59,260 --> 00:13:06,100
So because this is a slice of structure inside of these parentheses that I just created, I'm going

217
00:13:06,100 --> 00:13:08,650
to have each entry in its own curly braces.

218
00:13:09,760 --> 00:13:14,800
And the first thing I want for my first one, let's just test the homepage will take Will Naimark test

219
00:13:14,800 --> 00:13:19,780
home because we're testing the homepage, the path that is just slash and that's straight out of our

220
00:13:19,780 --> 00:13:20,420
routes file.

221
00:13:20,890 --> 00:13:27,130
The method is get because there's no form being posted on the homepage and then I need to specify the

222
00:13:27,130 --> 00:13:27,760
parameters.

223
00:13:27,790 --> 00:13:33,460
Well, there are no primes for the home page, so I'll just put in a slice of post data and make it

224
00:13:33,460 --> 00:13:33,760
empty.

225
00:13:34,330 --> 00:13:36,040
And that satisfies that requirement.

226
00:13:36,520 --> 00:13:42,400
And for the status that I'm getting back from the server, I could put 200 because that is status.

227
00:13:42,400 --> 00:13:48,760
OK, but I like to use the constants built into the standard library http status.

228
00:13:48,770 --> 00:13:50,520
OK, all right.

229
00:13:51,070 --> 00:13:55,380
So there is one thing we can test now.

230
00:13:55,420 --> 00:13:58,210
Let's just test that one by itself to make sure we have everything right.

231
00:13:58,210 --> 00:13:59,620
So we'll write the actual test here.

232
00:14:00,070 --> 00:14:07,050
Func test handlers, which is just a name that I'm choosing because I'm testing all of my handlers and

233
00:14:07,090 --> 00:14:11,290
has a parameter of T type of pointer to testing Dorte.

234
00:14:12,820 --> 00:14:16,450
And inside we first of all, to get our roots.

235
00:14:16,620 --> 00:14:22,540
Well, remember, we have this function we set up in our setup underscore test called Get Roots, and

236
00:14:22,540 --> 00:14:23,440
that returns.

237
00:14:23,440 --> 00:14:28,600
First of all, it sets a whole bunch of things up and then it returns mux and mux is our router.

238
00:14:28,600 --> 00:14:29,950
So we can just call that function.

239
00:14:29,950 --> 00:14:37,140
So go back to handlers test and say route's is assigned the value of get roots there.

240
00:14:37,180 --> 00:14:38,120
Now we have our roots.

241
00:14:39,010 --> 00:14:40,000
Now, here's the cool part.

242
00:14:40,930 --> 00:14:47,920
How are we going to create a Web server that actually returns status code, something we can post to?

243
00:14:48,250 --> 00:14:50,950
We need to, in effect, create a server.

244
00:14:51,190 --> 00:14:54,910
And we also need to create a client that can call that server.

245
00:14:55,180 --> 00:14:59,670
And the great thing about testing ingoing is that all of this is done for us.

246
00:15:00,220 --> 00:15:02,890
We can just create a test server and stored in a variable.

247
00:15:02,890 --> 00:15:11,710
So I'll create a variable called for test server and I will assign that the value of HTP test DOT New

248
00:15:11,800 --> 00:15:12,640
Teela server.

249
00:15:13,240 --> 00:15:17,320
And that requires well, you see what it requires just by rolling over top of it.

250
00:15:17,320 --> 00:15:22,510
It says it requires an HIV handler, which we just got when we called get route's.

251
00:15:22,510 --> 00:15:23,790
We put our reach in there.

252
00:15:25,330 --> 00:15:26,710
Now, I've created that server.

253
00:15:27,340 --> 00:15:31,180
Now, once you create the server, it's going to fire up for the life of this task.

254
00:15:31,180 --> 00:15:33,580
It's going to listen on a poor it's going to open some things.

255
00:15:33,580 --> 00:15:36,490
And it's always good practice to close that when you're done with it.

256
00:15:36,490 --> 00:15:43,210
So we can do that using our defer keyword defer close, which is a function built right into that package.

257
00:15:43,900 --> 00:15:49,450
So as you know, from way back when Defer doesn't get whatever is after the keyword deferred doesn't

258
00:15:49,450 --> 00:15:52,960
get executed until after the current function is finished.

259
00:15:52,960 --> 00:15:56,480
So when test handlers is finished, it will close the test server.

260
00:15:57,340 --> 00:16:01,510
Now we need to do something to actually run our individual tests.

261
00:16:01,510 --> 00:16:02,920
And this is a table test.

262
00:16:02,920 --> 00:16:08,290
So we use exactly the same logic we did last time around, the last time we did a table test.

263
00:16:08,290 --> 00:16:12,910
And it's been a while, but for I don't care what the index is, so I'll use the placeholder for that,

264
00:16:12,910 --> 00:16:19,360
the blank underscore and then I'll name my individual test as I'm running it e because I got to call

265
00:16:19,360 --> 00:16:20,620
it something and it works.

266
00:16:20,950 --> 00:16:25,060
That's going to be assigned the value of range through the tests.

267
00:16:26,830 --> 00:16:31,450
Now inside of that for loop, there are really two kinds of tests we're going to be running.

268
00:16:31,570 --> 00:16:36,520
There are ones that may get requests like just showing the homepage or showing the about page.

269
00:16:36,820 --> 00:16:42,370
And there are ones that make post requests, ones that are where a user fills out a form and submits

270
00:16:42,370 --> 00:16:43,330
that as a post.

271
00:16:43,540 --> 00:16:46,990
So we need to determine which time we're going to run and we can do that really easily.

272
00:16:47,440 --> 00:16:57,520
If IDOT method, which we specified right up here, if that's equal to get do this, otherwise it's

273
00:16:57,520 --> 00:16:57,880
post.

274
00:16:58,450 --> 00:16:59,910
And right now I'm just going to do with the.

275
00:17:00,140 --> 00:17:03,360
The deal with the debt requests, we'll do the post requests in the next election.

276
00:17:04,280 --> 00:17:05,780
So what do we want to test here?

277
00:17:05,900 --> 00:17:12,860
Well, we actually need to make a request as a client as though we were a Web browser accessing a Web

278
00:17:12,860 --> 00:17:13,220
page.

279
00:17:13,250 --> 00:17:17,660
Unfortunately, that functionality is built right into our test server.

280
00:17:18,120 --> 00:17:25,370
We can call TEUs client, which an up close client, which effectively creates a Web browser or a Web

281
00:17:25,370 --> 00:17:25,820
client.

282
00:17:26,130 --> 00:17:30,860
And then we'll call the get method on that and we'll call our You URL.

283
00:17:30,950 --> 00:17:36,560
Now, you might think that's just Iida neutral, which is in this case a slash, but actually we need

284
00:17:36,560 --> 00:17:37,550
a little bit before that.

285
00:17:37,580 --> 00:17:41,380
We need the part that actually calls the Web server and we don't know what that is.

286
00:17:41,390 --> 00:17:43,100
We don't know what port it's listening on.

287
00:17:43,640 --> 00:17:49,910
Fortunately, TSA does our test server has your URL as an available value.

288
00:17:49,910 --> 00:17:55,020
We can call and all we do is append our URL to whatever the test servers you are.

289
00:17:55,580 --> 00:17:58,730
So we've now called that now.

290
00:17:59,090 --> 00:18:00,500
We're not doing anything with this.

291
00:18:00,500 --> 00:18:05,360
We need to store that in a response, which I'll call response and get actually returns.

292
00:18:05,360 --> 00:18:06,680
As you can see, if you roll over it.

293
00:18:06,800 --> 00:18:07,420
Two things.

294
00:18:07,430 --> 00:18:13,190
It returns the response, which is a pointer to a response, and it also might return an error if something

295
00:18:13,190 --> 00:18:13,680
goes wrong.

296
00:18:13,700 --> 00:18:19,490
So we have to track for that slow response and error are equal to our test server client.

297
00:18:19,490 --> 00:18:24,190
Make a get request to the URL we want to test, then we check for an error.

298
00:18:24,230 --> 00:18:28,760
So if the error is not equal to nil, then we what are we going to do?

299
00:18:28,790 --> 00:18:33,290
Well, let's go with T to log the log there and then we'll fail the test.

300
00:18:33,380 --> 00:18:35,600
Edenvale teed up.

301
00:18:35,600 --> 00:18:38,070
Fadal will kill it for us and we'll put the error in there too.

302
00:18:39,080 --> 00:18:39,490
All right.

303
00:18:40,130 --> 00:18:43,160
If we get past that, then we need to check our response.

304
00:18:43,520 --> 00:18:45,680
And how are we going to do that when we're going to do it?

305
00:18:45,680 --> 00:18:51,680
By looking at the status code, we get back from our test server and comparing that with our expected

306
00:18:51,680 --> 00:18:52,280
status code.

307
00:18:52,520 --> 00:18:55,090
And all we have to do is say give respect.

308
00:18:55,130 --> 00:19:03,440
Status code is not equal to our expected status code e dot expected status code, then write an error

309
00:19:03,440 --> 00:19:08,320
teig error and they still use error f g so I can put a more meaningful error in there.

310
00:19:08,600 --> 00:19:17,060
It works just like s print F and I'll just say four and the test sname, which is a string expected

311
00:19:17,870 --> 00:19:23,960
the integer what our expected value is but got the other integer, the one that we actually got back

312
00:19:23,960 --> 00:19:26,540
then we replaced those placeholders with our actual values.

313
00:19:26,930 --> 00:19:34,260
So Eatock name is the name of the test idot expected status code is the expected status code and respect

314
00:19:34,280 --> 00:19:35,690
status code is the status.

315
00:19:35,690 --> 00:19:40,700
Could we actually go and that should be enough to test our homepage.

316
00:19:40,880 --> 00:19:49,330
So let's try it out, go to my terminal and let's run the tests, make sure we're in the right direction.

317
00:19:49,340 --> 00:19:56,410
I'm not so CWD internal handlers now I'm in the correct directory and let's run the test.

318
00:19:56,420 --> 00:19:58,850
Go test dash verbose.

319
00:20:01,570 --> 00:20:02,420
And it failed.

320
00:20:02,440 --> 00:20:03,400
What did it fail with?

321
00:20:05,200 --> 00:20:06,670
Oh, I see what I forgot to do.

322
00:20:06,700 --> 00:20:07,450
So let's go back.

323
00:20:07,570 --> 00:20:12,670
I forgot to actually use that test to create templates in our setup.

324
00:20:12,670 --> 00:20:20,440
So we're going to set up Dutko, set up underscore Testico and find the part where we actually yes.

325
00:20:20,440 --> 00:20:24,040
This should not be render create template, template cache.

326
00:20:24,790 --> 00:20:30,000
And I said we built a new one and put it in here just for this purpose.

327
00:20:30,010 --> 00:20:35,470
So let's go back to our terminal, clear the screen, go test, Dashti.

328
00:20:37,080 --> 00:20:38,850
And it passed perfect.

329
00:20:38,970 --> 00:20:41,450
All right, so let's go back and try a few more routes.

330
00:20:42,540 --> 00:20:46,430
So in our tests, we need to add some more test data.

331
00:20:46,440 --> 00:20:50,940
So let's see what else we have for gets under our setup test.

332
00:20:50,970 --> 00:20:51,810
Let's look at our routes.

333
00:20:51,810 --> 00:20:55,720
We have aget for about four generals quarters and four major suite.

334
00:20:55,740 --> 00:20:56,970
Let's do those.

335
00:20:57,660 --> 00:21:01,070
So we'll just duplicate this one twice, three times.

336
00:21:01,080 --> 00:21:07,710
So we have one for about one for generals quarters one four major suite.

337
00:21:07,830 --> 00:21:18,550
And all I have to do here is change the URLs, slash about generals quarters and Meijers suite.

338
00:21:19,080 --> 00:21:20,270
Let's see what other ones we have.

339
00:21:21,450 --> 00:21:23,670
We have a get for search availability.

340
00:21:23,850 --> 00:21:33,540
So let's do that one search availability, which I'll just call SRN and change the route to search availability

341
00:21:34,590 --> 00:21:35,160
anymore.

342
00:21:36,600 --> 00:21:40,230
We have one for contact and one for make reservation.

343
00:21:41,940 --> 00:21:54,550
So contact and make reservation contact, which is just contact and make reservation, make a reservation.

344
00:21:55,260 --> 00:21:55,790
All right.

345
00:21:55,800 --> 00:21:57,240
So we have those set up.

346
00:21:57,660 --> 00:21:58,690
All look right to me.

347
00:21:58,710 --> 00:22:02,460
Let's find out is there and we will go test Rushdi.

348
00:22:04,150 --> 00:22:05,490
And they all passed good.

349
00:22:05,620 --> 00:22:09,070
Now let's make sure it's actually working instead of having status.

350
00:22:09,100 --> 00:22:13,220
OK, here, let's change that to status not found.

351
00:22:13,540 --> 00:22:17,920
Now, this should fail because you won't get a 404 when you go to make that reservation.

352
00:22:20,290 --> 00:22:24,950
And it felt good, so we'll just change that back to status, OK, perfect.

353
00:22:25,000 --> 00:22:30,490
So those are our get requests and in the next lecture we'll actually do our post requests.

354
00:22:30,490 --> 00:22:32,380
And that's not terribly difficult either.
