-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.tests.ps1
131 lines (101 loc) · 4.37 KB
/
index.tests.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
. .\index.ps1 handler.ps1::handle
Describe 'index tests' {
Context 'getRequestBody' {
It 'Should correctly read in request and return request body' {
$contentEncoding = [System.Text.Encoding]::UTF8
$message = $contentEncoding.GetBytes('{"context": null, "payload": {"name": "Jon", "place": "Winterfell"}}')
$inputStream = New-Object System.IO.MemoryStream
$inputStream.Write($message, 0 , $message.Length)
$inputStream.Seek(0, [System.IO.SeekOrigin]::Begin)
$request = @{InputStream=$inputStream; ContentEncoding=$contentEncoding}
$in = getRequestBody $request
$in.context | Should -BeExactly $null
$in.payload.name | Should -BeExactly "Jon"
$in.payload.place | Should -BeExactly "Winterfell"
}
}
Context 'processRequest' {
It 'Should return a SystemError with invalid json' {
$contentEncoding = [System.Text.Encoding]::UTF8
$message = $contentEncoding.GetBytes("{")
$inputStream = New-Object System.IO.MemoryStream
$inputStream.Write($message, 0 , $message.Length)
$inputStream.Seek(0, [System.IO.SeekOrigin]::Begin)
$request = @{InputStream=$inputStream; ContentEncoding=$contentEncoding}
try {
$r = processRequest $request
} catch [DispatchException] {
$r = $_.Exception.Error
}
$r.type | Should -BeExactly $SYSTEM_ERROR
$r.stacktrace.Count | Should -BeGreaterThan 0
}
It 'Should return a SystemError with closed input stream' {
$contentEncoding = [System.Text.Encoding]::UTF8
$message = $contentEncoding.GetBytes("{}")
$inputStream = New-Object System.IO.MemoryStream
$inputStream.Write($message, 0 , $message.Length)
$inputStream.Seek(0, [System.IO.SeekOrigin]::Begin)
$inputStream.Close()
$request = @{InputStream=$inputStream; ContentEncoding=$contentEncoding}
try {
$r = processRequest $request
} catch [DispatchException] {
$r = $_.Exception.Error
}
$r.type | Should -BeExactly $SYSTEM_ERROR
$r.stacktrace.Count | Should -BeGreaterThan 0
}
}
Context 'applyFunction' {
It 'Should return valid response with hello function' {
function hello($context, $payload) {
$name = $payload.name
if (!$name) {
$name = "Noone"
}
$place = $payload.place
if (!$place) {
$place = "Nowhere"
}
return "Hello, $name from $place"
}
$in = @{context=$null; payload=@{name="Jon"; place="Winterfell"}}
$r = applyFunction $in hello
$r | Should -BeExactly "Hello, Jon from Winterfell"
}
It 'Should return a FunctionError with fail function' {
function fail($context, $payload) {
[System.IO.File]::ReadAllText('FileNotFoundException.txt')
}
{ fail $null $null } | Should -Throw
$in = @{context=$null; payload=$null}
try {
$r = applyFunction $in fail
} catch [DispatchException] {
$r = $_.Exception.Error
}
$r.type | Should -BeExactly $FUNCTION_ERROR
$r.stacktrace.Count | Should -BeGreaterThan 0
}
It 'Should return an InputError with lower function' {
function lower($context, $payload) {
if ($payload.GetType() -ne [String]) {
throw [System.ArgumentException]::new("payload is not of type string")
}
return $payload.ToLower()
}
{ lower $null 0 } | Should -Throw
{ lower $null "" } | Should -Not -Throw
$in = @{context=$null; payload=0}
try {
$r = applyFunction $in lower
} catch [DispatchException] {
$r = $_.Exception.Error
}
$r.type | Should -BeExactly $INPUT_ERROR
$r.message | Should -BeExactly "payload is not of type string"
$r.stacktrace.Count | Should -BeGreaterThan 0
}
}
}