1
00:00:01,950 --> 00:00:07,350
So we need to fix the problem that we have with our tests in our handler package and let's run those

2
00:00:07,350 --> 00:00:10,530
tests again and this time I'll run them right in the handlers directory.

3
00:00:10,540 --> 00:00:13,440
So those are the only tests that I'm running go test.

4
00:00:15,420 --> 00:00:20,670
And if I look at the beginning of this right with the very top, just after I ran the tests.

5
00:00:22,170 --> 00:00:29,010
Which is right here, you'll see that it's calling telling us we have an error in handlers don't go

6
00:00:29,010 --> 00:00:30,210
line 65.

7
00:00:30,240 --> 00:00:33,010
So let's go look at handlers, don't go and see what's going on.

8
00:00:34,410 --> 00:00:37,620
Handlers don't go line 65.

9
00:00:38,190 --> 00:00:43,980
There are Salin, 65 is Helper's server error cannot get reservation from session.

10
00:00:43,980 --> 00:00:49,020
And if you look at all of the errors that were generated there, you'll quickly discover that every

11
00:00:49,020 --> 00:00:54,530
single one of them has to do with the fact that it can't get information from the session.

12
00:00:54,720 --> 00:00:56,580
So clearly we have a problem here.

13
00:00:56,610 --> 00:01:02,250
So let's go look at the actual tests themselves and see how they work so handlers don't test.

14
00:01:02,640 --> 00:01:06,360
The one that we're dealing with is calling the reservation.

15
00:01:06,360 --> 00:01:07,940
And that's this one, right?

16
00:01:10,500 --> 00:01:16,830
Here, make reservation, so make reservation is the route that is failing and it's not passing any

17
00:01:16,830 --> 00:01:17,530
post data.

18
00:01:17,820 --> 00:01:19,890
It's looking for an HTTP status.

19
00:01:19,890 --> 00:01:23,300
OK, and that's the URL that it's calling as a get request.

20
00:01:23,340 --> 00:01:29,850
So if we actually look at the handler itself, again, this is the function that is causing us the problem.

21
00:01:30,000 --> 00:01:32,500
And the problem is it can't get the session.

22
00:01:33,450 --> 00:01:40,020
Well, these tests are set up using setup test, this file right here in the handler's folder.

23
00:01:40,050 --> 00:01:42,020
Let's see if we're doing anything with the section there.

24
00:01:42,780 --> 00:01:48,780
So it looks like we have our imports, we declare some variables and then we have this function get

25
00:01:48,780 --> 00:01:49,200
roots.

26
00:01:49,200 --> 00:01:51,990
And that's what's called right in the tests.

27
00:01:52,350 --> 00:01:54,510
That's what's called before these tests run.

28
00:01:54,540 --> 00:02:00,300
So if I look at the handlers test, what I'm doing is calling get rates, setting up a server and then

29
00:02:00,300 --> 00:02:01,470
just running my tests.

30
00:02:01,830 --> 00:02:03,810
So it's clearly it's not getting the session.

31
00:02:03,810 --> 00:02:04,500
And why not?

32
00:02:04,650 --> 00:02:08,790
Because at no point do we ever put anything in the session.

33
00:02:09,660 --> 00:02:12,310
We need to have access to our request variable.

34
00:02:12,480 --> 00:02:13,590
Well, we have a request.

35
00:02:13,590 --> 00:02:19,500
There's no problem there, but we're not actually putting anything in that request to pull out in that

36
00:02:19,500 --> 00:02:19,950
handler.

37
00:02:20,190 --> 00:02:21,730
So we need some means of doing that.

38
00:02:22,200 --> 00:02:27,540
So what I'm going to do right now is just comment all of this stuff out and get a comment about just

39
00:02:27,540 --> 00:02:31,020
so these tests don't run at all because this is now an empty slice.

40
00:02:31,470 --> 00:02:34,430
It will never run anything in test handlers.

41
00:02:34,770 --> 00:02:40,340
And what I'll do instead is come down here and write a test specifically for the availability.

42
00:02:40,770 --> 00:02:43,080
So how are we going to do that?

43
00:02:43,410 --> 00:02:49,860
Create a new function func and it will be called Test Repository Reservation.

44
00:02:50,550 --> 00:02:51,870
So that's the test I'm going to run.

45
00:02:52,260 --> 00:02:58,500
But before I do that, I need to have access to a request variable that I can put a sessional variable

46
00:02:58,500 --> 00:03:00,690
in before I call that handler.

47
00:03:01,140 --> 00:03:05,250
So I'm going to go back to my setup tests and I'm going to make some changes here.

48
00:03:05,280 --> 00:03:08,190
First of all, I'm not going to use just to get route's function.

49
00:03:08,190 --> 00:03:13,920
Instead, we'll take advantage of that test main function that we used in our other tests.

50
00:03:14,700 --> 00:03:19,980
So I'll create a function called Test Me, which is what it has to be called in this instance, because

51
00:03:20,010 --> 00:03:24,420
this is part of the testing package available to us in the standard library.

52
00:03:24,810 --> 00:03:31,650
And it takes one parameter, which I'll call em, and it's a type testing program and it doesn't return

53
00:03:31,650 --> 00:03:32,010
anything.

54
00:03:32,820 --> 00:03:38,070
And then I'll grab the things out of routes that really belong in that test main function anyway.

55
00:03:38,100 --> 00:03:41,310
And that's everything up to and including the renderers.

56
00:03:41,640 --> 00:03:45,750
So I'll cut them out of here, I'll paste them into here.

57
00:03:46,770 --> 00:03:54,120
And then, of course, we need to have that OCR exit and run the tests just before you die, before

58
00:03:54,120 --> 00:03:55,170
it exits running.

59
00:03:56,460 --> 00:03:57,720
So they should be OS.

60
00:04:00,210 --> 00:04:05,400
All right, so now we have a test main function, and it seems that I have an error here.

61
00:04:05,460 --> 00:04:06,930
Oh, yes, Godbolt Register.

62
00:04:08,500 --> 00:04:09,400
Let's just.

63
00:04:11,480 --> 00:04:16,460
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

64
00:04:16,490 --> 00:04:21,650
register that'll force it to reimported into our imports and the air should go away.

65
00:04:22,160 --> 00:04:22,720
And it does.

66
00:04:23,390 --> 00:04:26,360
So now I've functionally I've changed nothing.

67
00:04:26,360 --> 00:04:32,150
I've just made it run the the set up in a test main function and we still have this get route's which

68
00:04:32,150 --> 00:04:33,380
we may or may not use.

69
00:04:33,440 --> 00:04:34,970
We'll find out in a little while.

70
00:04:36,020 --> 00:04:41,570
So let's go back to our tests and see what we can do to make this test actually passed.

71
00:04:42,770 --> 00:04:47,180
So what I want to do, first of all, is look at the reservation handler itself.

72
00:04:47,180 --> 00:04:48,350
So let's go back to handlers.

73
00:04:48,710 --> 00:04:54,350
It's expecting to pull a model's reservation out of the session and start in this variable.

74
00:04:54,390 --> 00:04:58,730
OK, so we need to have a model reservation to put in the session.

75
00:04:58,760 --> 00:04:59,710
Well, we know how to do that.

76
00:04:59,720 --> 00:05:00,250
That's easy.

77
00:05:01,010 --> 00:05:03,290
Let's just create a new variable called reservation.

78
00:05:04,910 --> 00:05:12,170
It's going to be assigned the value of models DOT Reservation, and I'll put just enough information

79
00:05:12,170 --> 00:05:18,020
in this variable to be able to actually deal with it in the reservation function in our handlers.

80
00:05:18,260 --> 00:05:23,810
So we need a remedy and we'll just use one, which is the general's quarters.

81
00:05:24,260 --> 00:05:29,240
And we also need the room, which is of type models room.

82
00:05:29,900 --> 00:05:31,490
We need the name and the ID in that.

83
00:05:31,490 --> 00:05:36,020
So that's going to be of type models that room.

84
00:05:38,410 --> 00:05:47,470
And inside of that, we will populate its ID, which is one, and it's Ramnath, which is Generals'

85
00:05:48,430 --> 00:05:49,150
quarters.

86
00:05:51,240 --> 00:05:57,770
When you come there and I need a comma there, and this should be a colon, not a comma.

87
00:05:59,540 --> 00:06:03,540
So now we have a reservation variable and I we need to put that in the session somehow.

88
00:06:03,560 --> 00:06:04,570
So let's get a request.

89
00:06:04,580 --> 00:06:11,840
We know how to get a request, request and potentially an error, which I'll ignore or assign the value

90
00:06:11,840 --> 00:06:13,790
of a new request.

91
00:06:15,770 --> 00:06:21,230
And the method is going to be get just as just like it is in the roots and the euro will be the same

92
00:06:21,230 --> 00:06:22,340
one we used in the Ritz.

93
00:06:22,340 --> 00:06:26,200
Make the reservation and we'll pass an antibody.

94
00:06:27,350 --> 00:06:29,210
So that's not HIV test.

95
00:06:29,450 --> 00:06:31,150
We're going to use HTTP request.

96
00:06:31,200 --> 00:06:31,630
There we are.

97
00:06:31,910 --> 00:06:37,220
So now we have a request and we need to put that in to the request.

98
00:06:38,420 --> 00:06:45,860
We need to put our reservation variable as a special variable into the session of the request.

99
00:06:46,220 --> 00:06:48,570
And of course, you do that using the context.

100
00:06:48,950 --> 00:06:50,390
Now, how am I going to do that?

101
00:06:50,420 --> 00:06:57,260
What I'm going to do is create a new function down here, which I'll just call get and it will take

102
00:06:57,260 --> 00:07:07,550
a request, which is a pointer to an HTTP request, and it's going to return a context, context, context.

103
00:07:08,670 --> 00:07:15,350
So what we're going to do is essentially get a context, put our sectional variable in it and then store

104
00:07:15,350 --> 00:07:17,270
that in our request.

105
00:07:17,810 --> 00:07:25,160
In order to do that, the context has to have a particular value in there, a key, something that is

106
00:07:25,280 --> 00:07:28,350
that these our session package knows is the session.

107
00:07:28,820 --> 00:07:30,260
Fortunately, that's pretty easy to do.

108
00:07:30,560 --> 00:07:36,110
So what I'm going to do is in this function, I'm going to create a context and potentially an error

109
00:07:37,160 --> 00:07:43,280
by calling session load, which is built right into our section session package.

110
00:07:43,280 --> 00:07:45,680
And we've done this before session that load.

111
00:07:46,370 --> 00:07:55,220
It requires a context which will be from the request that we've just passed this request context and

112
00:07:55,220 --> 00:07:56,740
it requires a header.

113
00:07:56,990 --> 00:08:05,450
So what we're going to do is get a header from that context, from that request request, that header

114
00:08:05,870 --> 00:08:13,250
doget and the header key is exactly like this and it uppercase and lowercase matter in this, in this.

115
00:08:13,250 --> 00:08:16,160
I think we're going to do it that way just to be absolutely safe.

116
00:08:16,640 --> 00:08:17,570
We'll check for an error.

117
00:08:17,660 --> 00:08:21,410
If error is not equal to nil, we'll just log the error.

118
00:08:21,500 --> 00:08:27,590
So log error log print line the error.

119
00:08:27,900 --> 00:08:28,760
That's all we'll do there.

120
00:08:31,450 --> 00:08:38,650
And then we'll return the context, but this context, we actually know about the header excession,

121
00:08:38,650 --> 00:08:42,930
which we need in order to read, to read from or write to the session.

122
00:08:43,840 --> 00:08:49,540
So back in this function test repository reservation, we have our request now.

123
00:08:49,540 --> 00:08:55,810
We need to get our context into that request so we'll get a context like this is a sign the value of

124
00:08:55,810 --> 00:08:59,310
get context and will pass at the request we just made.

125
00:08:59,590 --> 00:09:04,810
So now we have a context that we can actually add to our request.

126
00:09:04,810 --> 00:09:07,870
And we do that the same way we did the last time we wrote this sort of test.

127
00:09:08,260 --> 00:09:13,230
Request is equal to request with context the context we just created.

128
00:09:14,470 --> 00:09:20,680
So now we have a request that knows about the extra session header, so now we can do something with

129
00:09:20,680 --> 00:09:20,830
it.

130
00:09:21,850 --> 00:09:24,310
So what's the next step?

131
00:09:24,310 --> 00:09:27,630
The next step is a little different, something you haven't seen yet.

132
00:09:27,670 --> 00:09:34,210
We're going to take advantage of something that's built into the testing package of goal called a new

133
00:09:34,210 --> 00:09:37,090
recorder, an HIV test, new recorder.

134
00:09:37,120 --> 00:09:43,180
So what I'm going to do is create a new variable called R for request recorder that's going to be assigned

135
00:09:43,180 --> 00:09:47,620
the value of HTP test Dort new recorder.

136
00:09:48,430 --> 00:09:50,470
And this is exactly what it sounds like.

137
00:09:50,560 --> 00:09:52,180
We can actually look at the description of it.

138
00:09:52,480 --> 00:09:57,630
A new recorder is it returns an initialized response recorder.

139
00:09:57,760 --> 00:10:03,250
So this is basically simulating what we get from the request response lifecycle.

140
00:10:03,700 --> 00:10:09,700
When someone fires up a Web browser, hits our Web site, gets to a handler, passes that request,

141
00:10:09,850 --> 00:10:14,020
gets a response writer and the response writer writes the response to the Web browser.

142
00:10:14,080 --> 00:10:19,000
This fakes that entire process or the part of it that we need for a recorder.

143
00:10:19,300 --> 00:10:21,220
So we have a response recorder here.

144
00:10:21,400 --> 00:10:22,390
We're not using it yet.

145
00:10:22,390 --> 00:10:28,060
So we have to what we're going to do now is put our reservation in the session.

146
00:10:28,420 --> 00:10:30,070
Session put.

147
00:10:31,430 --> 00:10:38,150
Just like we do in a in our actual handlers, but instead of using the context from our request, we're

148
00:10:38,150 --> 00:10:46,970
going to use the context we've built X name and reservation and pass in our reservation.

149
00:10:49,180 --> 00:10:55,720
So now we've added that reservation variable to find up here with all of these values to our context,

150
00:10:55,720 --> 00:10:57,760
we've put it in the session in effect.

151
00:10:58,480 --> 00:11:06,100
And what we want to do now is actually call our reservation function, which is a handler, while we

152
00:11:06,100 --> 00:11:09,100
can't call it directly unless we do something to it.

153
00:11:09,220 --> 00:11:17,110
I'm going to take that handler reservation and turn it into a handler function so I can do that really

154
00:11:17,110 --> 00:11:17,520
easily.

155
00:11:17,860 --> 00:11:25,720
So I'm going to say handler, which is going to be just a variable that is assigned the value of our

156
00:11:26,230 --> 00:11:27,130
reservation.

157
00:11:27,130 --> 00:11:36,190
But I'm going to cast it to an HDTV handler function, htp dot handler func and I'm just going to put

158
00:11:36,190 --> 00:11:38,100
in repo dot reservation.

159
00:11:39,610 --> 00:11:44,880
So what I've done now is take our reservation variable or function, which is right here.

160
00:11:45,580 --> 00:11:52,210
It takes a response writer and a pointer to a request, and I've turned it into a function that I can

161
00:11:52,210 --> 00:11:53,050
call directly.

162
00:11:53,080 --> 00:11:57,070
So let's go back to our handlers test and actually call that function.

163
00:11:57,580 --> 00:12:02,310
And we do that simply by saying handler dot serve HTP.

164
00:12:02,920 --> 00:12:11,320
And what I'm going to put in there for its function, for its request and response are our request recorder

165
00:12:11,350 --> 00:12:14,590
are sort of response recorder and request that we've built.

166
00:12:15,730 --> 00:12:19,360
So when I do that, I can actually call that directly.

167
00:12:19,360 --> 00:12:25,270
I'm actually calling Serve HTP on this, just as I call it, on all of our roots in the main function

168
00:12:25,270 --> 00:12:32,200
of our application, simply because I've built a request, put that sessional variable reservation into

169
00:12:32,200 --> 00:12:36,700
the session, cast my reservation handler into a handler func.

170
00:12:36,880 --> 00:12:45,490
Now it has served HTTP because that is one of the things that you this satisfies all the the functions

171
00:12:45,490 --> 00:12:49,300
necessary to become something that actually acts as a Web server.

172
00:12:49,690 --> 00:12:52,330
So I don't even need my roots for this test.

173
00:12:52,330 --> 00:12:54,120
So I don't never call get roots at all.

174
00:12:54,130 --> 00:13:02,020
Instead I'm building it manually, calling this directly by calling handler serve htp, passing it my

175
00:13:02,020 --> 00:13:08,120
response recorder and passing it my request, which has the necessary sessional information in it.

176
00:13:08,530 --> 00:13:17,920
So at this point I can actually see if this test passed by saying if my are my response recorder, if

177
00:13:17,920 --> 00:13:23,530
its code is equal to http dot status, ok.

178
00:13:25,100 --> 00:13:32,360
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

179
00:13:32,360 --> 00:13:34,460
fail my test teig error.

180
00:13:36,380 --> 00:13:44,960
And I'll use Şeref in this case, and I put my string in here, reservation handler returned.

181
00:13:46,200 --> 00:13:48,300
Wrong response, quote.

182
00:13:49,890 --> 00:14:00,360
Got percent, Dean wanted percent, Dean, and then I replaced those placeholders with got our code

183
00:14:01,110 --> 00:14:04,530
wanted HTP status, OK?

184
00:14:06,280 --> 00:14:09,530
OK, so this will probably work.

185
00:14:09,850 --> 00:14:10,510
Let's find out.

186
00:14:10,780 --> 00:14:16,870
Let's go back to our terminal, clear the screen and run the tests, go test.

187
00:14:19,430 --> 00:14:25,430
OK, it passed, let's make sure it's actually passing, let's change this back to an equal, this should

188
00:14:25,430 --> 00:14:25,770
fail.

189
00:14:25,790 --> 00:14:28,840
Now let's clear our screen, run the test again.

190
00:14:30,120 --> 00:14:35,970
And it failed, so now we have a means of putting something in the session and pulling something out

191
00:14:35,970 --> 00:14:36,640
of our session.

192
00:14:36,660 --> 00:14:37,740
And this is a good start.

193
00:14:38,070 --> 00:14:46,620
So what we'll do in the next lecture or two is begin to create tests for all of the all of the routes

194
00:14:46,620 --> 00:14:48,600
in our handlers that use the session.

195
00:14:49,110 --> 00:14:56,520
And then we'll improve those tests by changing our test database repo to respond differently based upon

196
00:14:56,520 --> 00:14:59,190
the kind of information we pass it in our tests.

197
00:14:59,190 --> 00:15:00,720
And we'll do that in the next few lectures.
