magellan-rails 0.1.5 → 0.2.0.pre
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/magellan/rails/request.rb +31 -10
- data/lib/magellan/rails/response.rb +2 -1
- data/lib/magellan/rails/version.rb +1 -1
- data/lib/magellan/subscriber/request.rb +17 -5
- data/lib/magellan/worker/core.rb +10 -1
- data/magellan-rails.gemspec +1 -0
- data/spec/magellan/rails/request_spec.rb +186 -49
- data/spec/magellan/rails/response_spec.rb +5 -4
- data/spec/magellan/subscriber/request_spec.rb +148 -0
- data/spec/magellan/worker/core_spec.rb +59 -0
- metadata +20 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 604d2665dd317c1f40f5e5d574bae59d5ffe7a6e
|
4
|
+
data.tar.gz: e68191c69d8f8cdd9a890bd9b9b9d7342496dd97
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a4edb4881afde675ad1fa51e1568ee60a93a7f2bf50b05d5a80a44e7d8a015220fba5baeb693c5e42c5d127e12d6330f01478a171beac989173bc561377227a9
|
7
|
+
data.tar.gz: e296de514f853ca985c84eed969340aee1da12975eba77840ba324aa1371ec24a3a448364ea6b6ff5487c6900bc3fbd982d3ff2ef575f886560edce32833a53e
|
@@ -22,10 +22,15 @@ class Magellan::Rails::Request
|
|
22
22
|
end
|
23
23
|
|
24
24
|
def parse_message(request_message)
|
25
|
-
#
|
25
|
+
# v2 からリクエストオブジェクトに "v": 2 のようにバージョンが追加されるようになりました。
|
26
|
+
@version = request_message["v"] || 1
|
27
|
+
@options = request_message['options']
|
26
28
|
# TRから送られるメッセージのoptionsの型が本来はHash型なのですが、
|
27
|
-
#
|
28
|
-
@options
|
29
|
+
# 過去に配列型になってしまっていたバージョンがあったためクラスもチェックします
|
30
|
+
unless @options.is_a?(Hash)
|
31
|
+
@options = {}
|
32
|
+
end
|
33
|
+
@env = request_message["env"] || {}
|
29
34
|
@headers = request_message['headers']
|
30
35
|
@body_encoding = request_message['body_encoding']
|
31
36
|
@body_encoding = (@body_encoding.present? and VALID_ENCODING.include?(@body_encoding)) ? @body_encoding.to_sym : :plain
|
@@ -35,8 +40,24 @@ class Magellan::Rails::Request
|
|
35
40
|
def to_rack_env
|
36
41
|
env = Hash.new
|
37
42
|
|
38
|
-
|
39
|
-
|
43
|
+
case @version
|
44
|
+
when 1
|
45
|
+
http_method = @headers["Method"]
|
46
|
+
path_info = @headers["Path-Info"]
|
47
|
+
query_string = @headers["Query-String"]
|
48
|
+
server_name = @headers["Server-Name"]
|
49
|
+
server_port = @headers['Server-Port'].to_s
|
50
|
+
when 2
|
51
|
+
http_method = @env["METHOD"]
|
52
|
+
path_info = @env["PATH_INFO"]
|
53
|
+
query_string = @env["QUERY_STRING"]
|
54
|
+
server_name = @env["SERVER_NAME"]
|
55
|
+
server_port = @env['SERVER_PORT'].to_s
|
56
|
+
else
|
57
|
+
raise "Unsupported request format version: #{@version}. Please update magellan-rails."
|
58
|
+
end
|
59
|
+
|
60
|
+
scripe_name = path_info[path_info.rindex('/') + 1..-1]
|
40
61
|
|
41
62
|
# Rackにクエリーストリングを渡す時は
|
42
63
|
query_string.slice!(0) if (query_string[0] == '?')
|
@@ -46,19 +67,19 @@ class Magellan::Rails::Request
|
|
46
67
|
env["CONTENT_LENGTH"] = @body.bytesize
|
47
68
|
env["CONTENT_TYPE"] = ct.dup if ct
|
48
69
|
env["GATEWAY_INTERFACE"] = "CGI/1.1"
|
49
|
-
env["PATH_INFO"] =
|
50
|
-
env["QUERY_STRING"] =
|
70
|
+
env["PATH_INFO"] = path_info # "/hello/index",
|
71
|
+
env["QUERY_STRING"] = query_string #
|
51
72
|
|
52
73
|
# TRから送られてきていないので一旦コメントアウト
|
53
74
|
# env["REMOTE_ADDR"] = @peeraddr[3]
|
54
75
|
# env["REMOTE_HOST"] = @peeraddr[2]
|
55
76
|
# env["REMOTE_USER"] = @user
|
56
77
|
|
57
|
-
env["REQUEST_METHOD"] =
|
78
|
+
env["REQUEST_METHOD"] = http_method # GET, PUT, DELETE...
|
58
79
|
env["REQUEST_URI"] = "#{env["PATH_INFO"]}?#{env["QUERY_STRING"]}"
|
59
80
|
env["SCRIPT_NAME"] = '' # /scripts/sample?a=c の場合 ルートなので ''
|
60
|
-
env["SERVER_NAME"] =
|
61
|
-
env["SERVER_PORT"] =
|
81
|
+
env["SERVER_NAME"] = server_name # "localhost"
|
82
|
+
env["SERVER_PORT"] = server_port # "3000"
|
62
83
|
|
63
84
|
# 一旦決め打ち
|
64
85
|
env["SERVER_PROTOCOL"] = "HTTP/1.1"
|
@@ -2,6 +2,7 @@
|
|
2
2
|
require 'magellan/rails'
|
3
3
|
|
4
4
|
require 'json'
|
5
|
+
require 'msgpack'
|
5
6
|
require 'base64'
|
6
7
|
|
7
8
|
class Magellan::Rails::Response
|
@@ -25,7 +26,7 @@ class Magellan::Rails::Response
|
|
25
26
|
status: @status,
|
26
27
|
body: @body,
|
27
28
|
body_encoding: @body_encoding.to_s,
|
28
|
-
}.
|
29
|
+
}.to_msgpack
|
29
30
|
rescue *json_generate_errors
|
30
31
|
@body = Base64.strict_encode64(@body)
|
31
32
|
@body_encoding = :base64
|
@@ -3,18 +3,30 @@ require 'magellan/rails'
|
|
3
3
|
require 'set'
|
4
4
|
|
5
5
|
class Magellan::Subscriber::Request
|
6
|
-
attr_reader :headers, :options, :topic, :body
|
6
|
+
attr_reader :env, :headers, :options, :topic, :body
|
7
7
|
|
8
8
|
def option(key)
|
9
9
|
options[key]
|
10
10
|
end
|
11
11
|
|
12
12
|
def parse_message(request_message)
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
@options = request_message['options']
|
14
|
+
@version = request_message["v"] || 1
|
15
|
+
# TRから送られるメッセージのoptionsの型が本来はHash型なのですが、
|
16
|
+
# 過去に配列になっていたバージョンがあったため、クラスをチェックします
|
17
|
+
@options = {} unless @options.is_a?(Hash)
|
18
|
+
@env = request_message["env"] || {}
|
16
19
|
@headers = request_message['headers']
|
17
|
-
|
20
|
+
|
21
|
+
case @version
|
22
|
+
when 1
|
23
|
+
@topic = @headers["Path-Info"]
|
24
|
+
when 2
|
25
|
+
@topic = @env["PATH_INFO"]
|
26
|
+
else
|
27
|
+
raise "Unsupported request format version: #{@version}. Please update magellan-rails."
|
28
|
+
end
|
29
|
+
|
18
30
|
case request_message["body_encoding"]
|
19
31
|
when /\Aplain\z/i, nil
|
20
32
|
@body = request_message["body"]
|
data/lib/magellan/worker/core.rb
CHANGED
@@ -4,6 +4,7 @@ require "magellan/worker"
|
|
4
4
|
require "magellan/extentions"
|
5
5
|
|
6
6
|
require 'json'
|
7
|
+
require 'msgpack'
|
7
8
|
require "bunny"
|
8
9
|
|
9
10
|
class Magellan::Worker::Core
|
@@ -75,7 +76,7 @@ class Magellan::Worker::Core
|
|
75
76
|
@channel.basic_ack(delivery_tag, false)
|
76
77
|
|
77
78
|
begin
|
78
|
-
request_message =
|
79
|
+
request_message = parse_body(payload)
|
79
80
|
@executor.execute(reply_to, correlation_id, delivery_tag, request_message)
|
80
81
|
rescue
|
81
82
|
Magellan.logger.error("Magellan Worker request execution error: #{$!}\n" + $@.join("\n"))
|
@@ -90,4 +91,12 @@ class Magellan::Worker::Core
|
|
90
91
|
Signal.trap(:TERM, @original_trap_handler)
|
91
92
|
end
|
92
93
|
end
|
94
|
+
|
95
|
+
def parse_body(payload)
|
96
|
+
begin
|
97
|
+
MessagePack.unpack(payload)
|
98
|
+
rescue MessagePack::UnpackError
|
99
|
+
JSON.parse(payload)
|
100
|
+
end
|
101
|
+
end
|
93
102
|
end
|
data/magellan-rails.gemspec
CHANGED
@@ -21,6 +21,7 @@ Gem::Specification.new do |spec|
|
|
21
21
|
spec.add_runtime_dependency "railties", '~> 4.1'
|
22
22
|
spec.add_runtime_dependency "bunny"
|
23
23
|
spec.add_runtime_dependency "json"
|
24
|
+
spec.add_runtime_dependency "msgpack", '~> 0.5.4'
|
24
25
|
|
25
26
|
spec.add_development_dependency "bundler", "~> 1.6"
|
26
27
|
spec.add_development_dependency "rspec"
|
@@ -5,9 +5,10 @@ require 'json'
|
|
5
5
|
|
6
6
|
describe Magellan::Rails::Request do
|
7
7
|
describe '#parse_message' do
|
8
|
-
|
9
|
-
|
10
|
-
|
8
|
+
context "v1" do
|
9
|
+
before(:all) do
|
10
|
+
@request_message_base = {
|
11
|
+
'headers' => {
|
11
12
|
'Title' => 'groovenauts-app',
|
12
13
|
'Interface-Version' => '1.0.0',
|
13
14
|
'Title-Runtime-Version'=> '1.0.0',
|
@@ -21,75 +22,211 @@ describe Magellan::Rails::Request do
|
|
21
22
|
"Path-Info" => "/hello/index",
|
22
23
|
"Query-String" => "qs1=abc&qs2=def"
|
23
24
|
},
|
24
|
-
|
25
|
-
|
26
|
-
|
25
|
+
'body' => '',
|
26
|
+
'body_encoding' => 'plain',
|
27
|
+
'options' => {
|
27
28
|
# 当分実装される予定がなく、空のハッシュが渡されます
|
28
29
|
# 'reply_ttl' => 1000
|
29
30
|
}
|
30
|
-
|
31
|
-
|
31
|
+
}
|
32
|
+
end
|
33
|
+
|
34
|
+
context "valid format" do
|
35
|
+
context "body_encoding=nil" do
|
36
|
+
let(:request){ Magellan::Rails::Request.new }
|
37
|
+
|
38
|
+
before do
|
39
|
+
@request_message = @request_message_base.dup
|
40
|
+
@request_message.delete("body_encoding")
|
41
|
+
end
|
42
|
+
|
43
|
+
subject{ request }
|
44
|
+
|
45
|
+
it do
|
46
|
+
request.parse_message(@request_message)
|
47
|
+
expect(request.headers).to eq(@request_message['headers'])
|
48
|
+
expect(request.body ).to eq(@request_message['body'])
|
49
|
+
expect(request.options).to eq(@request_message['options'])
|
50
|
+
|
51
|
+
expected_env = {
|
52
|
+
"CONTENT_LENGTH" => 0,
|
53
|
+
"CONTENT_TYPE" => "application/json",
|
54
|
+
"GATEWAY_INTERFACE" => "CGI/1.1",
|
55
|
+
"HTTP_CONTENT_TYPE" => "application/json",
|
56
|
+
"HTTP_INTERFACE_VERSION" => "1.0.0",
|
57
|
+
"HTTP_OAUTH_REQUESTER_ID" => "tarou",
|
58
|
+
"HTTP_REFERENCE_ID" => "192.168.1.110",
|
59
|
+
"HTTP_TITLE" => "groovenauts-app",
|
60
|
+
"HTTP_TITLE_RUNTIME_VERSION" => "1.0.0",
|
61
|
+
"HTTP_URL" => "http://magellan-clouds.com/worker/test",
|
62
|
+
"HTTP_VERSION" => "HTTP/1.1",
|
63
|
+
"PATH_INFO" => "/hello/index",
|
64
|
+
"QUERY_STRING" => "qs1=abc&qs2=def",
|
65
|
+
"REQUEST_METHOD" => "GET",
|
66
|
+
"REQUEST_PATH" => "/hello/index",
|
67
|
+
"REQUEST_URI" => "/hello/index?qs1=abc&qs2=def",
|
68
|
+
"SCRIPT_NAME" => "",
|
69
|
+
"SERVER_NAME" => "localhost",
|
70
|
+
"SERVER_PORT" => "3000",
|
71
|
+
"SERVER_PROTOCOL" => "HTTP/1.1",
|
72
|
+
"SERVER_SOFTWARE" => "magellan",
|
73
|
+
}
|
74
|
+
request.to_rack_env.tap do |e|
|
75
|
+
expected_env.each do |k, v|
|
76
|
+
expect(e[k]).to eql(v)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
context "body_encoding=plain" do
|
83
|
+
let(:request){ Magellan::Rails::Request.new }
|
32
84
|
|
33
|
-
|
34
|
-
|
35
|
-
|
85
|
+
before do
|
86
|
+
@request_message = @request_message_base.dup
|
87
|
+
end
|
36
88
|
|
37
|
-
|
38
|
-
|
39
|
-
|
89
|
+
subject{ request }
|
90
|
+
|
91
|
+
it do
|
92
|
+
request.parse_message(@request_message)
|
93
|
+
expect(request.headers).to eq(@request_message['headers'])
|
94
|
+
expect(request.body ).to eq(@request_message['body'])
|
95
|
+
expect(request.options).to eq(@request_message['options'])
|
96
|
+
end
|
40
97
|
end
|
41
98
|
|
42
|
-
|
99
|
+
context "body_encoding=base64" do
|
100
|
+
let(:request){ Magellan::Rails::Request.new }
|
101
|
+
|
102
|
+
before do
|
103
|
+
@request_body = "\xff"
|
104
|
+
@request_message = @request_message_base.dup
|
105
|
+
@request_message['body'] = Base64.strict_encode64(@request_body)
|
106
|
+
@request_message['body_encoding'] = 'base64'
|
107
|
+
end
|
108
|
+
|
109
|
+
subject{ request }
|
43
110
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
111
|
+
it do
|
112
|
+
request.parse_message(@request_message)
|
113
|
+
expect(request.headers).to eq(@request_message['headers'])
|
114
|
+
expect(request.body.inspect).to eq(@request_body.inspect)
|
115
|
+
expect(request.options).to eq(@request_message['options'])
|
116
|
+
end
|
49
117
|
end
|
50
118
|
end
|
51
119
|
|
52
|
-
context "
|
53
|
-
|
120
|
+
context "invalid format" do
|
121
|
+
it 'raise invalid format exception'
|
122
|
+
end
|
123
|
+
end
|
54
124
|
|
55
|
-
|
56
|
-
|
125
|
+
context "v2" do
|
126
|
+
before(:all) do
|
127
|
+
@request_message_base = {
|
128
|
+
"v" => 2,
|
129
|
+
"env" => {
|
130
|
+
"METHOD" => "GET",
|
131
|
+
"SERVER_NAME" => "localhost",
|
132
|
+
"SERVER_PORT" => 3000,
|
133
|
+
"PATH_INFO" => "/hello/index",
|
134
|
+
"QUERY_STRING" => "qs1=abc&qs2=def",
|
135
|
+
'REFERENCE_IP' => '192.168.1.110',
|
136
|
+
'URL' => 'http://magellan-clouds.com/worker/test',
|
137
|
+
'OAUTH_REQUESTER_ID' => 'tarou',
|
138
|
+
},
|
139
|
+
'headers' => {
|
140
|
+
'Content-Type' => 'application/json',
|
141
|
+
},
|
142
|
+
'body' => '',
|
143
|
+
'body_encoding' => 'plain',
|
144
|
+
'options' => {
|
145
|
+
# 当分実装される予定がなく、空のハッシュが渡されます
|
146
|
+
# 'reply_ttl' => 1000
|
147
|
+
}
|
148
|
+
}
|
149
|
+
end
|
150
|
+
|
151
|
+
context "valid format" do
|
152
|
+
context "body_encoding=nil" do
|
153
|
+
let(:request){ Magellan::Rails::Request.new }
|
154
|
+
|
155
|
+
before do
|
156
|
+
@request_message = @request_message_base.dup
|
157
|
+
@request_message.delete("body_encoding")
|
158
|
+
end
|
159
|
+
|
160
|
+
subject{ request }
|
161
|
+
|
162
|
+
it do
|
163
|
+
request.parse_message(@request_message)
|
164
|
+
expect(request.headers).to eq(@request_message['headers'])
|
165
|
+
expect(request.body ).to eq(@request_message['body'])
|
166
|
+
expect(request.options).to eq(@request_message['options'])
|
167
|
+
expected_env = {
|
168
|
+
"CONTENT_LENGTH" => 0,
|
169
|
+
"CONTENT_TYPE" => "application/json",
|
170
|
+
"GATEWAY_INTERFACE" => "CGI/1.1",
|
171
|
+
"HTTP_CONTENT_TYPE" => "application/json",
|
172
|
+
"HTTP_VERSION" => "HTTP/1.1",
|
173
|
+
"PATH_INFO" => "/hello/index",
|
174
|
+
"QUERY_STRING" => "qs1=abc&qs2=def",
|
175
|
+
"REQUEST_METHOD" => "GET",
|
176
|
+
"REQUEST_PATH" => "/hello/index",
|
177
|
+
"REQUEST_URI" => "/hello/index?qs1=abc&qs2=def",
|
178
|
+
"SCRIPT_NAME" => "",
|
179
|
+
"SERVER_NAME" => "localhost",
|
180
|
+
"SERVER_PORT" => "3000",
|
181
|
+
"SERVER_PROTOCOL" => "HTTP/1.1",
|
182
|
+
"SERVER_SOFTWARE" => "magellan",
|
183
|
+
}
|
184
|
+
request.to_rack_env.tap do |e|
|
185
|
+
expected_env.each do |k, v|
|
186
|
+
expect(e[k]).to eql(v)
|
187
|
+
end
|
188
|
+
end
|
189
|
+
end
|
57
190
|
end
|
58
191
|
|
59
|
-
|
192
|
+
context "body_encoding=plain" do
|
193
|
+
let(:request){ Magellan::Rails::Request.new }
|
60
194
|
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
expect(request.body ).to eq(@request_message['body'])
|
65
|
-
expect(request.options).to eq(@request_message['options'])
|
66
|
-
end
|
67
|
-
end
|
195
|
+
before do
|
196
|
+
@request_message = @request_message_base.dup
|
197
|
+
end
|
68
198
|
|
69
|
-
|
70
|
-
let(:request){ Magellan::Rails::Request.new }
|
199
|
+
subject{ request }
|
71
200
|
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
201
|
+
it do
|
202
|
+
request.parse_message(@request_message)
|
203
|
+
expect(request.headers).to eq(@request_message['headers'])
|
204
|
+
expect(request.body ).to eq(@request_message['body'])
|
205
|
+
expect(request.options).to eq(@request_message['options'])
|
206
|
+
end
|
77
207
|
end
|
78
208
|
|
79
|
-
|
209
|
+
context "body_encoding=base64" do
|
210
|
+
let(:request){ Magellan::Rails::Request.new }
|
211
|
+
|
212
|
+
before do
|
213
|
+
@request_body = "\xff"
|
214
|
+
@request_message = @request_message_base.dup
|
215
|
+
@request_message['body'] = Base64.strict_encode64(@request_body)
|
216
|
+
@request_message['body_encoding'] = 'base64'
|
217
|
+
end
|
218
|
+
|
219
|
+
subject{ request }
|
80
220
|
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
221
|
+
it do
|
222
|
+
request.parse_message(@request_message)
|
223
|
+
expect(request.headers).to eq(@request_message['headers'])
|
224
|
+
expect(request.body.inspect).to eq(@request_body.inspect)
|
225
|
+
expect(request.options).to eq(@request_message['options'])
|
226
|
+
end
|
86
227
|
end
|
87
228
|
end
|
88
229
|
end
|
89
|
-
|
90
|
-
context "invalid format" do
|
91
|
-
it 'raise invalid format exception'
|
92
|
-
end
|
93
230
|
end
|
94
231
|
|
95
232
|
describe '#option' do
|
@@ -2,6 +2,7 @@
|
|
2
2
|
|
3
3
|
require "spec_helper"
|
4
4
|
require 'json'
|
5
|
+
require 'msgpack'
|
5
6
|
require 'base64'
|
6
7
|
|
7
8
|
describe Magellan::Rails::Request do
|
@@ -30,7 +31,7 @@ describe Magellan::Rails::Request do
|
|
30
31
|
end
|
31
32
|
it do
|
32
33
|
response.parse_rack_response(@response_data)
|
33
|
-
data =
|
34
|
+
data = MessagePack.unpack(response.to_message)
|
34
35
|
expect(data["status"]).to eq(200)
|
35
36
|
expect(data["headers"]).to eq([])
|
36
37
|
expect(data["body"]).to eq("Hello! Magellan!")
|
@@ -47,11 +48,11 @@ describe Magellan::Rails::Request do
|
|
47
48
|
end
|
48
49
|
it do
|
49
50
|
response.parse_rack_response(@response_data)
|
50
|
-
data =
|
51
|
+
data = MessagePack.unpack(response.to_message)
|
51
52
|
expect(data["status"]).to eq(200)
|
52
53
|
expect(data["headers"]).to eq([])
|
53
|
-
expect(data["body"]).to eq(
|
54
|
-
expect(data["body_encoding"]).to eq("
|
54
|
+
expect(data["body"]).to eq(@response_body)
|
55
|
+
expect(data["body_encoding"]).to eq("plain")
|
55
56
|
end
|
56
57
|
end
|
57
58
|
end
|
@@ -0,0 +1,148 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
require "spec_helper"
|
4
|
+
require 'json'
|
5
|
+
|
6
|
+
describe Magellan::Subscriber::Request do
|
7
|
+
describe '#parse_message' do
|
8
|
+
context "v1" do
|
9
|
+
before(:all) do
|
10
|
+
@request_message_base = {
|
11
|
+
'headers' => {
|
12
|
+
'Method' => 'PUBLISH',
|
13
|
+
"Path-Info" => "hello/index",
|
14
|
+
},
|
15
|
+
'body' => 'body',
|
16
|
+
'body_encoding' => 'plain',
|
17
|
+
}
|
18
|
+
end
|
19
|
+
|
20
|
+
context "valid format" do
|
21
|
+
context "body_encoding=nil" do
|
22
|
+
let(:request){ Magellan::Subscriber::Request.new }
|
23
|
+
|
24
|
+
before do
|
25
|
+
@request_message = @request_message_base.dup
|
26
|
+
@request_message.delete("body_encoding")
|
27
|
+
end
|
28
|
+
|
29
|
+
subject{ request }
|
30
|
+
|
31
|
+
it do
|
32
|
+
request.parse_message(@request_message)
|
33
|
+
expect(request.topic).to eq("hello/index")
|
34
|
+
expect(request.body).to eq("body")
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
context "body_encoding=plain" do
|
39
|
+
let(:request){ Magellan::Subscriber::Request.new }
|
40
|
+
|
41
|
+
before do
|
42
|
+
@request_message = @request_message_base.dup
|
43
|
+
end
|
44
|
+
|
45
|
+
subject{ request }
|
46
|
+
|
47
|
+
it do
|
48
|
+
request.parse_message(@request_message)
|
49
|
+
expect(request.topic).to eq("hello/index")
|
50
|
+
expect(request.body).to eq("body")
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
context "body_encoding=base64" do
|
55
|
+
let(:request){ Magellan::Subscriber::Request.new }
|
56
|
+
|
57
|
+
before do
|
58
|
+
@request_body = "\xff"
|
59
|
+
@request_message = @request_message_base.dup
|
60
|
+
@request_message['body'] = Base64.strict_encode64(@request_body)
|
61
|
+
@request_message['body_encoding'] = 'base64'
|
62
|
+
end
|
63
|
+
|
64
|
+
subject{ request }
|
65
|
+
|
66
|
+
it do
|
67
|
+
request.parse_message(@request_message)
|
68
|
+
expect(request.topic).to eq("hello/index")
|
69
|
+
expect(request.body).to eq("\xFF".force_encoding("ASCII-8BIT"))
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
context "v2" do
|
76
|
+
before(:all) do
|
77
|
+
@request_message_base = {
|
78
|
+
"v" => 2,
|
79
|
+
'env' => {
|
80
|
+
'Method' => 'PUBLISH',
|
81
|
+
"PATH_INFO" => "hello/index",
|
82
|
+
},
|
83
|
+
'headers' => {},
|
84
|
+
'body' => 'body',
|
85
|
+
'body_encoding' => 'plain',
|
86
|
+
'options' => {
|
87
|
+
# 当分実装される予定がなく、空のハッシュが渡されます
|
88
|
+
# 'reply_ttl' => 1000
|
89
|
+
}
|
90
|
+
}
|
91
|
+
end
|
92
|
+
|
93
|
+
context "valid format" do
|
94
|
+
context "body_encoding=nil" do
|
95
|
+
let(:request){ Magellan::Subscriber::Request.new }
|
96
|
+
|
97
|
+
before do
|
98
|
+
@request_message = @request_message_base.dup
|
99
|
+
@request_message.delete("body_encoding")
|
100
|
+
end
|
101
|
+
|
102
|
+
subject{ request }
|
103
|
+
|
104
|
+
it do
|
105
|
+
request.parse_message(@request_message)
|
106
|
+
expect(request.topic).to eq("hello/index")
|
107
|
+
expect(request.body).to eq("body")
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
context "body_encoding=plain" do
|
112
|
+
let(:request){ Magellan::Subscriber::Request.new }
|
113
|
+
|
114
|
+
before do
|
115
|
+
@request_message = @request_message_base.dup
|
116
|
+
end
|
117
|
+
|
118
|
+
subject{ request }
|
119
|
+
|
120
|
+
it do
|
121
|
+
request.parse_message(@request_message)
|
122
|
+
expect(request.topic).to eq("hello/index")
|
123
|
+
expect(request.body).to eq("body")
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
context "body_encoding=base64" do
|
128
|
+
let(:request){ Magellan::Subscriber::Request.new }
|
129
|
+
|
130
|
+
before do
|
131
|
+
@request_body = "\xff"
|
132
|
+
@request_message = @request_message_base.dup
|
133
|
+
@request_message['body'] = Base64.strict_encode64(@request_body)
|
134
|
+
@request_message['body_encoding'] = 'base64'
|
135
|
+
end
|
136
|
+
|
137
|
+
subject{ request }
|
138
|
+
|
139
|
+
it do
|
140
|
+
request.parse_message(@request_message)
|
141
|
+
expect(request.topic).to eq("hello/index")
|
142
|
+
expect(request.body).to eq("\xFF".force_encoding("ASCII-8BIT"))
|
143
|
+
end
|
144
|
+
end
|
145
|
+
end
|
146
|
+
end
|
147
|
+
end
|
148
|
+
end
|
@@ -4,6 +4,7 @@ require "spec_helper"
|
|
4
4
|
require 'bunny'
|
5
5
|
require 'bunny_mock'
|
6
6
|
require 'json'
|
7
|
+
require 'msgpack'
|
7
8
|
|
8
9
|
describe Magellan::Worker::Core do
|
9
10
|
|
@@ -65,6 +66,64 @@ describe Magellan::Worker::Core do
|
|
65
66
|
subject.run
|
66
67
|
expect(Magellan.logger ).to be_a(Logger)
|
67
68
|
end
|
69
|
+
end
|
70
|
+
|
71
|
+
context "with messagepack" do
|
72
|
+
before do
|
73
|
+
# テスト用のワーカー実行要求用メッセージ
|
74
|
+
@request_message = {
|
75
|
+
'headers' => {
|
76
|
+
'Title' => 'groovenauts-app',
|
77
|
+
'Interface-Version' => '1.0.0',
|
78
|
+
'Title-Runtime-Version'=> '1.0.0',
|
79
|
+
'Reference-Id' =>'192.168.1.110',
|
80
|
+
'Url' =>'http://magellan-clouds.com/worker/test',
|
81
|
+
'Oauth-Requester-Id' =>'tarou',
|
82
|
+
'Content-Type' =>'application/json',
|
83
|
+
},
|
84
|
+
'body' => '',
|
85
|
+
'options' => {
|
86
|
+
'reply_ttl' => 1000
|
87
|
+
}
|
88
|
+
}
|
89
|
+
|
90
|
+
bunny = BunnyMock.new
|
91
|
+
channel = bunny.create_channel
|
92
|
+
exchange = bunny.exchange('test_exchange', type: :direct, durable: false, auto_delete: false)
|
93
|
+
queue = channel.queue("consumer1.sample_project.1.0.0.rails")
|
94
|
+
queue.bind(exchange)
|
95
|
+
exchange.publish(@request_message.to_msgpack, routing_key: "1.0.0.rails", reply_to: 'reply_test', correlation_id: "correlation_id_sample")
|
96
|
+
@work_pool = double "work_pool"
|
97
|
+
|
98
|
+
allow(Bunny ).to receive(:new).and_return(bunny)
|
99
|
+
allow(bunny ).to receive(:create_channel).and_return(channel)
|
100
|
+
allow(channel).to receive(:queue).with('customer1.sample_project.1.0.0.rails', no_declare: true).and_return(queue)
|
101
|
+
allow(channel).to receive(:basic_ack).and_return(queue)
|
102
|
+
allow(channel).to receive(:work_pool).and_return(@work_pool)
|
103
|
+
|
104
|
+
config = {}
|
105
|
+
config[:host] = '127.0.0.1'
|
106
|
+
config[:port] = '5672'
|
107
|
+
config[:vhost] = '/customer1.sample_project'
|
108
|
+
config[:rabbitmq_user] = 'customer1.sample_project'
|
109
|
+
config[:request_queue] = 'customer1.sample_project.1.0.0.rails'
|
110
|
+
config[:response_exchange] = 'customer1.sample_project.reply'
|
111
|
+
config[:rabbitmq_password] = 'pswd1'
|
112
|
+
|
113
|
+
allow(Magellan::Worker::Config).to receive(:load_config).and_return(config)
|
114
|
+
|
115
|
+
@executor = double('executor')
|
116
|
+
allow(Magellan::Worker::Executor).to receive(:new).and_return(@executor)
|
117
|
+
allow(@executor).to receive(:execute).and_return(nil)
|
118
|
+
end
|
119
|
+
|
120
|
+
subject{ Magellan::Worker::Core.new.initialize! }
|
121
|
+
it do
|
122
|
+
expect(Magellan::Worker::Config).to receive(:load_config).once
|
123
|
+
expect(@executor ).to receive(:execute).with('reply_test', 'correlation_id_sample', '2', @request_message).once
|
124
|
+
subject.run
|
125
|
+
expect(Magellan.logger ).to be_a(Logger)
|
126
|
+
end
|
68
127
|
|
69
128
|
describe "gracefully shutdown" do
|
70
129
|
before do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: magellan-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0.pre
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yuuki Noguchi
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-02-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: railties
|
@@ -52,6 +52,20 @@ dependencies:
|
|
52
52
|
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: msgpack
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 0.5.4
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 0.5.4
|
55
69
|
- !ruby/object:Gem::Dependency
|
56
70
|
name: bundler
|
57
71
|
requirement: !ruby/object:Gem::Requirement
|
@@ -131,6 +145,7 @@ files:
|
|
131
145
|
- spec/magellan/rails/request_spec.rb
|
132
146
|
- spec/magellan/rails/response_spec.rb
|
133
147
|
- spec/magellan/subscriber/mapper_spec.rb
|
148
|
+
- spec/magellan/subscriber/request_spec.rb
|
134
149
|
- spec/magellan/worker/core_spec.rb
|
135
150
|
- spec/spec_helper.rb
|
136
151
|
homepage: ''
|
@@ -148,9 +163,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
148
163
|
version: '0'
|
149
164
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
150
165
|
requirements:
|
151
|
-
- - "
|
166
|
+
- - ">"
|
152
167
|
- !ruby/object:Gem::Version
|
153
|
-
version:
|
168
|
+
version: 1.3.1
|
154
169
|
requirements: []
|
155
170
|
rubyforge_project:
|
156
171
|
rubygems_version: 2.4.5
|
@@ -161,5 +176,6 @@ test_files:
|
|
161
176
|
- spec/magellan/rails/request_spec.rb
|
162
177
|
- spec/magellan/rails/response_spec.rb
|
163
178
|
- spec/magellan/subscriber/mapper_spec.rb
|
179
|
+
- spec/magellan/subscriber/request_spec.rb
|
164
180
|
- spec/magellan/worker/core_spec.rb
|
165
181
|
- spec/spec_helper.rb
|