Skip to content

Commit 2c1bf97

Browse files
committed
Using Hayabusa webserver to run local specs instead of connecting to various websites through sockets.
1 parent 4cab052 commit 2c1bf97

14 files changed

+166
-49
lines changed

Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ group :development do
99
gem "rdoc", "~> 3.12"
1010
gem "bundler", ">= 1.0.0"
1111
gem "jeweler", ">= 1.8.4"
12+
gem "hayabusa", ">= 0.0.25"
13+
gem "sqlite3"
1214
end
1315

1416
gem "codeclimate-test-reporter", group: :test, require: nil

Gemfile.lock

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ GEM
55
builder (3.2.2)
66
codeclimate-test-reporter (0.4.0)
77
simplecov (>= 0.7.1, < 1.0.0)
8+
datet (0.0.25)
89
descendants_tracker (0.0.4)
910
thread_safe (~> 0.3, >= 0.3.1)
1011
diff-lcs (1.1.3)
1112
docile (1.1.5)
13+
erubis (2.7.0)
1214
faraday (0.9.0)
1315
multipart-post (>= 1.2, < 3)
1416
git (1.2.8)
@@ -21,7 +23,17 @@ GEM
2123
nokogiri (~> 1.6.3)
2224
oauth2
2325
hashie (3.2.0)
26+
hayabusa (0.0.25)
27+
datet
28+
erubis
29+
http2
30+
knjrbfw
31+
mail
32+
ruby_process
33+
tpool
2434
highline (1.6.21)
35+
http2 (0.0.29)
36+
string-cases
2537
jeweler (2.0.1)
2638
builder
2739
bundler (>= 1.0)
@@ -33,6 +45,16 @@ GEM
3345
rdoc
3446
json (1.8.1)
3547
jwt (1.0.0)
48+
knjrbfw (0.0.110)
49+
datet
50+
http2
51+
php4r
52+
ruby_process
53+
tsafe
54+
wref
55+
mail (2.6.3)
56+
mime-types (>= 1.16, < 3)
57+
mime-types (2.4.3)
3658
mini_portile (0.6.0)
3759
multi_json (1.10.1)
3860
multi_xml (0.5.5)
@@ -45,6 +67,10 @@ GEM
4567
multi_json (~> 1.3)
4668
multi_xml (~> 0.5)
4769
rack (~> 1.2)
70+
php4r (0.0.4)
71+
datet
72+
http2
73+
string-strtr
4874
rack (1.5.2)
4975
rake (10.3.2)
5076
rdoc (3.12.2)
@@ -57,21 +83,31 @@ GEM
5783
rspec-expectations (2.8.0)
5884
diff-lcs (~> 1.1.2)
5985
rspec-mocks (2.8.0)
86+
ruby_process (0.0.9)
87+
tsafe
88+
wref
6089
simplecov (0.9.0)
6190
docile (~> 1.1.0)
6291
multi_json
6392
simplecov-html (~> 0.8.0)
6493
simplecov-html (0.8.0)
94+
sqlite3 (1.3.10)
6595
string-cases (0.0.0)
96+
string-strtr (0.0.3)
6697
thread_safe (0.3.4)
98+
tpool (0.0.4)
99+
tsafe (0.0.11)
100+
wref (0.0.6)
67101

68102
PLATFORMS
69103
ruby
70104

71105
DEPENDENCIES
72106
bundler (>= 1.0.0)
73107
codeclimate-test-reporter
108+
hayabusa (>= 0.0.25)
74109
jeweler (>= 1.8.4)
75110
rdoc (~> 3.12)
76111
rspec (~> 2.8.0)
112+
sqlite3
77113
string-cases

include/connection.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ def reconnect
6767
elsif @args[:proxy]
6868
connect_proxy
6969
else
70-
print "Http2: Opening socket connection to '#{@args[:host]}:#{@args[:port]}'.\n" if @debug
71-
@sock_plain = TCPSocket.new(@args[:host], @args[:port].to_i)
70+
puts "Http2: Opening socket connection to '#{@http2.host}:#{@http2.port}'." if @debug
71+
@sock_plain = TCPSocket.new(@http2.host, @http2.port)
7272
end
7373

7474
if @args[:ssl]

include/response.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,13 @@ def header?(key)
3636
end
3737

3838
def content_length
39-
header("content-length").to_i if header?("content-length")
39+
if header?("content-length")
40+
header("content-length").to_i
41+
elsif @body
42+
return @body.bytesize
43+
else
44+
raise "Couldn't calculate content-length."
45+
end
4046
end
4147

4248
#Returns the requested URL as a string.

include/response_reader.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def finish
7474
private
7575

7676
def check_and_follow_redirect
77-
if (@response.code == "302" || @response.code == "307") && @response.header?("location") && @http2.args[:follow_redirects]
77+
if (@response.code == "302" || @response.code == "303" || @response.code == "307") && @response.header?("location") && @http2.args[:follow_redirects]
7878
url, args = url_and_args_from_location
7979

8080
if !args[:host] || args[:host] == @args[:host]

lib/http2.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ def default_headers(args = {})
146146
host = args[:host] || self.host
147147
port = args[:port] || self.port
148148

149-
headers["Host"] = host
149+
headers["Host"] = "#{host}" # Copy host string to avoid changing the original string if port has been given!
150150
headers["Host"] << ":#{port}" if port && ![80, 443].include?(port.to_i) && !@args[:skip_port_in_host_header]
151151
headers["Accept-Encoding"] = "gzip" if @args[:encoding_gzip]
152152

spec/helpers.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
module Helpers
2+
def with_webserver
3+
require "hayabusa"
4+
hayabusa = Hayabusa.new(
5+
doc_root: "#{File.dirname(__FILE__)}/spec_root",
6+
port: 3005
7+
)
8+
hayabusa.start
9+
10+
begin
11+
yield(hayabusa)
12+
ensure
13+
hayabusa.stop
14+
end
15+
end
16+
17+
def with_http(args = {})
18+
with_webserver do |hayabusa|
19+
Http2.new({host: "localhost", port: hayabusa.port, encoding_gzip: false}.merge(args)) do |http|
20+
begin
21+
yield http
22+
rescue Http2::Errors::Internalserver => e
23+
puts "Body of error-response: #{e.response.body}"
24+
raise e
25+
end
26+
end
27+
end
28+
end
29+
end

spec/http2/response_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
describe Http2::Response do
44
it "should register content type" do
5-
Http2.new(host: "http2test.kaspernj.org") do |http|
6-
res = http.get("content_type_test.php")
5+
with_http do |http|
6+
res = http.get("content_type_test.rhtml")
77
res.content_type.should eq "text/html"
88
end
99
end
1010

1111
it "should register content length" do
12-
Http2.new(host: "http2test.kaspernj.org") do |http|
13-
res = http.get("content_type_test.php")
12+
with_http do |http|
13+
res = http.get("content_type_test.rhtml")
1414
res.content_length.should > 50
1515
end
1616
end

spec/http2_spec.rb

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
describe "Http2" do
55
it "should be able to do normal post-requests." do
66
#Test posting keep-alive and advanced post-data.
7-
Http2.new(host: "www.partyworm.dk", debug: false) do |http|
7+
with_http do |http|
88
0.upto(5) do
9-
resp = http.get("multipart_test.php")
9+
resp = http.get("multipart_test.rhtml")
1010

11-
resp = http.post(url: "multipart_test.php?choice=post-test", post: {
11+
resp = http.post(url: "multipart_test.rhtml?choice=post-test", post: {
1212
"val1" => "test1",
1313
"val2" => "test2",
1414
"val3" => [
@@ -31,33 +31,33 @@
3131
res.is_a?(Hash).should eq true
3232
res["val1"].should eq "test1"
3333
res["val2"].should eq "test2"
34-
res["val3"][0].should eq "test3"
34+
res["val3"]["0"].should eq "test3"
3535
res["val4"]["val5"].should eq "test5"
36-
res["val6"]["val7"][0]["val8"].should eq "test8"
37-
res["val9"][0].should eq "a"
38-
res["val9"][1].should eq "b"
39-
res["val9"][2].should eq "d"
36+
res["val6"]["val7"]["0"]["val8"].should eq "test8"
37+
res["val9"]["0"].should eq "a"
38+
res["val9"]["1"].should eq "b"
39+
res["val9"]["2"].should eq "d"
4040
end
4141
end
4242
end
4343

4444
it "#reconnect" do
45-
Http2.new(host: "www.partyworm.dk", follow_redirects: false, encoding_gzip: false, debug: false) do |http|
46-
resp1 = http.get("multipart_test.php")
45+
with_http(follow_redirects: false, encoding_gzip: false) do |http|
46+
resp1 = http.get("multipart_test.rhtml")
4747
http.reconnect
48-
resp2 = http.get("multipart_test.php")
48+
resp2 = http.get("multipart_test.rhtml")
4949

5050
resp1.body.should eq resp2.body
5151
end
5252
end
5353

5454
it "should be able to do multipart-requests and keep-alive when using multipart." do
55-
Http2.new(host: "www.partyworm.dk", follow_redirects: false, encoding_gzip: false, debug: false) do |http|
55+
with_http(follow_redirects: false) do |http|
5656
0.upto(5) do
5757
fpath = File.realpath(__FILE__)
5858
fpath2 = "#{File.realpath(File.dirname(__FILE__))}/../lib/http2.rb"
5959

60-
resp = http.post_multipart(url: "multipart_test.php", post: {
60+
resp = http.post_multipart(url: "multipart_test.rhtml", post: {
6161
"test_var" => "true",
6262
"test_file1" => {
6363
fpath: fpath,
@@ -80,14 +80,11 @@
8080

8181
it "it should be able to handle keep-alive correctly" do
8282
urls = [
83-
"?show=users_search",
84-
"?show=users_online",
85-
"?show=drinksdb",
86-
"?show=forum&fid=9&tid=1917&page=0"
83+
"content_type_test.rhtml",
84+
"json_test.rhtml"
8785
]
88-
urls = ["robots.txt"]
8986

90-
Http2.new(host: "www.partyworm.dk", debug: false) do |http|
87+
with_http do |http|
9188
0.upto(105) do |count|
9289
url = urls[rand(urls.size)]
9390
#print "Doing request #{count} of 200 (#{url}).\n"
@@ -103,53 +100,56 @@
103100
end
104101

105102
it "should raise exception when something is not found" do
106-
expect{
107-
Http2.new(host: "www.partyworm.dk") do |http|
108-
http.get("something_that_does_not_exist.php")
109-
end
110-
}.to raise_error(::Http2::Errors::Notfound)
103+
with_http do |http|
104+
expect{
105+
http.get("something_that_does_not_exist.rhtml")
106+
}.to raise_error(::Http2::Errors::Notfound)
107+
end
111108
end
112109

113110
it "should be able to post json" do
114-
Http2.new(host: "http2test.kaspernj.org") do |http|
111+
with_http do |http|
115112
res = http.post(
116-
url: "/jsontest.php",
113+
url: "json_test.rhtml",
117114
json: {testkey: "testvalue"}
118115
)
119116

120117
data = JSON.parse(res.body)
121-
data["_SERVER"]["CONTENT_TYPE"].should eql("application/json")
122-
data["PHP_JSON_INPUT"]["testkey"].should eql("testvalue")
118+
data["_SERVER"]["HTTP_CONTENT_TYPE"].should eq "application/json"
119+
120+
# Hack JSON data from Hayabusa.
121+
json_data = JSON.parse(data["_POST"].keys.first)
122+
json_data["testkey"].should eq "testvalue"
123123
end
124124
end
125125

126126
it "should be able to post custom content types" do
127-
require "json"
128-
129-
Http2.new(host: "http2test.kaspernj.org") do |http|
127+
with_http do |http|
130128
res = http.post(
131-
url: "content_type_test.php",
129+
url: "content_type_test.rhtml",
132130
content_type: "plain/text",
133131
post: "test1_test2_test3"
134132
)
135133

136134
data = JSON.parse(res.body)
137-
data["_SERVER"]["CONTENT_TYPE"].should eql("plain/text")
138-
data["PHP_INPUT"].should eql("test1_test2_test3")
135+
data["_SERVER"]["HTTP_CONTENT_TYPE"].should eq "plain/text"
136+
137+
raw_data = data["_POST"].keys.first
138+
raw_data.should eq "test1_test2_test3"
139139
end
140140
end
141141

142142
it "should set various timeouts" do
143-
Http2.new(host: "http2test.kaspernj.org") do |http|
144-
res = http.get("content_type_test.php")
145-
http.keepalive_timeout.should eq 5
146-
http.keepalive_max.should eq 100
143+
with_http do |http|
144+
res = http.get("content_type_test.rhtml")
145+
http.keepalive_timeout.should eq 15
146+
http.keepalive_max.should eq 30
147147
end
148148
end
149149

150150
it "should follow redirects" do
151-
Http2.new(host: "http2test.kaspernj.org", follow_redirects: true) do |http|
152-
resp = http.get("redirect_test.php")
151+
with_http(follow_redirects: true) do |http|
152+
resp = http.get("redirect_test.rhtml")
153153
resp.code.should eq "200"
154154
end
155155
end

spec/spec_helper.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
$LOAD_PATH.unshift(File.dirname(__FILE__))
66
require 'rspec'
77
require 'http2'
8+
require 'helpers'
89

910
# Requires supporting files with custom matchers and macros, etc,
1011
# in ./support/ and its subdirectories.
@@ -14,4 +15,6 @@
1415
config.expect_with :rspec do |c|
1516
c.syntax = [:expect, :should]
1617
end
18+
19+
config.include Helpers
1720
end

0 commit comments

Comments
 (0)