Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 191 lines (162 sloc) 6.561 kb
923e933 @raggi Add some basic Rack::Server tests, nothing like the oldies, but faster
raggi authored
1 require 'rack'
c73b474 @lsegal Rack::Server should accept :app and override :config.
lsegal authored
2 require 'rack/server'
923e933 @raggi Add some basic Rack::Server tests, nothing like the oldies, but faster
raggi authored
3 require 'tempfile'
4 require 'socket'
5 require 'open-uri'
c73b474 @lsegal Rack::Server should accept :app and override :config.
lsegal authored
6
7 describe Rack::Server do
d924f80 @smcgivern Prioritise CLI options over comment options
smcgivern authored
8 SPEC_ARGV = []
9
10 before { SPEC_ARGV[0..-1] = [] }
923e933 @raggi Add some basic Rack::Server tests, nothing like the oldies, but faster
raggi authored
11
12 def app
a9beb47 @raggi 1.9 lambdas care (yep, they're emo - rock on.)
raggi authored
13 lambda { |env| [200, {'Content-Type' => 'text/plain'}, ['success']] }
923e933 @raggi Add some basic Rack::Server tests, nothing like the oldies, but faster
raggi authored
14 end
15
edc8b92 @raggi Improve pidfile reporting and test coverage
raggi authored
16 def with_stderr
17 old, $stderr = $stderr, StringIO.new
18 yield $stderr
19 ensure
20 $stderr = old
21 end
22
c73b474 @lsegal Rack::Server should accept :app and override :config.
lsegal authored
23 it "overrides :config if :app is passed in" do
24 server = Rack::Server.new(:app => "FOO")
a13426b @raggi Squash test warnings
raggi authored
25 server.app.should.equal "FOO"
c73b474 @lsegal Rack::Server should accept :app and override :config.
lsegal authored
26 end
923e933 @raggi Add some basic Rack::Server tests, nothing like the oldies, but faster
raggi authored
27
3edd1e8 @udzura Add a rackup option for one-liner rack app server
udzura authored
28 should "prefer to use :builder when it is passed in" do
29 server = Rack::Server.new(:builder => "run lambda { |env| [200, {'Content-Type' => 'text/plain'}, ['success']] }")
a13426b @raggi Squash test warnings
raggi authored
30 server.app.class.should.equal Proc
3edd1e8 @udzura Add a rackup option for one-liner rack app server
udzura authored
31 Rack::MockRequest.new(server.app).get("/").body.to_s.should.equal 'success'
32 end
33
dc881c9 @jeremy Restore Rack::Server#middleware backward compatibility
jeremy authored
34 should "allow subclasses to override middleware" do
35 server = Class.new(Rack::Server).class_eval { def middleware; Hash.new [] end; self }
36 server.middleware['deployment'].should.not.equal []
37 server.new(:app => 'foo').middleware['deployment'].should.equal []
38 end
39
40 should "allow subclasses to override default middleware" do
41 server = Class.new(Rack::Server).instance_eval { def default_middleware_by_environment; Hash.new [] end; self }
42 server.middleware['deployment'].should.equal []
43 server.new(:app => 'foo').middleware['deployment'].should.equal []
44 end
45
46 should "only provide default middleware for development and deployment environments" do
47 Rack::Server.default_middleware_by_environment.keys.sort.should.equal %w(deployment development)
48 end
49
50 should "always return an empty array for unknown environments" do
923e933 @raggi Add some basic Rack::Server tests, nothing like the oldies, but faster
raggi authored
51 server = Rack::Server.new(:app => 'foo')
dc881c9 @jeremy Restore Rack::Server#middleware backward compatibility
jeremy authored
52 server.middleware['production'].should.equal []
923e933 @raggi Add some basic Rack::Server tests, nothing like the oldies, but faster
raggi authored
53 end
54
dc881c9 @jeremy Restore Rack::Server#middleware backward compatibility
jeremy authored
55 should "not include Rack::Lint in deployment environment" do
923e933 @raggi Add some basic Rack::Server tests, nothing like the oldies, but faster
raggi authored
56 server = Rack::Server.new(:app => 'foo')
dc881c9 @jeremy Restore Rack::Server#middleware backward compatibility
jeremy authored
57 server.middleware['deployment'].flatten.should.not.include(Rack::Lint)
923e933 @raggi Add some basic Rack::Server tests, nothing like the oldies, but faster
raggi authored
58 end
59
dc881c9 @jeremy Restore Rack::Server#middleware backward compatibility
jeremy authored
60 should "not include Rack::ShowExceptions in deployment environment" do
f667205 @rafaelfranca default_middleware_by_environment should always returns empty array f…
rafaelfranca authored
61 server = Rack::Server.new(:app => 'foo')
dc881c9 @jeremy Restore Rack::Server#middleware backward compatibility
jeremy authored
62 server.middleware['deployment'].flatten.should.not.include(Rack::ShowExceptions)
f667205 @rafaelfranca default_middleware_by_environment should always returns empty array f…
rafaelfranca authored
63 end
64
ccc542f @lenny Enable cleanup of Tempfiles from multipart form data by default
lenny authored
65 should "include Rack::TempfileReaper in deployment environment" do
66 server = Rack::Server.new(:app => 'foo')
67 server.middleware['deployment'].flatten.should.include(Rack::TempfileReaper)
68 end
69
923e933 @raggi Add some basic Rack::Server tests, nothing like the oldies, but faster
raggi authored
70 should "support CGI" do
71 begin
72 o, ENV["REQUEST_METHOD"] = ENV["REQUEST_METHOD"], 'foo'
73 server = Rack::Server.new(:app => 'foo')
74 server.server.name =~ /CGI/
75 Rack::Server.logging_middleware.call(server).should.eql(nil)
76 ensure
77 ENV['REQUEST_METHOD'] = o
78 end
79 end
80
9f0aa92 @ujifgc add quiet mode of rack server, rackup --quiet
ujifgc authored
81 should "be quiet if said so" do
82 server = Rack::Server.new(:app => "FOO", :quiet => true)
83 Rack::Server.logging_middleware.call(server).should.eql(nil)
84 end
85
923e933 @raggi Add some basic Rack::Server tests, nothing like the oldies, but faster
raggi authored
86 should "use a full path to the pidfile" do
87 # avoids issues with daemonize chdir
88 opts = Rack::Server.new.send(:parse_options, %w[--pid testing.pid])
89 opts[:pid].should.eql(::File.expand_path('testing.pid'))
90 end
91
d924f80 @smcgivern Prioritise CLI options over comment options
smcgivern authored
92 should "get options from ARGV" do
93 SPEC_ARGV[0..-1] = ['--debug', '-sthin', '--env', 'production']
94 server = Rack::Server.new
95 server.options[:debug].should.be.true
96 server.options[:server].should.equal 'thin'
97 server.options[:environment].should.equal 'production'
98 end
99
100 should "only override non-passed options from parsed .ru file" do
101 builder_file = File.join(File.dirname(__FILE__), 'builder', 'options.ru')
102 SPEC_ARGV[0..-1] = ['--debug', '-sthin', '--env', 'production', builder_file]
103 server = Rack::Server.new
104 server.app # force .ru file to be parsed
105
106 server.options[:debug].should.be.true
107 server.options[:server].should.equal 'thin'
108 server.options[:environment].should.equal 'production'
109 server.options[:Port].should.equal '2929'
110 end
111
923e933 @raggi Add some basic Rack::Server tests, nothing like the oldies, but faster
raggi authored
112 should "run a server" do
d924f80 @smcgivern Prioritise CLI options over comment options
smcgivern authored
113 pidfile = Tempfile.open('pidfile') { |f| break f }
114 FileUtils.rm pidfile.path
923e933 @raggi Add some basic Rack::Server tests, nothing like the oldies, but faster
raggi authored
115 server = Rack::Server.new(
116 :app => app,
96fd002 @raggi Undo test that falsely exemplifies production env
raggi authored
117 :environment => 'none',
d924f80 @smcgivern Prioritise CLI options over comment options
smcgivern authored
118 :pid => pidfile.path,
923e933 @raggi Add some basic Rack::Server tests, nothing like the oldies, but faster
raggi authored
119 :Port => TCPServer.open('127.0.0.1', 0){|s| s.addr[1] },
120 :Host => '127.0.0.1',
121 :daemonize => false,
122 :server => 'webrick'
123 )
2f06008 @raggi Rack::Server#start passes any given block through to Handler#run.
raggi authored
124 t = Thread.new { server.start { |s| Thread.current[:server] = s } }
125 t.join(0.01) until t[:server] && t[:server].status != :Stop
923e933 @raggi Add some basic Rack::Server tests, nothing like the oldies, but faster
raggi authored
126 body = open("http://127.0.0.1:#{server.options[:Port]}/") { |f| f.read }
127 body.should.eql('success')
128
129 Process.kill(:INT, $$)
130 t.join
d924f80 @smcgivern Prioritise CLI options over comment options
smcgivern authored
131 open(pidfile.path) { |f| f.read.should.eql $$.to_s }
923e933 @raggi Add some basic Rack::Server tests, nothing like the oldies, but faster
raggi authored
132 end
133
b4d7b95 @byroot Check if the PID in pidfile is still running #371
byroot authored
134 should "check pid file presence and running process" do
135 pidfile = Tempfile.open('pidfile') { |f| f.write($$); break f }.path
136 server = Rack::Server.new(:pid => pidfile)
137 server.send(:pidfile_process_status).should.eql :running
138 end
139
140 should "check pid file presence and dead process" do
141 dead_pid = `echo $$`.to_i
142 pidfile = Tempfile.open('pidfile') { |f| f.write(dead_pid); break f }.path
143 server = Rack::Server.new(:pid => pidfile)
144 server.send(:pidfile_process_status).should.eql :dead
145 end
146
147 should "check pid file presence and exited process" do
148 pidfile = Tempfile.open('pidfile') { |f| break f }.path
149 ::File.delete(pidfile)
150 server = Rack::Server.new(:pid => pidfile)
151 server.send(:pidfile_process_status).should.eql :exited
152 end
153
154 should "check pid file presence and not owned process" do
155 pidfile = Tempfile.open('pidfile') { |f| f.write(1); break f }.path
156 server = Rack::Server.new(:pid => pidfile)
157 server.send(:pidfile_process_status).should.eql :not_owned
158 end
159
85b50e2 @TimMoore Stop overwriting existing pidfiles.
TimMoore authored
160 should "not write pid file when it is created after check" do
161 pidfile = Tempfile.open('pidfile') { |f| break f }.path
162 ::File.delete(pidfile)
163 server = Rack::Server.new(:pid => pidfile)
164 ::File.open(pidfile, 'w') { |f| f.write(1) }
165 with_stderr do |err|
166 should.raise(SystemExit) do
167 server.send(:write_pid)
168 end
169 err.rewind
170 output = err.read
171 output.should.match(/already running/)
172 output.should.include? pidfile
173 end
174 end
175
edc8b92 @raggi Improve pidfile reporting and test coverage
raggi authored
176 should "inform the user about existing pidfiles with running processes" do
177 pidfile = Tempfile.open('pidfile') { |f| f.write(1); break f }.path
178 server = Rack::Server.new(:pid => pidfile)
179 with_stderr do |err|
180 should.raise(SystemExit) do
181 server.start
182 end
183 err.rewind
184 output = err.read
185 output.should.match(/already running/)
186 output.should.include? pidfile
187 end
188 end
189
c73b474 @lsegal Rack::Server should accept :app and override :config.
lsegal authored
190 end
Something went wrong with that request. Please try again.