slayer-thrift 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (105) hide show
  1. data/CHANGELOG +1 -0
  2. data/InstalledFiles +1 -0
  3. data/Makefile +512 -0
  4. data/Makefile.am +49 -0
  5. data/Makefile.in +512 -0
  6. data/Manifest +103 -0
  7. data/README +43 -0
  8. data/Rakefile +102 -0
  9. data/benchmark/Benchmark.thrift +24 -0
  10. data/benchmark/benchmark.rb +271 -0
  11. data/benchmark/client.rb +74 -0
  12. data/benchmark/gen-rb/benchmark_constants.rb +10 -0
  13. data/benchmark/gen-rb/benchmark_service.rb +80 -0
  14. data/benchmark/gen-rb/benchmark_types.rb +9 -0
  15. data/benchmark/server.rb +82 -0
  16. data/benchmark/thin_server.rb +44 -0
  17. data/debug_proto_test/gen-rb/debug_proto_test_constants.rb +273 -0
  18. data/debug_proto_test/gen-rb/debug_proto_test_types.rb +705 -0
  19. data/debug_proto_test/gen-rb/empty_service.rb +24 -0
  20. data/debug_proto_test/gen-rb/inherited.rb +79 -0
  21. data/debug_proto_test/gen-rb/reverse_order_service.rb +82 -0
  22. data/debug_proto_test/gen-rb/service_for_exception_with_a_map.rb +81 -0
  23. data/debug_proto_test/gen-rb/srv.rb +330 -0
  24. data/ext/binary_protocol_accelerated.c +441 -0
  25. data/ext/binary_protocol_accelerated.h +20 -0
  26. data/ext/compact_protocol.c +618 -0
  27. data/ext/compact_protocol.h +20 -0
  28. data/ext/constants.h +96 -0
  29. data/ext/extconf.rb +30 -0
  30. data/ext/macros.h +41 -0
  31. data/ext/memory_buffer.c +131 -0
  32. data/ext/memory_buffer.h +20 -0
  33. data/ext/protocol.c +185 -0
  34. data/ext/protocol.h +20 -0
  35. data/ext/struct.c +716 -0
  36. data/ext/struct.h +25 -0
  37. data/ext/thrift_native.c +196 -0
  38. data/lib/thrift.rb +64 -0
  39. data/lib/thrift/client.rb +62 -0
  40. data/lib/thrift/core_ext.rb +23 -0
  41. data/lib/thrift/core_ext/fixnum.rb +29 -0
  42. data/lib/thrift/exceptions.rb +84 -0
  43. data/lib/thrift/processor.rb +57 -0
  44. data/lib/thrift/protocol/base_protocol.rb +290 -0
  45. data/lib/thrift/protocol/binary_protocol.rb +229 -0
  46. data/lib/thrift/protocol/binary_protocol_accelerated.rb +39 -0
  47. data/lib/thrift/protocol/compact_protocol.rb +426 -0
  48. data/lib/thrift/serializer/deserializer.rb +33 -0
  49. data/lib/thrift/serializer/serializer.rb +34 -0
  50. data/lib/thrift/server/base_server.rb +31 -0
  51. data/lib/thrift/server/mongrel_http_server.rb +58 -0
  52. data/lib/thrift/server/nonblocking_server.rb +305 -0
  53. data/lib/thrift/server/simple_server.rb +43 -0
  54. data/lib/thrift/server/thread_pool_server.rb +75 -0
  55. data/lib/thrift/server/threaded_server.rb +47 -0
  56. data/lib/thrift/struct.rb +237 -0
  57. data/lib/thrift/struct_union.rb +192 -0
  58. data/lib/thrift/thrift_native.rb +24 -0
  59. data/lib/thrift/transport/base_server_transport.rb +37 -0
  60. data/lib/thrift/transport/base_transport.rb +107 -0
  61. data/lib/thrift/transport/buffered_transport.rb +108 -0
  62. data/lib/thrift/transport/framed_transport.rb +116 -0
  63. data/lib/thrift/transport/http_client_transport.rb +51 -0
  64. data/lib/thrift/transport/io_stream_transport.rb +39 -0
  65. data/lib/thrift/transport/memory_buffer_transport.rb +125 -0
  66. data/lib/thrift/transport/server_socket.rb +63 -0
  67. data/lib/thrift/transport/socket.rb +137 -0
  68. data/lib/thrift/transport/unix_server_socket.rb +60 -0
  69. data/lib/thrift/transport/unix_socket.rb +40 -0
  70. data/lib/thrift/types.rb +101 -0
  71. data/lib/thrift/union.rb +179 -0
  72. data/script/proto_benchmark.rb +121 -0
  73. data/script/read_struct.rb +43 -0
  74. data/script/write_struct.rb +30 -0
  75. data/setup.rb +1585 -0
  76. data/slayer-thrift.gemspec +30 -0
  77. data/spec/ThriftSpec.thrift +132 -0
  78. data/spec/base_protocol_spec.rb +160 -0
  79. data/spec/base_transport_spec.rb +351 -0
  80. data/spec/binary_protocol_accelerated_spec.rb +46 -0
  81. data/spec/binary_protocol_spec.rb +61 -0
  82. data/spec/binary_protocol_spec_shared.rb +375 -0
  83. data/spec/client_spec.rb +100 -0
  84. data/spec/compact_protocol_spec.rb +133 -0
  85. data/spec/exception_spec.rb +142 -0
  86. data/spec/gen-rb/nonblocking_service.rb +272 -0
  87. data/spec/gen-rb/thrift_spec_constants.rb +10 -0
  88. data/spec/gen-rb/thrift_spec_types.rb +345 -0
  89. data/spec/http_client_spec.rb +64 -0
  90. data/spec/mongrel_http_server_spec.rb +117 -0
  91. data/spec/nonblocking_server_spec.rb +265 -0
  92. data/spec/processor_spec.rb +83 -0
  93. data/spec/serializer_spec.rb +69 -0
  94. data/spec/server_socket_spec.rb +80 -0
  95. data/spec/server_spec.rb +160 -0
  96. data/spec/socket_spec.rb +61 -0
  97. data/spec/socket_spec_shared.rb +104 -0
  98. data/spec/spec_helper.rb +58 -0
  99. data/spec/struct_spec.rb +295 -0
  100. data/spec/types_spec.rb +116 -0
  101. data/spec/union_spec.rb +193 -0
  102. data/spec/unix_socket_spec.rb +108 -0
  103. data/thrift.gemspec +30 -0
  104. data/tmp/thrift-0.7.0.gem +0 -0
  105. metadata +207 -0
@@ -0,0 +1,10 @@
1
+ #
2
+ # Autogenerated by Thrift Compiler (0.7.0)
3
+ #
4
+ # DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
5
+ #
6
+
7
+ require 'thrift_spec_types'
8
+
9
+ module SpecNamespace
10
+ end
@@ -0,0 +1,345 @@
1
+ #
2
+ # Autogenerated by Thrift Compiler (0.7.0)
3
+ #
4
+ # DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
5
+ #
6
+
7
+
8
+ module SpecNamespace
9
+ module SomeEnum
10
+ ONE = 0
11
+ TWO = 1
12
+ VALUE_MAP = {0 => "ONE", 1 => "TWO"}
13
+ VALID_VALUES = Set.new([ONE, TWO]).freeze
14
+ end
15
+
16
+ class Hello
17
+ include ::Thrift::Struct, ::Thrift::Struct_Union
18
+ GREETING = 1
19
+
20
+ FIELDS = {
21
+ GREETING => {:type => ::Thrift::Types::STRING, :name => 'greeting', :default => %q"hello world"}
22
+ }
23
+
24
+ def struct_fields; FIELDS; end
25
+
26
+ def validate
27
+ end
28
+
29
+ ::Thrift::Struct.generate_accessors self
30
+ end
31
+
32
+ class StructWithSomeEnum
33
+ include ::Thrift::Struct, ::Thrift::Struct_Union
34
+ SOME_ENUM = 1
35
+
36
+ FIELDS = {
37
+ SOME_ENUM => {:type => ::Thrift::Types::I32, :name => 'some_enum', :enum_class => SpecNamespace::SomeEnum}
38
+ }
39
+
40
+ def struct_fields; FIELDS; end
41
+
42
+ def validate
43
+ unless @some_enum.nil? || SpecNamespace::SomeEnum::VALID_VALUES.include?(@some_enum)
44
+ raise ::Thrift::ProtocolException.new(::Thrift::ProtocolException::UNKNOWN, 'Invalid value of field some_enum!')
45
+ end
46
+ end
47
+
48
+ ::Thrift::Struct.generate_accessors self
49
+ end
50
+
51
+ class TestUnion < ::Thrift::Union
52
+ include ::Thrift::Struct_Union
53
+ class << self
54
+ def string_field(val)
55
+ TestUnion.new(:string_field, val)
56
+ end
57
+
58
+ def i32_field(val)
59
+ TestUnion.new(:i32_field, val)
60
+ end
61
+
62
+ def other_i32_field(val)
63
+ TestUnion.new(:other_i32_field, val)
64
+ end
65
+
66
+ def enum_field(val)
67
+ TestUnion.new(:enum_field, val)
68
+ end
69
+
70
+ def binary_field(val)
71
+ TestUnion.new(:binary_field, val)
72
+ end
73
+ end
74
+
75
+ STRING_FIELD = 1
76
+ I32_FIELD = 2
77
+ OTHER_I32_FIELD = 3
78
+ ENUM_FIELD = 4
79
+ BINARY_FIELD = 5
80
+
81
+ FIELDS = {
82
+ # A doc string
83
+ STRING_FIELD => {:type => ::Thrift::Types::STRING, :name => 'string_field'},
84
+ I32_FIELD => {:type => ::Thrift::Types::I32, :name => 'i32_field'},
85
+ OTHER_I32_FIELD => {:type => ::Thrift::Types::I32, :name => 'other_i32_field'},
86
+ ENUM_FIELD => {:type => ::Thrift::Types::I32, :name => 'enum_field', :enum_class => SpecNamespace::SomeEnum},
87
+ BINARY_FIELD => {:type => ::Thrift::Types::STRING, :name => 'binary_field', :binary => true}
88
+ }
89
+
90
+ def struct_fields; FIELDS; end
91
+
92
+ def validate
93
+ raise(StandardError, 'Union fields are not set.') if get_set_field.nil? || get_value.nil?
94
+ if get_set_field == :enum_field
95
+ raise ::Thrift::ProtocolException.new(::Thrift::ProtocolException::UNKNOWN, 'Invalid value of field enum_field!') unless SpecNamespace::SomeEnum::VALID_VALUES.include?(get_value)
96
+ end
97
+ end
98
+
99
+ ::Thrift::Union.generate_accessors self
100
+ end
101
+
102
+ class Foo
103
+ include ::Thrift::Struct, ::Thrift::Struct_Union
104
+ SIMPLE = 1
105
+ WORDS = 2
106
+ HELLO = 3
107
+ INTS = 4
108
+ COMPLEX = 5
109
+ SHORTS = 6
110
+ OPT_STRING = 7
111
+ MY_BOOL = 8
112
+
113
+ FIELDS = {
114
+ SIMPLE => {:type => ::Thrift::Types::I32, :name => 'simple', :default => 53},
115
+ WORDS => {:type => ::Thrift::Types::STRING, :name => 'words', :default => %q"words"},
116
+ HELLO => {:type => ::Thrift::Types::STRUCT, :name => 'hello', :default => SpecNamespace::Hello.new({
117
+ %q"greeting" => %q"hello, world!",
118
+ }), :class => SpecNamespace::Hello},
119
+ INTS => {:type => ::Thrift::Types::LIST, :name => 'ints', :default => [
120
+ 1,
121
+ 2,
122
+ 2,
123
+ 3,
124
+ ], :element => {:type => ::Thrift::Types::I32}},
125
+ COMPLEX => {:type => ::Thrift::Types::MAP, :name => 'complex', :key => {:type => ::Thrift::Types::I32}, :value => {:type => ::Thrift::Types::MAP, :key => {:type => ::Thrift::Types::STRING}, :value => {:type => ::Thrift::Types::DOUBLE}}},
126
+ SHORTS => {:type => ::Thrift::Types::SET, :name => 'shorts', :default => Set.new([
127
+ 5,
128
+ 17,
129
+ 239,
130
+ ]), :element => {:type => ::Thrift::Types::I16}},
131
+ OPT_STRING => {:type => ::Thrift::Types::STRING, :name => 'opt_string', :optional => true},
132
+ MY_BOOL => {:type => ::Thrift::Types::BOOL, :name => 'my_bool'}
133
+ }
134
+
135
+ def struct_fields; FIELDS; end
136
+
137
+ def validate
138
+ end
139
+
140
+ ::Thrift::Struct.generate_accessors self
141
+ end
142
+
143
+ class Foo2
144
+ include ::Thrift::Struct, ::Thrift::Struct_Union
145
+ MY_BINARY = 1
146
+
147
+ FIELDS = {
148
+ MY_BINARY => {:type => ::Thrift::Types::STRING, :name => 'my_binary', :binary => true}
149
+ }
150
+
151
+ def struct_fields; FIELDS; end
152
+
153
+ def validate
154
+ end
155
+
156
+ ::Thrift::Struct.generate_accessors self
157
+ end
158
+
159
+ class BoolStruct
160
+ include ::Thrift::Struct, ::Thrift::Struct_Union
161
+ YESNO = 1
162
+
163
+ FIELDS = {
164
+ YESNO => {:type => ::Thrift::Types::BOOL, :name => 'yesno', :default => true}
165
+ }
166
+
167
+ def struct_fields; FIELDS; end
168
+
169
+ def validate
170
+ end
171
+
172
+ ::Thrift::Struct.generate_accessors self
173
+ end
174
+
175
+ class SimpleList
176
+ include ::Thrift::Struct, ::Thrift::Struct_Union
177
+ BOOLS = 1
178
+ BYTES = 2
179
+ I16S = 3
180
+ I32S = 4
181
+ I64S = 5
182
+ DOUBLES = 6
183
+ STRINGS = 7
184
+ MAPS = 8
185
+ LISTS = 9
186
+ SETS = 10
187
+ HELLOS = 11
188
+
189
+ FIELDS = {
190
+ BOOLS => {:type => ::Thrift::Types::LIST, :name => 'bools', :element => {:type => ::Thrift::Types::BOOL}},
191
+ BYTES => {:type => ::Thrift::Types::LIST, :name => 'bytes', :element => {:type => ::Thrift::Types::BYTE}},
192
+ I16S => {:type => ::Thrift::Types::LIST, :name => 'i16s', :element => {:type => ::Thrift::Types::I16}},
193
+ I32S => {:type => ::Thrift::Types::LIST, :name => 'i32s', :element => {:type => ::Thrift::Types::I32}},
194
+ I64S => {:type => ::Thrift::Types::LIST, :name => 'i64s', :element => {:type => ::Thrift::Types::I64}},
195
+ DOUBLES => {:type => ::Thrift::Types::LIST, :name => 'doubles', :element => {:type => ::Thrift::Types::DOUBLE}},
196
+ STRINGS => {:type => ::Thrift::Types::LIST, :name => 'strings', :element => {:type => ::Thrift::Types::STRING}},
197
+ MAPS => {:type => ::Thrift::Types::LIST, :name => 'maps', :element => {:type => ::Thrift::Types::MAP, :key => {:type => ::Thrift::Types::I16}, :value => {:type => ::Thrift::Types::I16}}},
198
+ LISTS => {:type => ::Thrift::Types::LIST, :name => 'lists', :element => {:type => ::Thrift::Types::LIST, :element => {:type => ::Thrift::Types::I16}}},
199
+ SETS => {:type => ::Thrift::Types::LIST, :name => 'sets', :element => {:type => ::Thrift::Types::SET, :element => {:type => ::Thrift::Types::I16}}},
200
+ HELLOS => {:type => ::Thrift::Types::LIST, :name => 'hellos', :element => {:type => ::Thrift::Types::STRUCT, :class => SpecNamespace::Hello}}
201
+ }
202
+
203
+ def struct_fields; FIELDS; end
204
+
205
+ def validate
206
+ end
207
+
208
+ ::Thrift::Struct.generate_accessors self
209
+ end
210
+
211
+ class Xception < ::Thrift::Exception
212
+ include ::Thrift::Struct, ::Thrift::Struct_Union
213
+ MESSAGE = 1
214
+ CODE = 2
215
+
216
+ FIELDS = {
217
+ MESSAGE => {:type => ::Thrift::Types::STRING, :name => 'message'},
218
+ CODE => {:type => ::Thrift::Types::I32, :name => 'code', :default => 1}
219
+ }
220
+
221
+ def struct_fields; FIELDS; end
222
+
223
+ def validate
224
+ end
225
+
226
+ ::Thrift::Struct.generate_accessors self
227
+ end
228
+
229
+ class My_union < ::Thrift::Union
230
+ include ::Thrift::Struct_Union
231
+ class << self
232
+ def im_true(val)
233
+ My_union.new(:im_true, val)
234
+ end
235
+
236
+ def a_bite(val)
237
+ My_union.new(:a_bite, val)
238
+ end
239
+
240
+ def integer16(val)
241
+ My_union.new(:integer16, val)
242
+ end
243
+
244
+ def integer32(val)
245
+ My_union.new(:integer32, val)
246
+ end
247
+
248
+ def integer64(val)
249
+ My_union.new(:integer64, val)
250
+ end
251
+
252
+ def double_precision(val)
253
+ My_union.new(:double_precision, val)
254
+ end
255
+
256
+ def some_characters(val)
257
+ My_union.new(:some_characters, val)
258
+ end
259
+
260
+ def other_i32(val)
261
+ My_union.new(:other_i32, val)
262
+ end
263
+
264
+ def some_enum(val)
265
+ My_union.new(:some_enum, val)
266
+ end
267
+
268
+ def my_map(val)
269
+ My_union.new(:my_map, val)
270
+ end
271
+ end
272
+
273
+ IM_TRUE = 1
274
+ A_BITE = 2
275
+ INTEGER16 = 3
276
+ INTEGER32 = 4
277
+ INTEGER64 = 5
278
+ DOUBLE_PRECISION = 6
279
+ SOME_CHARACTERS = 7
280
+ OTHER_I32 = 8
281
+ SOME_ENUM = 9
282
+ MY_MAP = 10
283
+
284
+ FIELDS = {
285
+ IM_TRUE => {:type => ::Thrift::Types::BOOL, :name => 'im_true'},
286
+ A_BITE => {:type => ::Thrift::Types::BYTE, :name => 'a_bite'},
287
+ INTEGER16 => {:type => ::Thrift::Types::I16, :name => 'integer16'},
288
+ INTEGER32 => {:type => ::Thrift::Types::I32, :name => 'integer32'},
289
+ INTEGER64 => {:type => ::Thrift::Types::I64, :name => 'integer64'},
290
+ DOUBLE_PRECISION => {:type => ::Thrift::Types::DOUBLE, :name => 'double_precision'},
291
+ SOME_CHARACTERS => {:type => ::Thrift::Types::STRING, :name => 'some_characters'},
292
+ OTHER_I32 => {:type => ::Thrift::Types::I32, :name => 'other_i32'},
293
+ SOME_ENUM => {:type => ::Thrift::Types::I32, :name => 'some_enum', :enum_class => SpecNamespace::SomeEnum},
294
+ MY_MAP => {:type => ::Thrift::Types::MAP, :name => 'my_map', :key => {:type => ::Thrift::Types::I32, :enum_class => SpecNamespace::SomeEnum}, :value => {:type => ::Thrift::Types::LIST, :element => {:type => ::Thrift::Types::I32, :enum_class => SpecNamespace::SomeEnum}}}
295
+ }
296
+
297
+ def struct_fields; FIELDS; end
298
+
299
+ def validate
300
+ raise(StandardError, 'Union fields are not set.') if get_set_field.nil? || get_value.nil?
301
+ if get_set_field == :some_enum
302
+ raise ::Thrift::ProtocolException.new(::Thrift::ProtocolException::UNKNOWN, 'Invalid value of field some_enum!') unless SpecNamespace::SomeEnum::VALID_VALUES.include?(get_value)
303
+ end
304
+ end
305
+
306
+ ::Thrift::Union.generate_accessors self
307
+ end
308
+
309
+ class Struct_with_union
310
+ include ::Thrift::Struct, ::Thrift::Struct_Union
311
+ FUN_UNION = 1
312
+ INTEGER32 = 2
313
+ SOME_CHARACTERS = 3
314
+
315
+ FIELDS = {
316
+ FUN_UNION => {:type => ::Thrift::Types::STRUCT, :name => 'fun_union', :class => SpecNamespace::My_union},
317
+ INTEGER32 => {:type => ::Thrift::Types::I32, :name => 'integer32'},
318
+ SOME_CHARACTERS => {:type => ::Thrift::Types::STRING, :name => 'some_characters'}
319
+ }
320
+
321
+ def struct_fields; FIELDS; end
322
+
323
+ def validate
324
+ end
325
+
326
+ ::Thrift::Struct.generate_accessors self
327
+ end
328
+
329
+ class StructWithEnumMap
330
+ include ::Thrift::Struct, ::Thrift::Struct_Union
331
+ MY_MAP = 1
332
+
333
+ FIELDS = {
334
+ MY_MAP => {:type => ::Thrift::Types::MAP, :name => 'my_map', :key => {:type => ::Thrift::Types::I32, :enum_class => SpecNamespace::SomeEnum}, :value => {:type => ::Thrift::Types::LIST, :element => {:type => ::Thrift::Types::I32, :enum_class => SpecNamespace::SomeEnum}}}
335
+ }
336
+
337
+ def struct_fields; FIELDS; end
338
+
339
+ def validate
340
+ end
341
+
342
+ ::Thrift::Struct.generate_accessors self
343
+ end
344
+
345
+ end
@@ -0,0 +1,64 @@
1
+ #
2
+ # Licensed to the Apache Software Foundation (ASF) under one
3
+ # or more contributor license agreements. See the NOTICE file
4
+ # distributed with this work for additional information
5
+ # regarding copyright ownership. The ASF licenses this file
6
+ # to you under the Apache License, Version 2.0 (the
7
+ # "License"); you may not use this file except in compliance
8
+ # with the License. You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing,
13
+ # software distributed under the License is distributed on an
14
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
+ # KIND, either express or implied. See the License for the
16
+ # specific language governing permissions and limitations
17
+ # under the License.
18
+ #
19
+
20
+ require File.dirname(__FILE__) + '/spec_helper'
21
+
22
+ class ThriftHTTPClientTransportSpec < Spec::ExampleGroup
23
+ include Thrift
24
+
25
+ describe HTTPClientTransport do
26
+ before(:each) do
27
+ @client = HTTPClientTransport.new("http://my.domain.com/path/to/service?param=value")
28
+ end
29
+
30
+ it "should always be open" do
31
+ @client.should be_open
32
+ @client.close
33
+ @client.should be_open
34
+ end
35
+
36
+ it "should post via HTTP and return the results" do
37
+ @client.write "a test"
38
+ @client.write " frame"
39
+ Net::HTTP.should_receive(:new).with("my.domain.com", 80).and_return do
40
+ mock("Net::HTTP").tee do |http|
41
+ http.should_receive(:use_ssl=).with(false)
42
+ http.should_receive(:post).with("/path/to/service?param=value", "a test frame", {"Content-Type"=>"application/x-thrift"}).and_return([nil, "data"])
43
+ end
44
+ end
45
+ @client.flush
46
+ @client.read(10).should == "data"
47
+ end
48
+
49
+ it "should send custom headers if defined" do
50
+ @client.write "test"
51
+ custom_headers = {"Cookie" => "Foo"}
52
+ headers = {"Content-Type"=>"application/x-thrift"}.merge(custom_headers)
53
+
54
+ @client.add_headers(custom_headers)
55
+ Net::HTTP.should_receive(:new).with("my.domain.com", 80).and_return do
56
+ mock("Net::HTTP").tee do |http|
57
+ http.should_receive(:use_ssl=).with(false)
58
+ http.should_receive(:post).with("/path/to/service?param=value", "test", headers).and_return([nil, "data"])
59
+ end
60
+ end
61
+ @client.flush
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,117 @@
1
+ #
2
+ # Licensed to the Apache Software Foundation (ASF) under one
3
+ # or more contributor license agreements. See the NOTICE file
4
+ # distributed with this work for additional information
5
+ # regarding copyright ownership. The ASF licenses this file
6
+ # to you under the Apache License, Version 2.0 (the
7
+ # "License"); you may not use this file except in compliance
8
+ # with the License. You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing,
13
+ # software distributed under the License is distributed on an
14
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
+ # KIND, either express or implied. See the License for the
16
+ # specific language governing permissions and limitations
17
+ # under the License.
18
+ #
19
+
20
+ require File.dirname(__FILE__) + '/spec_helper'
21
+ require 'thrift/server/mongrel_http_server'
22
+
23
+ class ThriftHTTPServerSpec < Spec::ExampleGroup
24
+ include Thrift
25
+
26
+ Handler = MongrelHTTPServer::Handler
27
+
28
+ describe MongrelHTTPServer do
29
+ it "should have appropriate defaults" do
30
+ mock_factory = mock("BinaryProtocolFactory")
31
+ mock_proc = mock("Processor")
32
+ BinaryProtocolFactory.should_receive(:new).and_return(mock_factory)
33
+ Mongrel::HttpServer.should_receive(:new).with("0.0.0.0", 80).and_return do
34
+ mock("Mongrel::HttpServer").tee do |mock|
35
+ handler = mock("Handler")
36
+ Handler.should_receive(:new).with(mock_proc, mock_factory).and_return(handler)
37
+ mock.should_receive(:register).with("/", handler)
38
+ end
39
+ end
40
+ MongrelHTTPServer.new(mock_proc)
41
+ end
42
+
43
+ it "should understand :ip, :port, :path, and :protocol_factory" do
44
+ mock_proc = mock("Processor")
45
+ mock_factory = mock("ProtocolFactory")
46
+ Mongrel::HttpServer.should_receive(:new).with("1.2.3.4", 1234).and_return do
47
+ mock("Mongrel::HttpServer").tee do |mock|
48
+ handler = mock("Handler")
49
+ Handler.should_receive(:new).with(mock_proc, mock_factory).and_return(handler)
50
+ mock.should_receive(:register).with("/foo", handler)
51
+ end
52
+ end
53
+ MongrelHTTPServer.new(mock_proc, :ip => "1.2.3.4", :port => 1234, :path => "foo",
54
+ :protocol_factory => mock_factory)
55
+ end
56
+
57
+ it "should serve using Mongrel::HttpServer" do
58
+ BinaryProtocolFactory.stub!(:new)
59
+ Mongrel::HttpServer.should_receive(:new).and_return do
60
+ mock("Mongrel::HttpServer").tee do |mock|
61
+ Handler.stub!(:new)
62
+ mock.stub!(:register)
63
+ mock.should_receive(:run).and_return do
64
+ mock("Mongrel::HttpServer.run").tee do |runner|
65
+ runner.should_receive(:join)
66
+ end
67
+ end
68
+ end
69
+ end
70
+ MongrelHTTPServer.new(nil).serve
71
+ end
72
+ end
73
+
74
+ describe MongrelHTTPServer::Handler do
75
+ before(:each) do
76
+ @processor = mock("Processor")
77
+ @factory = mock("ProtocolFactory")
78
+ @handler = Handler.new(@processor, @factory)
79
+ end
80
+
81
+ it "should return 404 for non-POST requests" do
82
+ request = mock("request", :params => {"REQUEST_METHOD" => "GET"})
83
+ response = mock("response")
84
+ response.should_receive(:start).with(404)
85
+ response.should_not_receive(:start).with(200)
86
+ @handler.process(request, response)
87
+ end
88
+
89
+ it "should serve using application/x-thrift" do
90
+ request = mock("request", :params => {"REQUEST_METHOD" => "POST"}, :body => nil)
91
+ response = mock("response")
92
+ head = mock("head")
93
+ head.should_receive(:[]=).with("Content-Type", "application/x-thrift")
94
+ IOStreamTransport.stub!(:new)
95
+ @factory.stub!(:get_protocol)
96
+ @processor.stub!(:process)
97
+ response.should_receive(:start).with(200).and_yield(head, nil)
98
+ @handler.process(request, response)
99
+ end
100
+
101
+ it "should use the IOStreamTransport" do
102
+ body = mock("body")
103
+ request = mock("request", :params => {"REQUEST_METHOD" => "POST"}, :body => body)
104
+ response = mock("response")
105
+ head = mock("head")
106
+ head.stub!(:[]=)
107
+ out = mock("out")
108
+ protocol = mock("protocol")
109
+ transport = mock("transport")
110
+ IOStreamTransport.should_receive(:new).with(body, out).and_return(transport)
111
+ @factory.should_receive(:get_protocol).with(transport).and_return(protocol)
112
+ @processor.should_receive(:process).with(protocol, protocol)
113
+ response.should_receive(:start).with(200).and_yield(head, out)
114
+ @handler.process(request, response)
115
+ end
116
+ end
117
+ end