forked from igrigorik/em-proxy
/
proxy_spec.rb
167 lines (142 loc) · 4.72 KB
/
proxy_spec.rb
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
163
164
165
166
167
require 'spec/helper'
describe Proxy do
def failed
EventMachine.stop
fail
end
it "should recieve data on port 8080" do
EM.run do
EventMachine.add_timer(0.1) do
EventMachine::HttpRequest.new('http://127.0.0.1:8080/test').get({:timeout => 1})
end
Proxy.start(:host => "0.0.0.0", :port => 8080) do |conn|
conn.on_data do |data|
data.should =~ /GET \/test/
EventMachine.stop
end
end
end
end
it "should call the on_connect callback" do
connected = false
EM.run do
EventMachine.add_timer(0.1) do
EventMachine::HttpRequest.new('http://127.0.0.1:8080/').get({:timeout => 1})
end
Proxy.start(:host => "0.0.0.0", :port => 8080) do |conn|
conn.server :goog, :host => "google.com", :port => 80
conn.on_connect do |name|
connected = true
EventMachine.stop
end
end
end
connected.should == true
end
it "should transparently redirect TCP traffic to google" do
EM.run do
EventMachine.add_timer(0.1) do
EventMachine::HttpRequest.new('http://127.0.0.1:8080/').get({:timeout => 1})
end
Proxy.start(:host => "0.0.0.0", :port => 8080) do |conn|
conn.server :goog, :host => "google.com", :port => 80
conn.on_data { |data| data }
conn.on_response do |backend, resp|
backend.should == :goog
resp.should =~ /google/
EventMachine.stop
end
end
end
end
it "should duplex TCP traffic to two backends google & yahoo" do
EM.run do
EventMachine.add_timer(0.1) do
EventMachine::HttpRequest.new('http://127.0.0.1:8080/test').get({:timeout => 1})
end
Proxy.start(:host => "0.0.0.0", :port => 8080) do |conn|
conn.server :goog, :host => "google.com", :port => 80
conn.server :yhoo, :host => "yahoo.com", :port => 80
conn.on_data { |data| data }
seen = []
conn.on_response do |backend, resp|
case backend
when :goog then
resp.should =~ /404/
seen.push backend
when :yhoo
resp.should =~ /404/
seen.push backend
end
seen.uniq!
EventMachine.stop if seen.size == 2
end
end
end
end
it "should intercept & alter response from Google" do
EM.run do
EventMachine.add_timer(0.1) do
http = EventMachine::HttpRequest.new('http://127.0.0.1:8080/').get({:timeout => 1})
http.errback { failed }
http.callback {
http.response_header.status.should == 404
EventMachine.stop
}
end
Proxy.start(:host => "0.0.0.0", :port => 8080) do |conn|
conn.server :goog, :host => "google.com", :port => 80
conn.on_data { |data| data }
conn.on_response do |backend, data|
data.gsub(/^HTTP\/1.1 200/, 'HTTP/1.1 404')
end
end
end
end
it "should invoke on_finish callback when connection is terminated" do
EM.run do
EventMachine.add_timer(0.1) do
EventMachine::HttpRequest.new('http://127.0.0.1:8080/').get({:timeout => 1})
end
Proxy.start(:host => "0.0.0.0", :port => 8080) do |conn|
conn.server :goog, :host => "google.com", :port => 80
conn.on_data { |data| data }
conn.on_response { |backend, resp| resp }
conn.on_finish do |backend|
backend.should == :goog
EventMachine.stop
end
end
end
end
it "should not invoke on_data when :relay_client is passed as server option" do
lambda {
EM.run do
EventMachine.add_timer(0.1) do
http =EventMachine::HttpRequest.new('http://127.0.0.1:8080/').get({:timeout => 1})
http.callback { EventMachine.stop }
end
Proxy.start(:host => "0.0.0.0", :port => 8080) do |conn|
conn.server :goog, :host => "google.com", :port => 80, :relay_client => true
conn.on_data { |data| raise "Should not be here"; data }
conn.on_response { |backend, resp| resp }
end
end
}.should_not raise_error
end
it "should not invoke on_response when :relay_server is passed as server option" do
lambda {
EM.run do
EventMachine.add_timer(0.1) do
http =EventMachine::HttpRequest.new('http://127.0.0.1:8080/').get({:timeout => 1})
http.callback { EventMachine.stop }
end
Proxy.start(:host => "0.0.0.0", :port => 8080) do |conn|
conn.server :goog, :host => "google.com", :port => 80, :relay_server => true
conn.on_data { |data| data }
conn.on_response { |backend, resp| raise "Should not be here"; }
end
end
}.should_not raise_error
end
end