json_proxy 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,137 @@
1
+ require 'rubygems'
2
+ require 'net/http'
3
+ require 'active_support'
4
+
5
+ require File.dirname(__FILE__) + '/spec_helper'
6
+ require File.dirname(__FILE__) + '/test_service.rb'
7
+
8
+
9
+
10
+ # Test basic server operation using echo server
11
+
12
+ describe "Server" do
13
+
14
+ def start_server
15
+ app = Server::Server.new
16
+ Thread.new {
17
+ @acc = Rack::Handler::Mongrel.run(app, :Port => 4567) do |server|
18
+ puts "Server running"
19
+ end
20
+ }
21
+ sleep 2
22
+
23
+ end
24
+
25
+ def start_worker
26
+ Thread.new {
27
+ puts "Worker Started"
28
+ worker = UrlQueue::QueueDaemon.new
29
+ sleeping = false
30
+ loop do
31
+ processed = worker.process
32
+ if processed == 0 && !sleeping
33
+ sleeping = true
34
+ puts "Queue empty \n"
35
+ elsif processed > 0
36
+ puts "Processed #{processed}\n"
37
+ sleeping = false
38
+ end
39
+ sleep(1)
40
+ end
41
+ }
42
+ sleep 1
43
+ end
44
+
45
+ before(:all) do
46
+ start_server
47
+ start_worker
48
+ end
49
+
50
+
51
+ before(:each) do
52
+ end
53
+
54
+ def get(msg)
55
+ Net::HTTP.get_response(URI.parse('http://localhost:4567/echo/echo.js?message='+msg))
56
+ end
57
+
58
+ def force_get(msg)
59
+ Net::HTTP.get_response(URI.parse('http://localhost:4567/echo/echo.js?message='+msg+'&force=true'))
60
+ end
61
+
62
+ def random_msg
63
+ "random-#{rand(9999999)}"
64
+ end
65
+
66
+ describe "with missing service" do
67
+ it "response should be 404" do
68
+ response = Net::HTTP.get_response(URI.parse('http://localhost:4567/missing'))
69
+ response.code.should =="200"
70
+ response.body.should =="Handler not found"
71
+ end
72
+ end
73
+
74
+ describe "with echo service" do
75
+
76
+ describe "with cached message" do
77
+ before(:each) do
78
+ force_get "cached"
79
+ end
80
+
81
+ it "response should == message " do
82
+ response = get("cached")
83
+ response.code.should == "200"
84
+ body = ActiveSupport::JSON.decode(response.body)
85
+ body.should be :kind_of, Hash
86
+ body['status'].should == 200
87
+ body['data'].should be :kind_of, Hash
88
+ body['data']['message'].should == "cached"
89
+ end
90
+
91
+ end
92
+
93
+ describe "with uncached message" do
94
+
95
+ it "response should be 202 - Processing until complete then success" do
96
+ msg = random_msg
97
+ response = get(msg)
98
+ response.code.should == "202"
99
+ response.body.should_not be(:empty)
100
+ body = ActiveSupport::JSON.decode(response.body)
101
+ body.should be :kind_of, Hash
102
+ body['status'].should == 202
103
+ count = 1;
104
+ maxCount = 10;
105
+ while response = get(msg)
106
+ puts "Retry #{count} \n"
107
+ if (response.code != "202" || count > maxCount)
108
+ break
109
+ end
110
+ count += 1
111
+ sleep(1)
112
+ end
113
+ response.code.should == "200"
114
+ body = ActiveSupport::JSON.decode(response.body)
115
+ body['status'].should == 200
116
+ body['data'].should be :kind_of, Hash
117
+ body['data']['message'].should == msg
118
+ end
119
+
120
+ end
121
+
122
+ describe "with forced query" do
123
+
124
+ it "response should == message " do
125
+ msg = random_msg
126
+ response = force_get(msg)
127
+ response.code.should == "200"
128
+ body = ActiveSupport::JSON.decode(response.body)
129
+ body['status'].should == 200
130
+ body['data'].should be :kind_of, Hash
131
+ body['data']['message'].should == msg
132
+ end
133
+
134
+ end
135
+
136
+ end
137
+ end
@@ -0,0 +1,14 @@
1
+
2
+ require 'rubygems'
3
+ require 'spec'
4
+ require 'configatron'
5
+
6
+
7
+ $:.unshift(File.dirname(__FILE__) + "/../../lib")
8
+ require File.dirname(__FILE__) + '/../../lib/json_proxy.rb'
9
+
10
+ require File.dirname(__FILE__) + '/../../lib/queue/queue.rb'
11
+ require File.dirname(__FILE__) + '/../../lib/queue/queue_daemon.rb'
12
+
13
+
14
+
@@ -0,0 +1,16 @@
1
+
2
+ require 'rubygems'
3
+ require 'spec'
4
+ require 'configatron'
5
+
6
+
7
+ $:.unshift(File.dirname(__FILE__) + "/../../lib")
8
+ require File.dirname(__FILE__) + '/../../lib/json_proxy.rb'
9
+
10
+ require File.dirname(__FILE__) + '/../../lib/queue/queue.rb'
11
+ require File.dirname(__FILE__) + '/../../lib/queue/queue_daemon.rb'
12
+
13
+
14
+ include Server::DSL
15
+ require File.dirname(__FILE__) + '/../../lib/webservices/echo.rb'
16
+
@@ -0,0 +1,17 @@
1
+
2
+
3
+ # Not needed but the test should be a little more robust this way
4
+ class EchoResponse
5
+ attr_accessor :message
6
+
7
+ def initialize(msg)
8
+ @message = msg
9
+ end
10
+ end
11
+
12
+ np_namespace "echo" do |ns|
13
+
14
+ ns.route 'echo', [:message] do |message|
15
+ EchoResponse.new message
16
+ end
17
+ end
File without changes
@@ -0,0 +1,206 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+
4
+ describe "Cache Handler#action" do
5
+
6
+
7
+ before(:each) do
8
+ @name = 'name'
9
+ @namespace = 'namespace'
10
+ @options = mock("options")
11
+ @options.stub!('[]').with(:cache_key).and_return(Proc.new { "key" })
12
+ @handler = Server::Handlers::CacheHandler.new @namespace, @name, @options
13
+
14
+ @cache = mock("cache")
15
+ @cache.stub!(:fetch)
16
+ Utils::CouchCache.stub!(:new).and_return(@cache)
17
+
18
+ @request = mock("request")
19
+ @request.stub!(:args)
20
+ @request.stub!(:force?)
21
+ @response = mock("response")
22
+
23
+ @block_body = mock("block_body")
24
+ @block_body.stub!(:yielded)
25
+ end
26
+
27
+ def action
28
+ @handler.action @request, @response do |request, response|
29
+ @block_body.yielded request, response
30
+ end
31
+ end
32
+
33
+ it "should initialise cache" do
34
+ Utils::CouchCache.should_receive(:new).with("#{@namespace.downcase}_#{@name.downcase}")
35
+ action
36
+ end
37
+
38
+ it "should raise exception if cache_key is not a proc" do
39
+ cache_key = mock("cache_key")
40
+ @options.stub!('[]').with(:cache_key).and_return(cache_key)
41
+ cache_key.should_receive(:is_a?).with(Proc).and_return(false)
42
+
43
+ lambda { action }.should raise_error
44
+
45
+ end
46
+
47
+ it "should apply the cache_key function to the request args" do
48
+ args = mock("args")
49
+ cache_key_body = mock("cache_key_body")
50
+ cache_key = Proc.new { |args| cache_key_body.yielded args }
51
+
52
+ @options.stub!('[]').with(:cache_key).and_return(cache_key)
53
+ cache_key_body.should_receive(:yielded).with(args)
54
+ @request.stub!(:args).and_return(args)
55
+
56
+ action
57
+ end
58
+
59
+ it "should fetch the result from the cache" do
60
+ @cache.should_receive(:fetch).with("key")
61
+
62
+ action
63
+ end
64
+
65
+ describe ", cache miss" do
66
+ before(:each) do
67
+ @cache.stub!(:fetch).and_return(nil)
68
+ end
69
+
70
+ it "should yield" do
71
+ @block_body.should_receive(:yielded).with(@request, @response)
72
+
73
+ action
74
+ end
75
+
76
+ it "should check if the request is forced" do
77
+ @request.should_receive(:force?)
78
+
79
+ action
80
+ end
81
+
82
+ describe "with unforced request" do
83
+ before(:each) do
84
+ @request.stub!(:force?).and_return(false)
85
+ end
86
+
87
+ it "should not store result" do
88
+ @cache.should_not_receive(:store)
89
+ action
90
+ end
91
+
92
+ end
93
+
94
+ describe "with forced request" do
95
+ before(:each) do
96
+ @request.stub!(:force?).and_return(true)
97
+ end
98
+
99
+ it "should store result" do
100
+ body = {}
101
+ @response.should_receive(:body).and_return(body)
102
+
103
+ @cache.should_receive(:store).with("key", anything()) do |key , json|
104
+ cacheObj = JSON.parse json
105
+ cacheObj['mtime'].should be_kind_of(Numeric)
106
+ cacheObj['ctime'].should be_kind_of(Numeric)
107
+ cacheObj['app_version'].should be(JsonProxy::APP_VERSION)
108
+ end
109
+ action
110
+ end
111
+ end
112
+ end
113
+
114
+ describe ", cache hit" do
115
+ before(:each) do
116
+ @cacheObj = mock("cacheObj")
117
+ @cacheObj.stub!('[]').with('data')
118
+ @cacheObj.stub!('[]').with('mtime')
119
+
120
+ @handler.stub!(:expired?).and_return(false)
121
+ @cache.stub!(:fetch).and_return("cacheJSON")
122
+
123
+ JSON.stub!(:parse).with("cacheJSON").and_return(@cacheObj)
124
+
125
+ @response.stub!(:body=)
126
+ end
127
+
128
+ it "should parse JSON result" do
129
+ JSON.should_receive(:parse).with("cacheJSON")
130
+ action
131
+ end
132
+
133
+ it "should check expiry" do
134
+
135
+ @handler.should_receive(:expired?).with(@cacheObj)
136
+ action
137
+ end
138
+
139
+ describe "with unexpired cached result" do
140
+ before(:each) do
141
+ @handler.stub!(:expired?).and_return(false)
142
+ end
143
+
144
+ it "should set the response" do
145
+ @cacheObj.should_receive('[]').with('data').and_return(:data)
146
+ @response.should_receive(:body=).with(:data)
147
+ action
148
+ end
149
+
150
+ it "should not yield" do
151
+ @block_body.should_not_receive(:yielded)
152
+ action
153
+ end
154
+
155
+ end
156
+
157
+ describe "with expired cache result" do
158
+ before(:each) do
159
+ @handler.stub!(:expired?).and_return(true)
160
+ end
161
+
162
+ it "should yield" do
163
+ @block_body.should_receive(:yielded).with(@request, @response)
164
+ action
165
+ end
166
+
167
+ describe "with unforced request" do
168
+
169
+ before(:each) do
170
+ @request.stub!(:force?).and_return(false)
171
+ end
172
+
173
+ it "should not store result" do
174
+ @cache.should_not_receive(:store)
175
+ action
176
+ end
177
+ end
178
+
179
+ describe "with forced request" do
180
+
181
+ before(:each) do
182
+ @request.stub!(:force?).and_return(true)
183
+ end
184
+
185
+ it "should store result" do
186
+ body = {}
187
+ @response.should_receive(:body).and_return(body)
188
+ @cacheObj.should_receive('[]').with('ctime').and_return("ctime")
189
+ @cacheObj.should_receive('[]').with('_id').and_return("id")
190
+ @cacheObj.should_receive('[]').with('_rev').and_return("rev")
191
+
192
+ @cache.should_receive(:store).with("key", anything()) do |key , json|
193
+ cacheObj = JSON::Parser.new(json).parse #JSON.parse is stubbed
194
+ cacheObj['ctime'].should =="ctime"
195
+ cacheObj['mtime'].should be_kind_of(Numeric)
196
+ cacheObj['app_version'].should be(JsonProxy::APP_VERSION)
197
+ cacheObj['_rev'].should =="rev"
198
+ cacheObj['_id'].should =="id"
199
+ end
200
+ action
201
+ end
202
+ end
203
+ end
204
+
205
+ end
206
+ end
@@ -0,0 +1,206 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+
4
+ describe "Cache Handler#action" do
5
+
6
+
7
+ before(:each) do
8
+ @name = 'name'
9
+ @namespace = 'namespace'
10
+ @options = mock("options")
11
+ @options.stub!('[]').with(:cache_key).and_return(Proc.new { "key" })
12
+ @handler = Server::Handlers::CacheHandler.new @namespace, @name, @options
13
+
14
+ @cache = mock("cache")
15
+ @cache.stub!(:fetch)
16
+ Utils::CouchCache.stub!(:new).and_return(@cache)
17
+
18
+ @request = mock("request")
19
+ @request.stub!(:args)
20
+ @request.stub!(:force?)
21
+ @response = mock("response")
22
+
23
+ @block_body = mock("block_body")
24
+ @block_body.stub!(:yielded)
25
+ end
26
+
27
+ def action
28
+ @handler.action @request, @response do |request, response|
29
+ @block_body.yielded request, response
30
+ end
31
+ end
32
+
33
+ it "should initialise cache" do
34
+ Utils::CouchCache.should_receive(:new).with("#{@namespace.downcase}_#{@name.downcase}")
35
+ action
36
+ end
37
+
38
+ it "should raise exception if cache_key is not a proc" do
39
+ cache_key = mock("cache_key")
40
+ @options.stub!('[]').with(:cache_key).and_return(cache_key)
41
+ cache_key.should_receive(:is_a?).with(Proc).and_return(false)
42
+
43
+ lambda { action }.should raise_error
44
+
45
+ end
46
+
47
+ it "should apply the cache_key function to the request args" do
48
+ args = mock("args")
49
+ cache_key_body = mock("cache_key_body")
50
+ cache_key = Proc.new { |args| cache_key_body.yielded args }
51
+
52
+ @options.stub!('[]').with(:cache_key).and_return(cache_key)
53
+ cache_key_body.should_receive(:yielded).with(args)
54
+ @request.stub!(:args).and_return(args)
55
+
56
+ action
57
+ end
58
+
59
+ it "should fetch the result from the cache" do
60
+ @cache.should_receive(:fetch).with("key")
61
+
62
+ action
63
+ end
64
+
65
+ describe "with no cache result" do
66
+ before(:each) do
67
+ @cache.stub!(:fetch).and_return(nil)
68
+ end
69
+
70
+ it "should yield" do
71
+ @block_body.should_receive(:yielded).with(@request, @response)
72
+
73
+ action
74
+ end
75
+
76
+ it "should check if the request is forced" do
77
+ @request.should_receive(:force?)
78
+
79
+ action
80
+ end
81
+
82
+ describe "with unforced request" do
83
+ before(:each) do
84
+ @request.stub!(:force?).and_return(false)
85
+ end
86
+
87
+ it "should not store result" do
88
+ @cache.should_not_receive(:store)
89
+ action
90
+ end
91
+
92
+ end
93
+
94
+ describe "with forced request" do
95
+ before(:each) do
96
+ @request.stub!(:force?).and_return(true)
97
+ end
98
+
99
+ it "should store result" do
100
+ body = {}
101
+ @response.should_receive(:body).and_return(body)
102
+
103
+ @cache.should_receive(:store).with("key", anything()) do |key , json|
104
+ cacheObj = JSON.parse json
105
+ cacheObj['mtime'].should be_kind_of(Numeric)
106
+ cacheObj['ctime'].should be_kind_of(Numeric)
107
+ cacheObj['app_version'].should be(JsonProxy::APP_VERSION)
108
+ end
109
+ action
110
+ end
111
+ end
112
+ end
113
+
114
+ describe ", cache hit" do
115
+ before(:each) do
116
+ @cacheObj = mock("cacheObj")
117
+ @cacheObj.stub!('[]').with('data')
118
+ @cacheObj.stub!('[]').with('mtime')
119
+
120
+ @handler.stub!(:expired?).and_return(false)
121
+ @cache.stub!(:fetch).and_return("cacheJSON")
122
+
123
+ JSON.stub!(:parse).with("cacheJSON").and_return(@cacheObj)
124
+
125
+ @response.stub!(:body=)
126
+ end
127
+
128
+ it "should parse JSON result" do
129
+ JSON.should_receive(:parse).with("cacheJSON")
130
+ action
131
+ end
132
+
133
+ it "should check expiry" do
134
+
135
+ @handler.should_receive(:expired?).with(@cacheObj)
136
+ action
137
+ end
138
+
139
+ describe "with unexpired cached result" do
140
+ before(:each) do
141
+ @handler.stub!(:expired?).and_return(false)
142
+ end
143
+
144
+ it "should set the response" do
145
+ @cacheObj.should_receive('[]').with('data').and_return(:data)
146
+ @response.should_receive(:body=).with(:data)
147
+ action
148
+ end
149
+
150
+ it "should not yield" do
151
+ @block_body.should_not_receive(:yielded)
152
+ action
153
+ end
154
+
155
+ end
156
+
157
+ describe "with expired cache result" do
158
+ before(:each) do
159
+ @handler.stub!(:expired?).and_return(true)
160
+ end
161
+
162
+ it "should yield" do
163
+ @block_body.should_receive(:yielded).with(@request, @response)
164
+ action
165
+ end
166
+
167
+ describe "with unforced request" do
168
+
169
+ before(:each) do
170
+ @request.stub!(:force?).and_return(false)
171
+ end
172
+
173
+ it "should not store result" do
174
+ @cache.should_not_receive(:store)
175
+ action
176
+ end
177
+ end
178
+
179
+ describe "with forced request" do
180
+
181
+ before(:each) do
182
+ @request.stub!(:force?).and_return(true)
183
+ end
184
+
185
+ it "should store result" do
186
+ body = {}
187
+ @response.should_receive(:body).and_return(body)
188
+ @cacheObj.should_receive('[]').with('ctime').and_return("ctime")
189
+ @cacheObj.should_receive('[]').with('_id').and_return("id")
190
+ @cacheObj.should_receive('[]').with('_rev').and_return("rev")
191
+
192
+ @cache.should_receive(:store).with("key", anything()) do |key , json|
193
+ cacheObj = JSON::Parser.new(json).parse #JSON.parse is stubbed
194
+ cacheObj['ctime'].should =="ctime"
195
+ cacheObj['mtime'].should be_kind_of(Numeric)
196
+ cacheObj['app_version'].should be(JsonProxy::APP_VERSION)
197
+ cacheObj['_rev'].should =="rev"
198
+ cacheObj['_id'].should =="id"
199
+ end
200
+ action
201
+ end
202
+ end
203
+ end
204
+
205
+ end
206
+ end