Skip to content

Commit

Permalink
Merge pull request #84 from NoRedInk/add-delete
Browse files Browse the repository at this point in the history
add delete
  • Loading branch information
mfonism authored Nov 12, 2024
2 parents cad0f73 + 4bfdc92 commit 8c93216
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# The Changelog

## Version 2.1.0: August 13, 2024
- adds `delete` method for simulating delete requests
- adds `set_authenticity_token` method for setting the authenticity token in the header & session

## Version 2.0.0: August 13, 2024
- drops support for Ruby 2.*
- adds `post` method for simulating post requests
Expand Down
12 changes: 12 additions & 0 deletions lib/rails_edge_test/dsl/edge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,18 @@ def perform_post(parameters={})
process(parameters)
end

def perform_delete(parameters={})
request.instance_variable_set(:@method, "DELETE")
request.env['REQUEST_METHOD'] = "DELETE"
process(parameters)
end

def set_authenticity_token
r = request
controller.instance_eval { @_request = r }
request.headers['X-CSRF-Token'] = controller.send :form_authenticity_token
end

def process(parameters={})
request.assign_parameters(
::Rails.application.routes,
Expand Down
2 changes: 1 addition & 1 deletion lib/rails_edge_test/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module RailsEdgeTest
VERSION = "2.0.0"
VERSION = "2.1.0"
end
53 changes: 52 additions & 1 deletion spec/rails_edge_test/dsl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ def complex
def post_action
render json: {hello: 'world'}
end

def delete_action
render json: {this: 'deletes'}
end
end

AnotherController = Class.new ActionController::Base do
Expand All @@ -29,7 +33,8 @@ def another
Rails.application.routes.draw do
get 'test/simple' => 'my#simple'
get 'test/complex' => 'my#complex'
get 'test/post_action' => 'my#post_action'
post 'test/post_action' => 'my#post_action'
delete 'test/delete_action' => 'my#delete_action'

get 'test/another' => 'another#another'
end
Expand Down Expand Up @@ -152,6 +157,52 @@ def another
expect(test_value[2].body).to eq({hello: 'world'}.to_json)
end

it "can perform a delete request" do
test_value = nil

Module.new do
extend RailsEdgeTest::Dsl

controller MyController do
action :delete_action do
edge "delete :delete_action" do
test_value = perform_delete
end
end
end
end

RailsEdgeTest::Dsl.execute!

expect(test_value[0]).to eq 200
expect(test_value[1]).to be_a Hash
expect(test_value[2]).to be_a ActionDispatch::Response::RackBody
expect(test_value[2].body).to eq({this: 'deletes'}.to_json)
end

it "can set authenticity token for the request" do
test_request = nil

Module.new do
extend RailsEdgeTest::Dsl

controller MyController do
action :simple do
edge "set the authenticity token" do
test_request = request

set_authenticity_token
end
end
end
end

allow_any_instance_of(MyController).to receive(:form_authenticity_token).and_return('a_test_token')
RailsEdgeTest::Dsl.execute!

expect(test_request.headers['X-CSRF-Token']).to eq 'a_test_token'
end

it "can incorporate request, session, and params when making a request" do
test_value = nil
expected_value = {
Expand Down

0 comments on commit 8c93216

Please sign in to comment.