1
00:00:01,350 --> 00:00:07,260
So while I was writing the last remaining tests for our handlers package, I realized that there is

2
00:00:07,260 --> 00:00:13,530
one particular function availability, Jason, where you may not, in fact, have enough information

3
00:00:13,530 --> 00:00:14,430
to complete this one.

4
00:00:14,430 --> 00:00:15,390
So I'll get you started.

5
00:00:16,080 --> 00:00:21,030
So first of all, when you look at the function itself, you'll notice that I don't actually pass the

6
00:00:21,030 --> 00:00:24,270
form and I need to pass the form in order to write a test.

7
00:00:24,660 --> 00:00:29,940
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

8
00:00:29,940 --> 00:00:34,680
what I'm doing here, what I'm doing, first of all, saying calling pass form, which I told you is

9
00:00:34,680 --> 00:00:39,360
good practice and it's good practice because when you're testing things, for example, you actually

10
00:00:39,360 --> 00:00:41,170
have to have this or you can't test it.

11
00:00:41,610 --> 00:00:45,630
The function worked just fine without it in the actual application.

12
00:00:45,630 --> 00:00:48,180
But we need to write test, so I'm going to pass it.

13
00:00:48,300 --> 00:00:51,600
And all I do here is pass the form and then I check for an error.

14
00:00:51,600 --> 00:00:56,340
And if there's an error, I'm going to return, not a server error or anything like that.

15
00:00:56,530 --> 00:01:00,870
I'm actually going to return Jason and Jason that says it's false and populates the message.

16
00:01:00,870 --> 00:01:09,300
So I just construct a JSON response which is defined right in this application right here, put in the

17
00:01:09,300 --> 00:01:13,530
appropriate values and write that Jason back to the browser and then I keep going.

18
00:01:13,830 --> 00:01:18,210
And the other thing that I want to do here is I'm going to copy this little bit where I'm writing JSON

19
00:01:18,660 --> 00:01:20,880
as when I hit the database.

20
00:01:21,300 --> 00:01:25,410
I actually need to ensure that I'm not writing a server error because that's just going to, you know,

21
00:01:25,410 --> 00:01:26,550
cause grief for us.

22
00:01:26,730 --> 00:01:29,580
Instead, I'll return again JSON.

23
00:01:29,580 --> 00:01:31,740
But this time I'll change the message to.

24
00:01:36,080 --> 00:01:41,330
Error connecting to database, which is sufficient for my purposes right now.

25
00:01:41,840 --> 00:01:44,840
OK, so that's the change I made to the application itself.

26
00:01:45,110 --> 00:01:48,380
And then down here, I actually removed the error check.

27
00:01:48,410 --> 00:01:53,630
And that's on this one, because when you think about it, I'd get into a situation where I'm trying

28
00:01:53,630 --> 00:01:56,230
to create JSON and have an error.

29
00:01:56,240 --> 00:02:01,030
So I try to create JSON to send the fact that I have an error and it just is a never ending chain there.

30
00:02:01,310 --> 00:02:07,580
So all I've done is to look at the actual code and here I am manually constructing this JSON response.

31
00:02:07,580 --> 00:02:10,070
So I know it's always going to be correct.

32
00:02:10,460 --> 00:02:13,200
There is no situation where this will ever throw an error.

33
00:02:13,220 --> 00:02:14,990
So all I did here was remove the error.

34
00:02:14,990 --> 00:02:19,190
And I added a comment to this, which you'll see in the source code if you choose to download it from

35
00:02:19,190 --> 00:02:19,910
the next lecture.

36
00:02:20,600 --> 00:02:25,280
So now let's go back and get started writing this test, and I will just start writing it for you and

37
00:02:25,280 --> 00:02:26,550
let you finish it on your own.

38
00:02:27,530 --> 00:02:35,300
So I am going to put in a new function here, which I will call func test repository.

39
00:02:35,300 --> 00:02:37,970
And the one I want is availability JSON right there.

40
00:02:38,630 --> 00:02:41,290
So the very first thing I'm going to do is identify my first case.

41
00:02:41,300 --> 00:02:46,940
So what's my first case to first case is rooms are not available.

42
00:02:47,850 --> 00:02:54,950
OK, so all I need to do is create a request body and my request body will be named Recke Body.

43
00:02:55,670 --> 00:03:03,170
It's assign the value of some start date start equals 2050 and fire in the future 01 01.

44
00:03:08,350 --> 00:03:17,830
And then I will, as I've done before, append to that request body equals format, not a sprint F and

45
00:03:17,830 --> 00:03:23,680
then a placeholder that's a string and an ampersand and a placeholder that's a string.

46
00:03:24,010 --> 00:03:28,780
And my two values, the first one being request body, because I'm appending to this I'm actually building

47
00:03:28,780 --> 00:03:36,730
on this string and then I want to put an end equals 250 zero one zero two and then I need a remedy.

48
00:03:36,820 --> 00:03:45,250
So I will now append to that room ID and I will make that one.

49
00:03:45,880 --> 00:03:48,580
OK, so I've got my request body now.

50
00:03:48,580 --> 00:03:52,390
I need to actually create the request, so create a request.

51
00:03:54,220 --> 00:04:02,070
And we do that just like we did before, request and ignore the error or assign the value of HTTP request.

52
00:04:02,830 --> 00:04:06,550
The method is post because this is a handler that takes a post.

53
00:04:07,090 --> 00:04:16,630
The URL is search availability, Jason, and we put in the body of the request using strings dot new

54
00:04:16,630 --> 00:04:21,120
reader and we put in our string called record body.

55
00:04:21,850 --> 00:04:22,960
So now I have my request.

56
00:04:24,010 --> 00:04:25,110
What do I do with it next?

57
00:04:25,120 --> 00:04:29,110
Well, I get the contacts with the session, which I'm not using, but I'm going to I might at some

58
00:04:29,110 --> 00:04:30,260
point in the future, who knows.

59
00:04:30,280 --> 00:04:35,770
So get context with session and I put in I get it this way.

60
00:04:35,770 --> 00:04:40,180
Context is a sign the value of get context and my request.

61
00:04:40,930 --> 00:04:46,780
And then I append that request to put that that context back into my request.

62
00:04:46,960 --> 00:04:50,110
Request equals request with context.

63
00:04:50,380 --> 00:04:52,240
And I put in my context.

64
00:04:54,040 --> 00:04:55,420
All right, what do I do next?

65
00:04:55,660 --> 00:04:57,550
Set the request header.

66
00:04:57,940 --> 00:05:06,500
And if you recall, we do that by saying request dot header, dot set and we want to set oops, I don't

67
00:05:06,520 --> 00:05:09,100
want to do their set.

68
00:05:12,020 --> 00:05:21,430
Content type, and that is of type X dash w w w dash form, dash Eurail encoded.

69
00:05:23,300 --> 00:05:24,480
So I've set the header now.

70
00:05:25,190 --> 00:05:32,120
Next step, make our handler a handler function, make handler handler function.

71
00:05:33,140 --> 00:05:44,180
And we do that very easily by saying handler is assigned the value of handler htp handler func and the

72
00:05:44,180 --> 00:05:48,200
handler we want to call, which is repo dot availability.

73
00:05:48,560 --> 00:05:49,220
Try that again.

74
00:05:50,750 --> 00:05:51,900
Availability Jason.

75
00:05:53,180 --> 00:05:53,690
There it is.

76
00:05:53,760 --> 00:05:57,530
Now we have our handler and we make the request.

77
00:06:00,720 --> 00:06:10,310
To our handler, so I say handler serve HTP and we want a response recorder, which I haven't made yet,

78
00:06:10,320 --> 00:06:13,800
so I'll have to do that and a request.

79
00:06:14,280 --> 00:06:19,560
So let's get our response recorder, get a response recorder.

80
00:06:20,370 --> 00:06:24,900
Are is assigned the value of HTP test.

81
00:06:26,220 --> 00:06:26,840
New recorder.

82
00:06:29,970 --> 00:06:35,010
All right, so now we've made the request to our handler and here's the part where you really don't

83
00:06:35,010 --> 00:06:36,480
ah, you're not sure how to handle this.

84
00:06:36,510 --> 00:06:38,000
You may not be sure how to handle this.

85
00:06:38,010 --> 00:06:42,960
We have covered Jason way back earlier in the course when we were going through the basics of Goaland.

86
00:06:43,770 --> 00:06:49,790
But I need to get what the server is sending back to me and convert that into Jason.

87
00:06:49,800 --> 00:06:55,140
So what I'm going to do is create a new variable, Varg, which is Jason response, because that's the

88
00:06:55,140 --> 00:06:55,590
response.

89
00:06:55,590 --> 00:07:00,420
I'm getting it back in and I have access to that because I'm in the handler's package and then I need

90
00:07:00,420 --> 00:07:05,160
to get what's being handed back to me by the server and put that into Jason.

91
00:07:05,460 --> 00:07:11,310
So I will say error is assigned the value of just like we did at the beginning of the course.

92
00:07:11,310 --> 00:07:12,750
Jason Marshall.

93
00:07:13,800 --> 00:07:19,320
And my data in this case is the body that the response is sending back to me.

94
00:07:19,320 --> 00:07:24,390
So I need to convert that to a slice of bytes because that's what this this particular function needs.

95
00:07:24,870 --> 00:07:31,440
And all I do is call our body, which is the body of the response being sent to us by the test server

96
00:07:31,740 --> 00:07:33,060
and convert that to a string.

97
00:07:34,710 --> 00:07:42,990
And then I'm putting all of that into my variable that I just declared J and now I have let's see if

98
00:07:42,990 --> 00:07:43,560
I got this right.

99
00:07:43,560 --> 00:07:44,040
I did.

100
00:07:44,050 --> 00:07:45,270
I got a check for an error.

101
00:07:45,270 --> 00:07:50,190
So if error is not equal to nil, then I want to fail the test.

102
00:07:50,430 --> 00:08:00,870
Teig error failed to pass, Jason, so that should get you far enough that you can now complete the

103
00:08:00,870 --> 00:08:06,600
actual test you need to do, because you'll have the response from the server in this structure, in

104
00:08:06,600 --> 00:08:09,060
this struct J, which is of type Jason response.

105
00:08:09,060 --> 00:08:12,870
And you can actually look at the responses you're getting back from the server.

106
00:08:12,930 --> 00:08:15,840
So that should be enough to let you finish writing this test.

107
00:08:15,870 --> 00:08:18,650
The rest of them, you have everything you need at your disposal.

108
00:08:18,930 --> 00:08:19,620
So good luck.
