Skip to content

Commit

Permalink
test: pass in rack env rather than just request method to build_resource
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Jun 8, 2022
1 parent 9b159f8 commit eb163ed
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
21 changes: 18 additions & 3 deletions lib/webmachine/describe_routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,30 @@ def route_param_names
path_spec.select { | component | component.is_a?(Symbol) }
end

def build_resource(http_method, application_context, potential_params)
path_params = route_param_names.each_with_object({}){ | name, new_params | new_params[name] = potential_params[name] }
# Creates a Webmachine Resource for the given route for use in tests.
# @param [Hash] env the rack env from which to build the request
# @param [PactBroker::ApplicationContext] application_context the application context
# @param [Hash] path_param_values concrete parameter values from which to construct the path
# @return [Webmachine::Resource] the webmachine resource for the request
def build_resource(env, application_context, path_param_values)
path = "/" + path_spec.collect{ | part | part.is_a?(Symbol) ? (path_param_values[part] || "missing-param") : part }.join("/")

path_params = route_param_names.each_with_object({}){ | name, new_params | new_params[name] = path_param_values[name] }
path_info = {
application_context: application_context,
resource_name: resource_name
}.merge(path_params)

dummy_request = Webmachine::Adapters::Rack::RackRequest.new(http_method, "/", Webmachine::Headers["host" => "example.org"], nil, {}, {}, { "REQUEST_METHOD" => http_method })
rack_req = ::Rack::Request.new({ "REQUEST_METHOD" => "GET", "rack.input" => StringIO.new("") }.merge(env) )
dummy_request = Webmachine::Adapters::Rack::RackRequest.new(
rack_req.env["REQUEST_METHOD"],
path,
Webmachine::Headers.from_cgi({"HTTP_HOST" => "example.org"}.merge(env)),
Webmachine::Adapters::Rack::RequestBody.new(rack_req),
{},
{},
rack_req.env
)
dummy_request.path_info = path_info
resource_class.new(dummy_request, Webmachine::Response.new)
end
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/pact_broker/api/resources/all_routes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
end

it "has a policy record object" do
dummy_resource = pact_broker_route.build_resource(allowed_method, PactBroker::ApplicationContext.default_application_context, POTENTIAL_PARAMS)
dummy_resource = pact_broker_route.build_resource({ "REQUEST_METHOD" => allowed_method }, PactBroker::ApplicationContext.default_application_context, POTENTIAL_PARAMS)
expect(dummy_resource.policy_record).to_not be nil
end
end
Expand Down

0 comments on commit eb163ed

Please sign in to comment.