blob: 4cd0494c74d796279f1b52896b3c9c71b43f6342 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
# Copyright (c) 2010, Diaspora Inc. This file is
# licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file.
require 'spec_helper'
describe MessageHandler do
let(:handler) {MessageHandler.new()}
let(:message_body) {"I want to pump you up"}
let(:message_urls) {["http://www.google.com/", "http://yahoo.com/", "http://foo.com/"]}
let(:success) {FakeHttpRequest.new(:success)}
let(:failure) {FakeHttpRequest.new(:failure)}
before do
handler.clear_queue
end
after(:all) do
handler.clear_queue
end
describe 'GET messages' do
describe 'creating a GET query' do
it 'should be able to add a GET query to the queue with required destinations' do
EventMachine.run{
handler.add_get_request(message_urls)
handler.size.should == message_urls.size
EventMachine.stop
}
end
end
describe 'processing a GET query' do
it 'should remove sucessful http requests from the queue' do
EventMachine::HttpRequest.stub!(:new).and_return(success)
EventMachine.run {
handler.add_get_request("http://www.google.com/")
handler.size.should == 1
handler.process
handler.size.should == 0
EventMachine.stop
}
end
it 'should only retry a bad request the correct number of times' do
failure.should_receive(:get).exactly(MessageHandler::NUM_TRIES).times.and_return(failure)
EventMachine::HttpRequest.stub!(:new).and_return(failure)
EventMachine.run {
handler.add_get_request("http://asdfsdajfsdfbasdj.com/")
handler.size.should == 1
handler.process
handler.size.should == 0
EventMachine.stop
}
end
end
end
describe 'POST messages' do
it 'should be able to add a post message to the queue' do
EventMachine::HttpRequest.stub!(:new).and_return(success)
EventMachine.run {
handler.size.should ==0
handler.add_post_request(message_urls.first, message_body)
handler.size.should == 1
EventMachine.stop
}
end
it 'should be able to insert many posts into the queue' do
EventMachine::HttpRequest.stub!(:new).and_return(success)
EventMachine.run {
handler.size.should == 0
handler.add_post_request(message_urls, message_body)
handler.size.should == message_urls.size
EventMachine.stop
}
end
it 'should post a single message to a given URL' do
success.should_receive(:post).and_return(success)
EventMachine::HttpRequest.stub!(:new).and_return(success)
EventMachine.run{
handler.add_post_request(message_urls.first, message_body)
handler.size.should == 1
handler.process
handler.size.should == 0
EventMachine.stop
}
end
end
describe "Hub publish" do
before do
EventMachine::PubSubHubbub.stub(:new).and_return(:success)
end
it 'should correctly queue up a pubsubhubbub publish request' do
destination = "http://identi.ca/hub/"
feed_location = "http://google.com/"
EventMachine.run {
handler.add_hub_notification(destination, feed_location)
q = handler.instance_variable_get(:@queue)
message = ""
q.pop{|m| message = m}
message.destination.should == destination
message.body.should == feed_location
EventMachine.stop
}
end
end
describe "Mixed Queries" do
it 'should process both POST and GET requests in the same queue' do
success.should_receive(:get).exactly(3).times.and_return(success)
success.should_receive(:post).exactly(3).times.and_return(success)
EventMachine::HttpRequest.stub!(:new).and_return(success)
EventMachine.run{
handler.add_post_request(message_urls, message_body)
handler.size.should == 3
handler.add_get_request(message_urls)
handler.size.should == 6
handler.process
timer = EventMachine::Timer.new(1) do
handler.size.should == 0
EventMachine.stop
end
}
end
it 'should be able to have seperate POST and GET have different callbacks' do
success.should_receive(:get).exactly(1).times.and_return(success)
success.should_receive(:post).exactly(1).times.and_return(success)
EventMachine::HttpRequest.stub!(:new).and_return(success)
EventMachine.run{
handler.add_post_request(message_urls.first, message_body)
handler.add_get_request(message_urls.first)
handler.process
EventMachine.stop
}
end
end
end
|