waz-storage 1.1.1 → 1.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,299 @@
1
+ # enabling the load of files from root (on RSpec)
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__) + '/../')
3
+ require 'tests/configuration'
4
+ require 'lib/waz-queues'
5
+
6
+ describe "Windows Azure Queues API service" do
7
+ it "should list queues" do
8
+ # setup mocks
9
+ expected_url = "http://my_account.queue.core.windows.net/?comp=list"
10
+ expected_response = <<-eos
11
+ <?xml version="1.0" encoding="utf-8"?>
12
+ <EnumerationResults AccountName="http://myaccount.queue.core.windows.net">
13
+ <Queues>
14
+ <Queue>
15
+ <Name>mock-queue</Name>
16
+ <Url>http://myaccount.queue.core.windows.net/mock-queue</Url>
17
+ </Queue>
18
+ </Queues>
19
+ </EnumerationResults>
20
+ eos
21
+
22
+ mock_request = RestClient::Request.new(:method => :get, :url => expected_url)
23
+ mock_request.expects(:execute).returns(expected_response)
24
+
25
+ service = WAZ::Queues::Service.new(:account_name => "mock-account", :access_key => "mock-key", :type_of_service => "queue", :use_ssl => true, :base_url => "localhost")
26
+ # setup mocha expectations
27
+ service.expects(:generate_request_uri).with(nil, :comp => 'list').returns(expected_url)
28
+ service.expects(:generate_request).with(:get, expected_url, {:x_ms_version => '2009-09-19'}, nil).returns(mock_request)
29
+
30
+ queues = service.list_queues()
31
+ queues.first()[:name].should == "mock-queue"
32
+ queues.first()[:url].should == "http://myaccount.queue.core.windows.net/mock-queue"
33
+ end
34
+
35
+ it "should create queue" do
36
+ expected_url = "http://myaccount.queue.core.windows.net/mock-queue"
37
+ mock_request = RestClient::Request.new(:method => :get, :url => expected_url)
38
+ mock_request.stubs(:execute)
39
+
40
+ service = WAZ::Queues::Service.new(:account_name => "mock-account", :access_key => "mock-key", :type_of_service => "queue", :use_ssl => true, :base_url => "localhost")
41
+ # setup mocha expectations
42
+ service.expects(:generate_request_uri).with("mock-queue", nil).returns(expected_url)
43
+ service.expects(:generate_request).with(:put, expected_url, {:x_ms_meta_priority => "high-importance", :x_ms_version => '2009-09-19'}, nil).returns(mock_request)
44
+
45
+ service.create_queue("mock-queue", {:x_ms_meta_priority => "high-importance"})
46
+ end
47
+
48
+ it "should delete queue" do
49
+ expected_url = "http://myaccount.queue.core.windows.net/mock-queue"
50
+ mock_response = mock()
51
+ mock_request = RestClient::Request.new(:method => :get, :url => expected_url)
52
+ mock_request.stubs(:execute).returns(mock_response)
53
+
54
+ service = WAZ::Queues::Service.new(:account_name => "mock-account", :access_key => "mock-key", :type_of_service => "queue", :use_ssl => true, :base_url => "localhost")
55
+ # setup mocha expectations
56
+ service.expects(:generate_request_uri).with("mock-queue", {}).returns(expected_url)
57
+ service.expects(:generate_request).with(:delete, expected_url, {:x_ms_version => '2009-09-19'}, nil).returns(mock_request)
58
+ service.delete_queue('mock-queue')
59
+ end
60
+
61
+ it "should get queue metadata" do
62
+ expected_url = "http://myaccount.queue.core.windows.net/mock-queue?comp=metadata"
63
+ mock_response = mock()
64
+ mock_response.stubs(:headers).returns({ :x_ms_request_id => "fake-id" })
65
+ mock_request = RestClient::Request.new(:method => :head, :url => expected_url)
66
+ mock_request.stubs(:execute).returns(mock_response)
67
+
68
+ service = WAZ::Queues::Service.new(:account_name => "mock-account", :access_key => "mock-key", :type_of_service => "queue", :use_ssl => true, :base_url => "localhost")
69
+ # setup mocha expectations
70
+ service.expects(:generate_request_uri).with("mock-queue", {:comp => 'metadata'}).returns(expected_url)
71
+ service.expects(:generate_request).with(:head, expected_url, {:x_ms_version => '2009-09-19'}, nil).returns(mock_request)
72
+ service.get_queue_metadata('mock-queue').should == mock_response.headers
73
+ end
74
+
75
+ it "should set queue metadata" do
76
+ expected_url = "http://myaccount.queue.core.windows.net/mock-queue"
77
+ mock_request = RestClient::Request.new(:method => :get, :url => expected_url)
78
+ mock_request.stubs(:execute)
79
+
80
+ service = WAZ::Queues::Service.new(:account_name => "mock-account", :access_key => "mock-key", :type_of_service => "queue", :use_ssl => true, :base_url => "localhost")
81
+ # setup mocha expectations
82
+ service.expects(:generate_request_uri).with("mock-queue", :comp => 'metadata').returns(expected_url)
83
+ service.expects(:generate_request).with(:put, expected_url, {:x_ms_version => '2009-09-19', :x_ms_meta_priority => "high-importance"}, nil).returns(mock_request)
84
+
85
+ service.set_queue_metadata("mock-queue", {:x_ms_meta_priority => "high-importance"})
86
+ end
87
+
88
+ it "should enqueue message" do
89
+ expected_url = "http://myaccount.queue.core.windows.net/mock-queue/messages?messagettl=604800"
90
+ mock_request = RestClient::Request.new(:method => :post, :url => expected_url)
91
+ mock_request.stubs(:execute)
92
+
93
+ service = WAZ::Queues::Service.new(:account_name => "mock-account", :access_key => "mock-key", :type_of_service => "queue", :use_ssl => true, :base_url => "localhost")
94
+ # setup mocha expectations
95
+ payload = "<?xml version=\"1.0\" encoding=\"utf-8\"?><QueueMessage><MessageText>this is the message payload</MessageText></QueueMessage>"
96
+ service.expects(:generate_request_uri).with("mock-queue/messages", { :messagettl => 604800 }).returns(expected_url)
97
+ service.expects(:generate_request).with(:post, expected_url, { 'Content-Type' => "application/xml", :x_ms_version => '2009-09-19'}, payload).returns(mock_request)
98
+
99
+ service.enqueue("mock-queue", "this is the message payload")
100
+ end
101
+
102
+ it "should get messages from queue" do
103
+ expected_url = "http://myaccount.queue.core.windows.net/mock-queue/messages"
104
+ expected_response = <<-eos
105
+ <?xml version="1.0" encoding="utf-8"?>
106
+ <QueueMessagesList>
107
+ <QueueMessage>
108
+ <MessageId>message_id</MessageId>
109
+ <InsertionTime>Mon, 22 Sep 2008 23:29:20 GMT</InsertionTime>
110
+ <ExpirationTime>Mon, 29 Sep 2008 23:29:20 GMT</ExpirationTime>
111
+ <PopReceipt>receipt</PopReceipt>
112
+ <TimeNextVisible>Tue, 23 Sep 2008 05:29:20 GMT</TimeNextVisible>
113
+ <MessageText>text</MessageText>
114
+ </QueueMessage>
115
+ </QueueMessagesList>
116
+ eos
117
+
118
+ mock_request = RestClient::Request.new(:method => :get, :url => expected_url)
119
+ mock_request.stubs(:execute).returns(expected_response)
120
+ service = WAZ::Queues::Service.new(:account_name => "mock-account", :access_key => "mock-key", :type_of_service => "queue", :use_ssl => true, :base_url => "localhost")
121
+
122
+ # setup mocha expectations
123
+ service.expects(:generate_request_uri).with("mock-queue/messages", {}).returns(expected_url)
124
+ service.expects(:generate_request).with(:get, expected_url, {:x_ms_version => "2009-09-19"}, nil).returns(mock_request)
125
+
126
+ messages = service.get_messages("mock-queue")
127
+ messages.first()[:message_id].should == "message_id"
128
+ messages.first()[:pop_receipt].should == "receipt"
129
+ messages.first()[:message_text].should == "text"
130
+ messages.first()[:insertion_time].nil?.should == false
131
+ messages.first()[:expiration_time].nil?.should == false
132
+ messages.first()[:time_next_visible].nil?.should == false
133
+ end
134
+
135
+ it "should get messages from queue specifiying additional parameters" do
136
+ expected_url = "http://myaccount.queue.core.windows.net/mock-queue/messages"
137
+ expected_response = <<-eos
138
+ <?xml version="1.0" encoding="utf-8"?>
139
+ <QueueMessagesList>
140
+ <QueueMessage>
141
+ <MessageId>message_id</MessageId>
142
+ <InsertionTime>Mon, 22 Sep 2008 23:29:20 GMT</InsertionTime>
143
+ <ExpirationTime>Mon, 29 Sep 2008 23:29:20 GMT</ExpirationTime>
144
+ <PopReceipt>receipt</PopReceipt>
145
+ <TimeNextVisible>Tue, 23 Sep 2008 05:29:20 GMT</TimeNextVisible>
146
+ <MessageText>text</MessageText>
147
+ </QueueMessage>
148
+ </QueueMessagesList>
149
+ eos
150
+
151
+ mock_request = RestClient::Request.new(:method => :get, :url => expected_url)
152
+ mock_request.stubs(:execute).returns(expected_response)
153
+ service = WAZ::Queues::Service.new(:account_name => "mock-account", :access_key => "mock-key", :type_of_service => "queue", :use_ssl => true, :base_url => "localhost")
154
+
155
+ # setup mocha expectations
156
+ service.expects(:generate_request_uri).with("mock-queue/messages", {:num_of_messages => 2, :visibility_timeout => 3}).returns(expected_url)
157
+ service.expects(:generate_request).with(:get, expected_url, {:x_ms_version => "2009-09-19"}, nil).returns(mock_request)
158
+
159
+ messages = service.get_messages("mock-queue", :num_of_messages => 2, :visibility_timeout => 3)
160
+ messages.first()[:message_id].should == "message_id"
161
+ messages.first()[:pop_receipt].should == "receipt"
162
+ messages.first()[:message_text].should == "text"
163
+ messages.first()[:insertion_time].nil?.should == false
164
+ messages.first()[:expiration_time].nil?.should == false
165
+ messages.first()[:time_next_visible].nil?.should == false
166
+ end
167
+
168
+ it "should raise exception when :num_of_messages is out of range (1 - 32)" do
169
+ service = WAZ::Queues::Service.new(:account_name => "mock-account", :access_key => "mock-key", :type_of_service => "queue", :use_ssl => true, :base_url => "localhost")
170
+ lambda{ service.get_messages("mock-queue", :num_of_messages => 0) }.should raise_error(WAZ::Queues::OptionOutOfRange)
171
+ lambda{ service.get_messages("mock-queue", :num_of_messages => 33) }.should raise_error(WAZ::Queues::OptionOutOfRange)
172
+ end
173
+
174
+ it "should raise exception when :visibility_timeout is out of range (1 - 7200)" do
175
+ service = WAZ::Queues::Service.new(:account_name => "mock-account", :access_key => "mock-key", :type_of_service => "queue", :use_ssl => true, :base_url => "localhost")
176
+ lambda{ service.get_messages("mock-queue", :visibility_timeout => 0) }.should raise_error(WAZ::Queues::OptionOutOfRange)
177
+ lambda{ service.get_messages("mock-queue", :visibility_timeout => 7201) }.should raise_error(WAZ::Queues::OptionOutOfRange)
178
+ end
179
+
180
+ it "should peek message from queue" do
181
+ expected_url = "http://myaccount.queue.core.windows.net/mock-queue/messages"
182
+ expected_response = <<-eos
183
+ <?xml version="1.0" encoding="utf-8"?>
184
+ <QueueMessagesList>
185
+ <QueueMessage>
186
+ <MessageId>message_id</MessageId>
187
+ <InsertionTime>Mon, 22 Sep 2008 23:29:20 GMT</InsertionTime>
188
+ <ExpirationTime>Mon, 29 Sep 2008 23:29:20 GMT</ExpirationTime>
189
+ <MessageText>text</MessageText>
190
+ <DequeueCount>5</DequeueCount>
191
+ </QueueMessage>
192
+ </QueueMessagesList>
193
+ eos
194
+
195
+ mock_request = RestClient::Request.new(:method => :get, :url => expected_url)
196
+ mock_request.stubs(:execute).returns(expected_response)
197
+ service = WAZ::Queues::Service.new(:account_name => "mock-account", :access_key => "mock-key", :type_of_service => "queue", :use_ssl => true, :base_url => "localhost")
198
+
199
+ # setup mocha expectations
200
+ service.expects(:generate_request_uri).with("mock-queue/messages", {:peek_only => true}).returns(expected_url)
201
+ service.expects(:generate_request).with(:get, expected_url, {:x_ms_version => "2009-09-19"}, nil).returns(mock_request)
202
+
203
+ messages = service.peek("mock-queue")
204
+ messages.first()[:message_id].should == "message_id"
205
+ messages.first()[:message_text].should == "text"
206
+ messages.first()[:insertion_time].nil?.should == false
207
+ messages.first()[:expiration_time].nil?.should == false
208
+ messages.first()[:dequeue_count].should == 5
209
+ end
210
+
211
+ it "should peek messages from queue when :num_of_messages is specified" do
212
+ expected_url = "http://myaccount.queue.core.windows.net/mock-queue/messages"
213
+ expected_response = <<-eos
214
+ <?xml version="1.0" encoding="utf-8"?>
215
+ <QueueMessagesList>
216
+ <QueueMessage>
217
+ <MessageId>message_id</MessageId>
218
+ <InsertionTime>Mon, 22 Sep 2008 23:29:20 GMT</InsertionTime>
219
+ <ExpirationTime>Mon, 29 Sep 2008 23:29:20 GMT</ExpirationTime>
220
+ <MessageText>text</MessageText>
221
+ <DequeueCount>5</DequeueCount>
222
+ </QueueMessage>
223
+ </QueueMessagesList>
224
+ eos
225
+
226
+ mock_request = RestClient::Request.new(:method => :get, :url => expected_url)
227
+ mock_request.stubs(:execute).returns(expected_response)
228
+ service = WAZ::Queues::Service.new(:account_name => "mock-account", :access_key => "mock-key", :type_of_service => "queue", :use_ssl => true, :base_url => "localhost")
229
+
230
+ # setup mocha expectations
231
+ service.expects(:generate_request_uri).with("mock-queue/messages", {:peek_only => true, :num_of_messages => 32}).returns(expected_url)
232
+ service.expects(:generate_request).with(:get, expected_url, {:x_ms_version => "2009-09-19"}, nil).returns(mock_request)
233
+
234
+ messages = service.peek("mock-queue", {:num_of_messages => 32})
235
+ messages.first()[:message_id].should == "message_id"
236
+ messages.first()[:message_text].should == "text"
237
+ messages.first()[:insertion_time].nil?.should == false
238
+ messages.first()[:expiration_time].nil?.should == false
239
+ messages.first()[:dequeue_count].should == 5
240
+ end
241
+
242
+ it "should delete message" do
243
+ expected_url = "http://myaccount.queue.core.windows.net/mock-queue/messages/message_id?popreceipt=pop_receipt"
244
+ mock_request = RestClient::Request.new(:method => :get, :url => expected_url)
245
+ mock_request.stubs(:execute)
246
+
247
+ service = WAZ::Queues::Service.new(:account_name => "mock-account", :access_key => "mock-key", :type_of_service => "queue", :use_ssl => true, :base_url => "localhost")
248
+ # setup mocha expectations
249
+ service.expects(:generate_request_uri).with("mock-queue/messages/message_id", { :pop_receipt => "pop_receipt" }).returns(expected_url)
250
+ service.expects(:generate_request).with(:delete, expected_url, {}, nil).returns(mock_request)
251
+
252
+ service.delete_message("mock-queue", "message_id", "pop_receipt")
253
+ end
254
+
255
+ it "should clear queue" do
256
+ expected_url = "http://myaccount.queue.core.windows.net/mock-queue/messages"
257
+ mock_request = RestClient::Request.new(:method => :get, :url => expected_url)
258
+ mock_request.stubs(:execute)
259
+
260
+ service = WAZ::Queues::Service.new(:account_name => "mock-account", :access_key => "mock-key", :type_of_service => "queue", :use_ssl => true, :base_url => "localhost")
261
+ # setup mocha expectations
262
+ service.expects(:generate_request_uri).with("mock-queue/messages", {}).returns(expected_url)
263
+ service.expects(:generate_request).with(:delete, expected_url, {:x_ms_version => '2009-09-19'}, nil).returns(mock_request)
264
+
265
+ service.clear_queue("mock-queue")
266
+ end
267
+
268
+ it "should list queues with metadata" do
269
+ # setup mocks
270
+ expected_url = "http://my_account.queue.core.windows.net/?comp=list&include=metadata"
271
+ expected_response = <<-eos
272
+ <?xml version="1.0" encoding="utf-8"?>
273
+ <EnumerationResults AccountName="http://myaccount.queue.core.windows.net">
274
+ <Queues>
275
+ <Queue>
276
+ <Name>mock-queue</Name>
277
+ <Url>http://myaccount.queue.core.windows.net/mock-queue</Url>
278
+ <Metadata>
279
+ <x-ms-name>Custom Queue</x-ms-name>
280
+ </Metadata>
281
+ </Queue>
282
+ </Queues>
283
+ </EnumerationResults>
284
+ eos
285
+
286
+ mock_request = RestClient::Request.new(:method => :get, :url => expected_url)
287
+ mock_request.expects(:execute).returns(expected_response)
288
+
289
+ service = WAZ::Queues::Service.new(:account_name => "mock-account", :access_key => "mock-key", :type_of_service => "queue", :use_ssl => true, :base_url => "localhost")
290
+ # setup mocha expectations
291
+ service.expects(:generate_request_uri).with(nil, {:comp => 'list', :include => 'metadata'}).returns(expected_url)
292
+ service.expects(:generate_request).with(:get, expected_url, {:x_ms_version => '2009-09-19'}, nil).returns(mock_request)
293
+
294
+ queues = service.list_queues({:include => 'metadata'})
295
+ queues.first()[:name].should == "mock-queue"
296
+ queues.first()[:url].should == "http://myaccount.queue.core.windows.net/mock-queue"
297
+ queues.first()[:metadata][:x_ms_name] = 'Custom Queue'
298
+ end
299
+ end
@@ -0,0 +1,81 @@
1
+ # enabling the load of files from root (on RSpec)
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__) + '/../')
3
+ require 'tests/configuration'
4
+ require 'lib/waz-blobs'
5
+
6
+ describe "Base class for connection management" do
7
+ it "should throw an exception when it is not connected" do
8
+ lambda {WAZ::Storage::Base.default_connection}.should raise_error(WAZ::Storage::NotConnected)
9
+ end
10
+
11
+ it "establish connection and set it as default connection" do
12
+ WAZ::Storage::Base.establish_connection!(:account_name => 'myAccount',
13
+ :access_key => "accountKey",
14
+ :use_ssl => true)
15
+
16
+ connection = WAZ::Storage::Base.default_connection
17
+ connection[:account_name].should == "myAccount"
18
+ connection[:access_key].should == "accountKey"
19
+ connection[:use_ssl].should == true
20
+ end
21
+
22
+ it "should throw an exception when no account_name is provided" do
23
+ lambda {WAZ::Storage::Base.establish_connection!(:access_key => "accountKey", :use_ssl => false)}.should raise_error(WAZ::Storage::InvalidOption)
24
+ end
25
+
26
+ it "should throw an exception when no access_key is provided and no use_sas_auth_only" do
27
+ lambda {WAZ::Storage::Base.establish_connection!(:account_name => "my_account", :use_ssl => false)}.should raise_error(WAZ::Storage::InvalidOption)
28
+ end
29
+
30
+ it "should throw and exception when use_sas_auth_only is provided but no sharedaccessignature" do
31
+ lambda {WAZ::Storage::Base.establish_connection!(:account_name => "my_account",:use_sas_auth_only => true, :use_ssl => false)}.should raise_error(WAZ::Storage::InvalidOption)
32
+ end
33
+
34
+
35
+ it "should set use_ssl to false when no paramter provided" do
36
+ WAZ::Storage::Base.establish_connection!(:account_name => 'myAccount',
37
+ :access_key => "accountKey")
38
+
39
+ connection = WAZ::Storage::Base.default_connection
40
+ connection[:account_name].should == "myAccount"
41
+ connection[:access_key].should == "accountKey"
42
+ connection[:use_ssl].should == false
43
+ end
44
+
45
+ it "should set :use_sas_auth_only to false when no parameter provided" do
46
+ WAZ::Storage::Base.establish_connection!(:account_name => 'myAccount',
47
+ :access_key => "accountKey")
48
+ connection = WAZ::Storage::Base.default_connection
49
+ connection[:account_name].should == "myAccount"
50
+ connection[:access_key].should == "accountKey"
51
+ connection[:use_sas_auth_only] == false
52
+ end
53
+
54
+
55
+ it "should be able to tell whether it's connected or not" do
56
+ WAZ::Storage::Base.establish_connection!(:account_name => 'myAccount',
57
+ :access_key => "accountKey")
58
+
59
+ WAZ::Storage::Base.connected?.should == true
60
+ end
61
+
62
+ it "should be able to tell whters it's connected by shared access signature" do
63
+ WAZ::Storage::Base.establish_connection!(:account_name => 'myAccount',:use_sas_auth_only=>true, :sharedaccesssignature=>"?se=2011-12-13T23%3A54%3A07Z&sr=c&si=escrituraPAC&sig=e%2Bz6wsGhVRg%2FaxWATiS8A4WjWU%2FbfWjWa6zJ%2BTQIPzM%3D")
64
+ WAZ::Storage::Base.connected?.should == true
65
+ end
66
+
67
+ it "should be able manage scoped connections" do
68
+ WAZ::Storage::Base.establish_connection!(:account_name => 'myAccount', :access_key => "accountKey")
69
+
70
+ WAZ::Storage::Base.default_connection[:account_name].should == 'myAccount'
71
+
72
+ block_executed = false
73
+ WAZ::Storage::Base.establish_connection(:account_name => 'otherAccount', :access_key => "accountKey") do
74
+ WAZ::Storage::Base.default_connection[:account_name].should == 'otherAccount'
75
+ block_executed = true
76
+ end
77
+ block_executed.should == true
78
+
79
+ WAZ::Storage::Base.default_connection[:account_name].should == 'myAccount'
80
+ end
81
+ end
@@ -0,0 +1,142 @@
1
+ # enabling the load of files from root (on RSpec)
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__) + '/../')
3
+ require 'tests/configuration'
4
+ require 'lib/waz-queues'
5
+
6
+ describe "storage service core behavior" do
7
+ it "should generate URI with given operation" do
8
+ service = WAZ::Queues::Service.new(:account_name => "mock-account", :access_key => "mock-key", :type_of_service => "queue", :use_ssl => true, :base_url => "localhost")
9
+ service.generate_request_uri(nil, :comp => 'list').should == "https://mock-account.queue.localhost/?comp=list"
10
+ end
11
+
12
+ it "should generate an URI without operation when operation is not given" do
13
+ service = WAZ::Queues::Service.new(:account_name => "mock-account", :access_key => "mock-key", :type_of_service => "queue", :use_ssl => true, :base_url => "localhost")
14
+ service.generate_request_uri("queue").should == "https://mock-account.queue.localhost/queue"
15
+ end
16
+
17
+ it "should generate a safe URI when path includes forward slash" do
18
+ service = WAZ::Queues::Service.new(:account_name => "mock-account", :access_key => "mock-key", :type_of_service => "queue", :use_ssl => true, :base_url => "localhost")
19
+ service.generate_request_uri("/queue").should == "https://mock-account.queue.localhost/queue"
20
+ end
21
+
22
+ it "should include additional parameters when given" do
23
+ service = WAZ::Queues::Service.new(:account_name => "mock-account", :access_key => "mock-key", :type_of_service => "queue", :use_ssl => true, :base_url => "localhost")
24
+ service.generate_request_uri("/queue", {:comp => 'list', :prefix => "p"}).should == "https://mock-account.queue.localhost/queue?comp=list&prefix=p"
25
+ end
26
+
27
+ it "should include additional parameters when given althought when there is no comp" do
28
+ service = WAZ::Queues::Service.new(:account_name => "mock-account", :access_key => "mock-key", :type_of_service => "queue", :use_ssl => true, :base_url => "localhost")
29
+ service.generate_request_uri("/queue", {:prefix => "p", :other => "other"}).should == "https://mock-account.queue.localhost/queue?other=other&prefix=p"
30
+ end
31
+
32
+ it "should include additional parameters when given althought when there is no comp" do
33
+ service = WAZ::Queues::Service.new(:account_name => "mock-account", :access_key => "mock-key", :type_of_service => "queue", :use_ssl => true, :base_url => "localhost")
34
+ service.generate_request_uri("/queue", {:comp => "metadata", :messagettl => 650}).should == "https://mock-account.queue.localhost/queue?comp=metadata&messagettl=650"
35
+ end
36
+
37
+ it "should include additional parameters when given althought when there is no comp" do
38
+ service = WAZ::Queues::Service.new(:account_name => "mock-account", :access_key => "mock-key", :type_of_service => "queue", :use_ssl => true, :base_url => "localhost")
39
+ service.generate_request_uri("/queue", {:item => "%"}).should == "https://mock-account.queue.localhost/queue?item=%25"
40
+ end
41
+
42
+ it "should canonicalize headers (order lexicographical, trim values, and join by NEW_LINES)" do
43
+ service = WAZ::Queues::Service.new(:account_name => "mock-account", :access_key => "mock-key", :type_of_service => "queue", :use_ssl => true, :base_url => "localhost")
44
+ headers = { "Content-Type" => "application/xml",
45
+ "x-ms-prop-z" => "p",
46
+ "x-ms-meta-name" => "a ",
47
+ "x-other" => "other"}
48
+
49
+ service.canonicalize_headers(headers).should == "x-ms-meta-name:a\nx-ms-prop-z:p"
50
+ end
51
+
52
+ it "should return empty string when no MS headers" do
53
+ service = WAZ::Queues::Service.new(:account_name => "mock-account", :access_key => "mock-key", :type_of_service => "queue", :use_ssl => true, :base_url => "localhost")
54
+ headers = { "Content-Type" => "application/xml",
55
+ "x-other" => "other"}
56
+
57
+ service.canonicalize_headers(headers).should == ""
58
+ end
59
+
60
+ it "should cannonicalize message by appending account_name to the request path" do
61
+ service = WAZ::Queues::Service.new(:account_name => "mock-account", :access_key => "mock-key", :type_of_service => "queue", :use_ssl => true, :base_url => "localhost")
62
+ service.canonicalize_message("http://localhost/queue?comp=list").should == "/mock-account/queue?comp=list"
63
+ end
64
+
65
+ it "should ignore every other querystring parameter rather than comp=" do
66
+ service = WAZ::Queues::Service.new(:account_name => "mock-account", :access_key => "mock-key", :type_of_service => "queue", :use_ssl => true, :base_url => "localhost")
67
+ service.canonicalize_message("http://localhost/queue?myparam=1").should == "/mock-account/queue"
68
+ end
69
+
70
+ it "should properly canonicalize message when no parameter associated with it" do
71
+ service = WAZ::Queues::Service.new(:account_name => "mock-account", :access_key => "mock-key", :type_of_service => "queue", :use_ssl => true, :base_url => "localhost")
72
+ service.canonicalize_message("http://mock-account.queue.core.windows.net/").should == "/mock-account/"
73
+ end
74
+
75
+ it "should properly canonicalize message when a resource is associated with it" do
76
+ service = WAZ::Queues::Service.new(:account_name => "mock-account", :access_key => "mock-key", :type_of_service => "queue", :use_ssl => true, :base_url => "localhost")
77
+ service.canonicalize_message("http://mock-account.queue.core.windows.net/resource?comp=list").should == "/mock-account/resource?comp=list"
78
+ end
79
+
80
+ it "should generate request with proper headers" do
81
+ mock_time = Time.new
82
+ Time.stubs(:new).returns(mock_time)
83
+ service = WAZ::Queues::Service.new(:account_name => "mock-account", :access_key => "mock-key", :type_of_service => "queue", :use_ssl => true, :base_url => "localhost")
84
+
85
+ expected_request_hash = {:headers => {'x-ms-Date' => mock_time.httpdate, "Content-Length" => "payload".length},
86
+ :method => :put,
87
+ :url => "http://localhost/johnny",
88
+ :payload => "payload"}
89
+
90
+ # mock the generate signature method since we want to assert against a know value
91
+ service.expects(:generate_signature).with(expected_request_hash).returns("a_mock_signature")
92
+
93
+ request = service.generate_request("PUT", "http://localhost/johnny", nil, "payload")
94
+ request.headers["x-ms-Date"].should == mock_time.httpdate
95
+ request.headers["Content-Length"].should == "payload".length
96
+ request.headers["Authorization"] = "SharedKey mock-account:a_mock_signature"
97
+ end
98
+
99
+ it "should set content length when it is not provided" do
100
+ mock_time = Time.new
101
+ Time.stubs(:new).returns(mock_time)
102
+ service = WAZ::Queues::Service.new(:account_name => "mock-account", :access_key => "mock-key", :type_of_service => "queue", :use_ssl => true, :base_url => "localhost")
103
+
104
+ expected_request_hash = {:headers => {'x-ms-Date' => mock_time.httpdate, 'Content-Length' => 0},
105
+ :method => :put,
106
+ :url => "http://localhost/johnny",
107
+ :payload => nil}
108
+
109
+ # mock the generate signature method since we want to assert against a know value
110
+ service.expects(:generate_signature).with(expected_request_hash).returns("a_mock_signature")
111
+
112
+ request = service.generate_request("PUT", "http://localhost/johnny", nil)
113
+ request.headers["x-ms-Date"].should == mock_time.httpdate
114
+ request.headers["Content-Length"].should == 0
115
+ request.headers["Authorization"] = "SharedKey mock-account:a_mock_signature"
116
+ end
117
+
118
+ it "should name headers properly when they are provided as symbols" do
119
+ mock_time = Time.new
120
+ Time.stubs(:new).returns(mock_time)
121
+ service = WAZ::Queues::Service.new(:account_name => "mock-account", :access_key => "mock-key", :type_of_service => "queue", :use_ssl => true, :base_url => "localhost")
122
+
123
+ expected_request_hash = {:headers => {'x-ms-Date' => mock_time.httpdate, "Content-Length" => 0, 'Content-Type' => "plain/xml"},
124
+ :method => :put,
125
+ :url => "http://localhost/johnny",
126
+ :payload => nil}
127
+
128
+ # mock the generate signature method since we want to assert against a know value
129
+ service.expects(:generate_signature).with(expected_request_hash).returns("a_mock_signature")
130
+
131
+ request = service.generate_request("PUT", "http://localhost/johnny", {:Content_Type => "plain/xml"})
132
+ request.headers["x-ms-Date"].should == mock_time.httpdate
133
+ request.headers["Content-Length"].should == 0
134
+ request.headers["Authorization"] = "SharedKey mock-account:a_mock_signature"
135
+ end
136
+
137
+ it "should cannonicalize message by appending account_name to the request path following 2009-09-19 version of the API" do
138
+ service = WAZ::Queues::Service.new(:account_name => "myaccount", :access_key => "mock-key", :type_of_service => "queue", :use_ssl => true, :base_url => "localhost")
139
+ canonical_message = service.canonicalize_message20090919("http://myaccount.blob.core.windows.net/mycontainer?restype=container&comp=metadata")
140
+ canonical_message.should == "/myaccount/mycontainer\ncomp:metadata\nrestype:container"
141
+ end
142
+ end