spec/middleware_spec.rb in pdfkit-0.8.4 vs spec/middleware_spec.rb in pdfkit-0.8.4.1
- old
+ new
@@ -19,10 +19,39 @@
let(:headers) do
{'Content-Type' => "text/html"}
end
describe "#call" do
+
+ describe 'threadsafety' do
+ before { mock_app }
+ it 'is threadsafe' do
+ n = 30
+ extensions = Array.new(n) { rand > 0.5 ? 'html' : 'pdf' }
+ actual_content_types = Hash.new
+
+ threads = (0...n).map { |i|
+ Thread.new do
+ resp = get("http://www.example.org/public/test.#{extensions[i]}")
+ actual_content_types[i] = resp.content_type
+ end
+ }
+
+ threads.each(&:join)
+
+ extensions.each_with_index do |extension, index|
+ result = actual_content_types[index]
+ case extension
+ when 'html', 'txt', 'csv'
+ expect(result).to eq("text/#{extension}")
+ when 'pdf'
+ expect(result).to eq('application/pdf')
+ end
+ end
+ end
+ end
+
describe "caching" do
let(:headers) do
{
'Content-Type' => "text/html",
'ETag' => 'foo',
@@ -394,25 +423,7 @@
expect(root_url).to eq('http://example.net/')
expect(protocol).to eq('http')
end
end
- end
-
- it "does not get stuck rendering each request as pdf" do
- mock_app
- # false by default. No requests.
- expect(@app.send(:rendering_pdf?)).to eq(false)
-
- # Remain false on a normal request
- get 'http://www.example.org/public/file'
- expect(@app.send(:rendering_pdf?)).to eq(false)
-
- # Return true on a pdf request.
- get 'http://www.example.org/public/file.pdf'
- expect(@app.send(:rendering_pdf?)).to eq(true)
-
- # Restore to false on any non-pdf request.
- get 'http://www.example.org/public/file'
- expect(@app.send(:rendering_pdf?)).to eq(false)
end
end