Please consider this test:
def test_ok_on_second_request
bad_response = @request.get "/bad-response"
assert_equal 404, bad_response.status
good_response = @request.get "/test-title"
assert_equal 200, good_response.status
assert_equal "text/html", good_response.content_type
end
I have assured that /test-title is a valid path. The assertion that’s supposed to return 200 is in fact returning 404. How is Rack behaving in order to return two different results for the same request?
This is the code for the Server class inside the project:
module Blogrite
class Server
attr_accessor :status, :mimetype, :body, :provider
def initialize *args, &block
@status, @mimetype = 200, "text/html"
provider = args[0][:with].nil? ? :filesystem : args[0][:with]
@provider = Blogrite.const_get(provider.capitalize).new
# p "Server is running with #{@provider.class}."
end
def call env
begin
article = go env['PATH_INFO'].delete("/")
rescue Blogrite::Article::NoBodyError
@status = 404
end
@status = 404 if !article
@status = 403 if env["REQUEST_METHOD"] == 'POST'
@mimetype = "text/css" if env["PATH_INFO"].include?("css")
@body = if article then article.render
elsif env.respond_to?(:to_yaml) then "<pre>#{env.to_yaml}</pre>"
else "oops"
end
[@status,{ "Content-Type" => @mimetype},[@body]]
end
def go path
f = @provider.fetch path
Article.parse f unless f.nil?
end
end
end
The whole workflow is too big for me to paste it in but you can check the project out on Github. I appreciate your help, thank you.
The solution for the problem is as simple as initializing
@statusinside thecallfunction.That way the rack instance – that is called only once – stays out of the request’s way. Every call function should have its own defaults, not the server class.
Thanks to @rubenfonseca for helping me out.