-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathtest_response.rb
More file actions
229 lines (203 loc) · 5.12 KB
/
test_response.rb
File metadata and controls
229 lines (203 loc) · 5.12 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
require 'test/unit'
require 'evma_httpserver'
begin
once = false
require 'eventmachine'
rescue LoadError => e
raise e if once
once = true
require 'rubygems'
retry
end
#--------------------------------------
module EventMachine
# This is a test harness wired into the HttpResponse class so we
# can test it without requiring any actual network communication.
#
class HttpResponse
attr_reader :output_data
attr_reader :closed_after_writing
def send_data data
@output_data ||= ""
@output_data << data
end
def close_connection_after_writing
@closed_after_writing = true
end
end
end
#--------------------------------------
class TestHttpResponse < Test::Unit::TestCase
def test_properties
a = EventMachine::HttpResponse.new
a.status = 200
a.content = "Some content"
a.headers["Content-type"] = "text/xml"
end
def test_header_sugarings
a = EventMachine::HttpResponse.new
a.content_type "text/xml"
a.set_cookie "a=b"
a.headers["X-bayshore"] = "aaaa"
assert_equal({
"Content-type" => "text/xml",
"Set-cookie" => ["a=b"],
"X-bayshore" => "aaaa"
}, a.headers)
end
def test_send_response
a = EventMachine::HttpResponse.new
a.status = 200
a.send_response
assert_equal([
"HTTP/1.1 200 ...\r\n",
"Content-length: 0\r\n",
"\r\n"
].join, a.output_data)
assert_equal( true, a.closed_after_writing )
end
def test_send_response_1
a = EventMachine::HttpResponse.new
a.status = 200
a.content_type "text/plain"
a.content = "ABC"
a.send_response
assert_equal([
"HTTP/1.1 200 ...\r\n",
"Content-length: 3\r\n",
"Content-type: text/plain\r\n",
"\r\n",
"ABC"
].join, a.output_data)
assert( a.closed_after_writing )
end
def test_send_response_no_close
a = EventMachine::HttpResponse.new
a.status = 200
a.content_type "text/plain"
a.content = "ABC"
a.keep_connection_open
a.send_response
assert_equal([
"HTTP/1.1 200 ...\r\n",
"Content-length: 3\r\n",
"Content-type: text/plain\r\n",
"\r\n",
"ABC"
].join, a.output_data)
assert( ! a.closed_after_writing )
end
def test_send_response_no_close_with_a_404_response
a = EventMachine::HttpResponse.new
a.status = 404
a.content_type "text/plain"
a.content = "ABC"
a.keep_connection_open
a.send_response
assert_equal([
"HTTP/1.1 404 ...\r\n",
"Content-length: 3\r\n",
"Content-type: text/plain\r\n",
"\r\n",
"ABC"
].join, a.output_data)
assert( ! a.closed_after_writing )
end
def test_send_response_no_close_with_a_201_response
a = EventMachine::HttpResponse.new
a.status = 201
a.content_type "text/plain"
a.content = "ABC"
a.keep_connection_open
a.send_response
assert_equal([
"HTTP/1.1 201 ...\r\n",
"Content-length: 3\r\n",
"Content-type: text/plain\r\n",
"\r\n",
"ABC"
].join, a.output_data)
assert( ! a.closed_after_writing )
end
def test_send_response_no_close_with_a_500_response
a = EventMachine::HttpResponse.new
a.status = 500
a.content_type "text/plain"
a.content = "ABC"
a.keep_connection_open
a.send_response
assert_equal([
"HTTP/1.1 500 ...\r\n",
"Content-length: 3\r\n",
"Content-type: text/plain\r\n",
"\r\n",
"ABC"
].join, a.output_data)
assert( a.closed_after_writing )
end
def test_send_response_multiple_times
a = EventMachine::HttpResponse.new
a.status = 200
a.send_response
assert_raise( RuntimeError ) {
a.send_response
}
end
def test_send_headers
a = EventMachine::HttpResponse.new
a.status = 200
a.send_headers
assert_equal([
"HTTP/1.1 200 ...\r\n",
"Content-length: 0\r\n",
"\r\n"
].join, a.output_data)
assert( ! a.closed_after_writing )
assert_raise( RuntimeError ) {
a.send_headers
}
end
def test_send_chunks
a = EventMachine::HttpResponse.new
a.chunk "ABC"
a.chunk "DEF"
a.chunk "GHI"
a.keep_connection_open
a.send_response
assert_equal([
"HTTP/1.1 200 ...\r\n",
"Transfer-encoding: chunked\r\n",
"\r\n",
"3\r\n",
"ABC\r\n",
"3\r\n",
"DEF\r\n",
"3\r\n",
"GHI\r\n",
"0\r\n",
"\r\n"
].join, a.output_data)
assert( !a.closed_after_writing )
end
def test_send_chunks_with_close
a = EventMachine::HttpResponse.new
a.chunk "ABC"
a.chunk "DEF"
a.chunk "GHI"
a.send_response
assert_equal([
"HTTP/1.1 200 ...\r\n",
"Transfer-encoding: chunked\r\n",
"\r\n",
"3\r\n",
"ABC\r\n",
"3\r\n",
"DEF\r\n",
"3\r\n",
"GHI\r\n",
"0\r\n",
"\r\n"
].join, a.output_data)
assert( a.closed_after_writing )
end
end