dot_net_services 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,161 @@
1
+ require "#{File.dirname(__FILE__)}/../../spec_helper"
2
+
3
+ module DotNetServices
4
+ describe MessageBuffer do
5
+ describe "initialize(name, auth_data)" do
6
+ it "should create a session" do
7
+ Session.should_receive(:new).with('foo', :username => 'bar', :password => 'baz').and_return(:session)
8
+ mb = MessageBuffer.new('foo', :username => 'bar', :password => 'baz')
9
+ mb.session.should == :session
10
+ end
11
+
12
+ it "should be possible to specify polling_interval"
13
+ end
14
+
15
+ describe "register" do
16
+ it "should register a message buffer" do
17
+ response = Net::HTTPCreated.new(nil, nil, nil)
18
+
19
+ mb = MessageBuffer.new('foo', :username => 'bar', :password => 'baz')
20
+ mb.session.should_receive(:createmb).and_return(response)
21
+ mb.register.should equal(mb)
22
+ end
23
+
24
+ it "should blow up if the response code is not 201 and the buffer cannot be queried" do
25
+ create_response = Net::HTTPInternalServerError.new(nil, nil, nil)
26
+ query_response = Net::HTTPNotFound.new(nil, nil, nil)
27
+
28
+ mb = MessageBuffer.new('foo', :username => 'bar', :password => 'baz')
29
+ mb.session.should_receive(:createmb).and_return(create_response)
30
+ mb.session.should_receive(:get_from_relay).and_return(query_response)
31
+ proc { mb.register }.should raise_error(
32
+ RuntimeError, "Creating VMB failed. Service responded with Net::HTTPInternalServerError")
33
+ end
34
+
35
+ it "should not blow up if the response code to CREATEMB is not 201, byt the buffer can be queried" do
36
+ create_response = Net::HTTPInternalServerError.new(nil, nil, nil)
37
+ query_response = Net::HTTPSuccess.new(nil, nil, nil)
38
+
39
+ mb = MessageBuffer.new('foo', :username => 'bar', :password => 'baz')
40
+ mb.session.should_receive(:createmb).and_return(create_response)
41
+ mb.session.should_receive(:get_from_relay).and_return(query_response)
42
+ proc { mb.register }.should_not raise_error(
43
+ RuntimeError, "Creating VMB failed. Service responded with Net::HTTPInternalServerError")
44
+ end
45
+
46
+ end
47
+
48
+ describe "open(&block)" do
49
+ it "should register the buffer if necessary" do
50
+ block = lambda { :foo }
51
+ mb = MessageBuffer.new('foo', :username => 'bar', :password => 'baz')
52
+ mb.session.should_receive(:get_from_relay).and_return(Net::HTTPNotFound.new(nil, nil, nil))
53
+ mb.should_receive(:register).and_return(mb)
54
+ mb.open(&block)
55
+ end
56
+ end
57
+
58
+ describe "poll" do
59
+ it "should retrieve a single message from the buffer" do
60
+ response = Net::HTTPOK.new(nil, nil, nil)
61
+
62
+ mb = MessageBuffer.new('foo', :username => 'bar', :password => 'baz')
63
+ mb.session.should_receive(:retrieve).with(:encoding => 'asreply', :timeout => nil).and_return(response)
64
+
65
+ message = mb.poll
66
+
67
+ message.should == response
68
+ end
69
+
70
+ it "should return nil if no no message was retrieved in the buffer" do
71
+ response = Net::HTTPNoContent.new(nil, nil, nil)
72
+
73
+ mb = MessageBuffer.new('foo', :username => 'bar', :password => 'baz')
74
+ mb.session.should_receive(:retrieve).and_return(response)
75
+
76
+ mb.poll.should be_nil
77
+ end
78
+
79
+ it "should recreate a buffer if the bus says it doesn't exist and poll again" do
80
+ not_found_response = Net::HTTPNotFound.new(nil, nil, nil)
81
+ created_response = Net::HTTPCreated.new(nil, nil, nil)
82
+
83
+ mb = MessageBuffer.new('foo', :username => 'bar', :password => 'baz')
84
+
85
+ mb.session.should_receive(:retrieve).with(:encoding => 'asreply', :timeout => nil).and_return(not_found_response)
86
+ mb.session.should_receive(:createmb).and_return(created_response)
87
+
88
+ message = mb.poll
89
+
90
+ message.should == nil
91
+ end
92
+
93
+ end
94
+
95
+ describe "delete()" do
96
+ it "should send a DELETE request to the relay" do
97
+ mb = MessageBuffer.new('foo', :username => 'bar', :password => 'baz')
98
+ response = Net::HTTPNoContent.new(nil, nil, nil)
99
+
100
+ mb.session.should_receive(:delete).and_return(response)
101
+
102
+ mb.delete.should == mb
103
+ end
104
+
105
+ it "should do nothing if the response is NotFound (i.e., the VMB already doesn't exist"
106
+
107
+ it "should blow up if response is not NoContent, and not NotFound" do
108
+ mb = MessageBuffer.new('foo', :username => 'bar', :password => 'baz')
109
+ response = Net::HTTPNotFound.new(nil, nil, nil)
110
+
111
+ mb.session.should_receive(:delete).and_return(response)
112
+
113
+ lambda { mb.delete }.should raise_error("Deleting VMB failed. Response was Net::HTTPNotFound")
114
+ end
115
+ end
116
+
117
+ describe "expires()" do
118
+ it "should do a GET to relay and return the value of 'expires' header" do
119
+ mb = MessageBuffer.new('foo', :username => 'bar', :password => 'baz')
120
+
121
+ response = Net::HTTPOK.new(nil, nil, nil)
122
+ response.should_receive(:[]).with('expires').and_return("Thu, 01 Dec 2009 16:00:00 GMT")
123
+
124
+ mb.session.should_receive(:get_from_relay).and_return(response)
125
+
126
+ mb.expires.should == DateTime.parse("2009-12-01 16:00:00 Z")
127
+ end
128
+
129
+ it "should blow up if response is not 200" do
130
+ mb = MessageBuffer.new('foo', :username => 'bar', :password => 'baz')
131
+ response = Net::HTTPNotFound.new(nil, nil, nil)
132
+ mb.session.should_receive(:get_from_relay).and_return(response)
133
+
134
+ lambda { mb.expires }.should raise_error("Querying expiry status of VMB failed. Response was Net::HTTPNotFound")
135
+ end
136
+
137
+ it "should blow up if response has no expires header" do
138
+ mb = MessageBuffer.new('foo', :username => 'bar', :password => 'baz')
139
+ response = Net::HTTPOK.new(nil, nil, nil)
140
+ response.should_receive(:[]).with('expires').and_return(nil)
141
+ mb.session.should_receive(:get_from_relay).and_return(response)
142
+
143
+ lambda { mb.expires }.should raise_error("Querying expiry status of VMB failed. Response doesn't have expires: header")
144
+ end
145
+ end
146
+
147
+ describe "keep_alive" do
148
+ it "should send an empty post to relay on buffer endpoint" do
149
+ mb = MessageBuffer.new('foo', :username => 'bar', :password => 'baz')
150
+ response = Net::HTTPOK.new(nil, nil, nil)
151
+ mb.session.should_receive(:post_to_relay).with("\n").and_return(response)
152
+ mb.keep_alive.should == mb
153
+ end
154
+ end
155
+
156
+ describe "subscribe" do
157
+
158
+ end
159
+
160
+ end
161
+ end
@@ -0,0 +1,247 @@
1
+ require "#{File.dirname(__FILE__)}/../../spec_helper"
2
+
3
+ module DotNetServices
4
+
5
+ describe Session do
6
+
7
+ describe "new(endpoint, auth_data = nil)" do
8
+ it "should take a relative path of the endpoint and convert to a full path" do
9
+ session = Session.new("/alexeyv/EchoService/")
10
+ session.endpoint.should == "#{DotNetServices.root_url}/alexeyv/EchoService/"
11
+ end
12
+
13
+ it "should not prefix an absolute endpoint path" do
14
+ Session.new("http://foo/").endpoint.should == "http://foo/"
15
+ end
16
+
17
+ it "should prefix relative endpoint path with a slash if necessary" do
18
+ Session.new("alexeyv/EchoService/").endpoint.should == "#{DotNetServices.root_url}/alexeyv/EchoService/"
19
+ Session.new("/alexeyv/EchoService/").endpoint.should == "#{DotNetServices.root_url}/alexeyv/EchoService/"
20
+ end
21
+
22
+ it "should append a slash to the endpoint path if necessary" do
23
+ Session.new("/alexeyv/EchoService").endpoint.should == "#{DotNetServices.root_url}/alexeyv/EchoService/"
24
+ Session.new("/alexeyv/EchoService/").endpoint.should == "#{DotNetServices.root_url}/alexeyv/EchoService/"
25
+ Session.new("http://foo").endpoint.should == "http://foo/"
26
+ Session.new("http://foo/").endpoint.should == "http://foo/"
27
+ end
28
+
29
+ end
30
+
31
+ describe "open" do
32
+ it "should create the session and return it" do
33
+ authenticator = mock('authenticator')
34
+ authenticator.should_receive(:authenticate)
35
+
36
+ session = Session.open("http://foo/", authenticator)
37
+
38
+ session.should be_instance_of(Session)
39
+ session.authenticator.should == authenticator
40
+ session.endpoint.should == "http://foo/"
41
+ end
42
+
43
+ it "should, given a block, pass the session to a block and return the result of the block" do
44
+ yielded_session = nil
45
+ result = Session.open("http://foo/") { |session| yielded_session = session; "been here"}
46
+
47
+ yielded_session.should be_instance_of(Session)
48
+ yielded_session.endpoint.should == "http://foo/"
49
+ result.should == "been here"
50
+ end
51
+ end
52
+
53
+ describe "get(query_options=nil)" do
54
+ it "should send a get request to the endpoint and return the response" do
55
+ mock_http_get('foo', 80, '/')
56
+ result = Session.open("http://foo/") { |session| session.get }
57
+ result.should == :http_get_result
58
+ end
59
+
60
+ it "should accept query options" do
61
+ mock_http_get('foo', 80, '/', 'bar=baz')
62
+ result = Session.open("http://foo/") { |session| session.get(:bar => 'baz') }
63
+ result.should == :http_get_result
64
+ end
65
+
66
+ it "should escape elements of the query" do
67
+ mock_http_get('foo', 80, '/', "foo=bar%2Bbaz%7Equx")
68
+ result = Session.open("http://foo/") { |session| session.get(:foo => "bar+baz~qux") }
69
+ result.should == :http_get_result
70
+ end
71
+
72
+ it "should pass a request through authentication handler" do
73
+ mock_http, mock_get = mock_http_get('foo', 80, '/')
74
+
75
+ authenticator = mock('authenticator')
76
+ authenticator.should_receive(:authenticate)
77
+ authenticator.should_receive(:enhance).with(mock_get)
78
+
79
+ result = Session.open("http://foo/", authenticator) { |session| session.get }
80
+ result.should == :http_get_result
81
+ end
82
+
83
+ end
84
+
85
+ describe "get_to_url(url, data=nil, content_type='application/x-www-form-urlencoded')" do
86
+
87
+ it "should get data from the URL under the endpoint" do
88
+ mock_http_get('foo', 80, '/services/suburl', "foo=bar")
89
+ result = Session.open("http://foo/services/") { |session| session.get_from_url('suburl', :foo => "bar") }
90
+ result.should == :http_get_result
91
+ end
92
+
93
+ end
94
+
95
+ describe "post(data=nil, content_type='application/x-www-form-urlencoded')" do
96
+ it "should pass data as URL encoded form if data is a Hash" do
97
+ mock_http_post_form('foo', 80, '/', :foo => 'bar')
98
+ result = Session.open("http://foo/") { |session| session.post(:foo => "bar") }
99
+ result.should == :http_post_result
100
+ end
101
+
102
+ it "should set data instead of form data on the Net::HTTP::Post instance, if content type is not url-encoded-form" do
103
+ mock_http = mock('http')
104
+ mock_post = mock('post')
105
+ Net::HTTP.should_receive(:start).with('foo', 80).and_yield(mock_http)
106
+ Net::HTTP::Post.should_receive(:new).with('/services/').and_return(mock_post)
107
+ mock_post.should_receive(:body=).with("post body")
108
+ mock_post.should_receive(:content_type=).with("text/plain")
109
+ mock_http.should_receive(:request).with(mock_post).and_return(:http_post_result)
110
+
111
+ result = Session.open("http://foo/services/") { |session| session.post("post body", "text/plain") }
112
+ end
113
+
114
+ end
115
+
116
+ describe "post_to_url(url, data=nil, content_type='application/x-www-form-urlencoded')" do
117
+
118
+ it "should post data to the URL under the endpoint" do
119
+ mock_http_post_form('foo', 80, '/services/suburl', :foo => 'bar')
120
+ result = Session.open("http://foo/services/") { |session| session.post_to_url('suburl', :foo => "bar") }
121
+ result.should == :http_post_result
122
+ end
123
+
124
+ end
125
+
126
+
127
+ describe "createmb" do
128
+ it "should make a createmb request" do
129
+ mock_createmb = mock('createmb')
130
+ Net::HTTP::CreateMB.should_receive(:new).with('/services/user/location/').and_return(mock_createmb)
131
+ mock_createmb.should_receive(:[]=).with(
132
+ "X-Process-At", "http://schemas.microsoft.com/ws/2007/08/connect/roles/relay")
133
+ mock_http = mock('http')
134
+ Net::HTTP.should_receive(:start).with(DotNetServices.relay_host, 80).and_yield(mock_http)
135
+ mock_http.should_receive(:request).with(mock_createmb).and_return(:response)
136
+ Session.open("http://#{DotNetServices.relay_host}/services/user/location") { |session| session.createmb }
137
+ end
138
+ end
139
+
140
+ describe "retrieve" do
141
+ it "should make a retrieve request" do
142
+ mock_retrieve = mock('retrieve')
143
+ Net::HTTP::Retrieve.should_receive(:new).with('/services/user/location/?timeout=10').and_return(mock_retrieve)
144
+ mock_retrieve.should_receive(:[]=).with(
145
+ 'X-Process-At', "http://schemas.microsoft.com/ws/2007/08/connect/roles/relay")
146
+ mock_http = mock('http')
147
+ Net::HTTP.should_receive(:start).with(DotNetServices.relay_host, 80).and_yield(mock_http)
148
+ mock_http.should_receive(:request).with(mock_retrieve).and_return(:response)
149
+ actual_response = Session.open("http://#{DotNetServices.relay_host}/services/user/location") { |session| session.retrieve(:timeout => 10) }
150
+ actual_response.should == :response
151
+ end
152
+
153
+ it "should coerce the keys in options to strings and downcase them" do
154
+ mock_retrieve = mock('retrieve')
155
+ mock_retrieve.stub!(:[]=)
156
+ mock_http = mock('http')
157
+
158
+ Net::HTTP::Retrieve.should_receive(:new).with("/services/user/location/?timeout=5").and_return(mock_retrieve)
159
+ Net::HTTP::Retrieve.should_receive(:new).with("/services/user/location/?timeout=6").and_return(mock_retrieve)
160
+ Net::HTTP.should_receive(:start).twice.and_yield(mock_http)
161
+ mock_http.should_receive(:request).twice.with(mock_retrieve).and_return(:response)
162
+
163
+ Session.open("http://#{DotNetServices.relay_host}/services/user/location") do
164
+ |session|
165
+ session.retrieve :timeout => 5
166
+ session.retrieve "TIMEOUT" => 6
167
+ end
168
+ end
169
+
170
+ it "should insert a default timeout of 15 seconds if it's not provided" do
171
+ mock_retrieve = mock('retrieve')
172
+ mock_retrieve.stub!(:[]=)
173
+ mock_http = mock('http')
174
+
175
+ Net::HTTP::Retrieve.should_receive(:new).with("/services/user/location/?timeout=15").and_return(mock_retrieve)
176
+ Net::HTTP.should_receive(:start).and_yield(mock_http)
177
+ mock_http.should_receive(:request).with(mock_retrieve).and_return(:response)
178
+
179
+ Session.open("http://#{DotNetServices.relay_host}/services/user/location") do
180
+ |session|
181
+ session.retrieve
182
+ end
183
+ end
184
+
185
+ end
186
+
187
+ describe "get_from_relay" do
188
+
189
+ it "should set X-Process-At header and send to the bus as normal post" do
190
+ mock_http = mock('http')
191
+ Net::HTTP.should_receive(:start).with(DotNetServices.relay_host, 80).and_yield(mock_http)
192
+
193
+ mock_http.should_receive(:request) do |get_request|
194
+ get_request['X-Process-At'].should == 'http://schemas.microsoft.com/ws/2007/08/connect/roles/relay'
195
+ true
196
+ end
197
+
198
+ Session.open("http://#{DotNetServices.relay_host}/services/user/location") do
199
+ |session|
200
+ session.get_from_relay
201
+ end
202
+ end
203
+
204
+ end
205
+
206
+ describe "post_to_relay" do
207
+
208
+ it "should set X-Process-At header and send to the bus as normal post" do
209
+ mock_http = mock('http')
210
+ Net::HTTP.should_receive(:start).with(DotNetServices.relay_host, 80).and_yield(mock_http)
211
+
212
+ mock_http.should_receive(:request) do |post_request|
213
+ post_request['X-Process-At'].should == 'http://schemas.microsoft.com/ws/2007/08/connect/roles/relay'
214
+ true
215
+ end
216
+
217
+ Session.open("http://#{DotNetServices.relay_host}/services/user/location") do
218
+ |session|
219
+ session.post_to_relay
220
+ end
221
+ end
222
+
223
+ end
224
+
225
+ def mock_http_get(host, port, path, query = nil)
226
+ mock_http = mock('http')
227
+ mock_get = mock('get')
228
+ Net::HTTP.should_receive(:start).with(host, port).and_yield(mock_http)
229
+ path_and_query = query ? "#{path}?#{query}" : path
230
+ Net::HTTP::Get.should_receive(:new).with(path_and_query).and_return(mock_get)
231
+ mock_http.should_receive(:request).with(mock_get).and_return(:http_get_result)
232
+ [mock_http, mock_get]
233
+ end
234
+
235
+ def mock_http_post_form(host, port, path, form_data=nil)
236
+ mock_http = mock('http')
237
+ mock_post = mock('post')
238
+ Net::HTTP.should_receive(:start).with(host, port).and_yield(mock_http)
239
+ Net::HTTP::Post.should_receive(:new).with(path).and_return(mock_post)
240
+ mock_post.should_receive(:form_data=).with(form_data)
241
+ mock_post.should_receive(:content_type=).with('application/x-www-form-urlencoded')
242
+ mock_http.should_receive(:request).with(mock_post).and_return(:http_post_result)
243
+ [mock_http, mock_post]
244
+ end
245
+
246
+ end
247
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dot_net_services
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - ThoughtWorks
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-10-27 00:00:00 -06:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: dotnetsrv-ruby-users@rubyforge.org
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - lib/dot_net_services
26
+ - lib/dot_net_services/authentication.rb
27
+ - lib/dot_net_services/error.rb
28
+ - lib/dot_net_services/message_buffer.rb
29
+ - lib/dot_net_services/session.rb
30
+ - lib/dot_net_services.rb
31
+ - lib/net
32
+ - lib/net/http
33
+ - lib/net/http/create_mb.rb
34
+ - lib/net/http/retrieve.rb
35
+ - lib/net/http/subscribe.rb
36
+ - lib/net/http/unsubscribe.rb
37
+ - spec/integration
38
+ - spec/integration/end_to_end_spec.rb
39
+ - spec/integration/TestService
40
+ - spec/integration/TestService/Service
41
+ - spec/integration/TestService/Service/AnonymousResourceService.cs
42
+ - spec/integration/TestService/Service/App.config
43
+ - spec/integration/TestService/Service/PlainTextService.cs
44
+ - spec/integration/TestService/Service/Program.cs
45
+ - spec/integration/TestService/Service/Properties
46
+ - spec/integration/TestService/Service/Properties/AssemblyInfo.cs
47
+ - spec/integration/TestService/Service/ResourceContract.cs
48
+ - spec/integration/TestService/Service/ResourceService.cs
49
+ - spec/integration/TestService/Service/Service.csproj
50
+ - spec/integration/TestService/TestService.sln
51
+ - spec/integration/vmb_spec.rb
52
+ - spec/spec_helper.rb
53
+ - spec/unit
54
+ - spec/unit/dot_net_services
55
+ - spec/unit/dot_net_services/authentication_spec.rb
56
+ - spec/unit/dot_net_services/message_buffer_spec.rb
57
+ - spec/unit/dot_net_services/session_spec.rb
58
+ - README
59
+ - LICENSE
60
+ has_rdoc: true
61
+ homepage: http://dotnetservicesruby.com/
62
+ post_install_message:
63
+ rdoc_options: []
64
+
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: "0"
72
+ version:
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: "0"
78
+ version:
79
+ requirements: []
80
+
81
+ rubyforge_project: dotnetsrv-ruby
82
+ rubygems_version: 1.2.0
83
+ signing_key:
84
+ specification_version: 2
85
+ summary: .NET Services for Ruby
86
+ test_files: []
87
+