Skip to content

add delete #84

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ... don't really know how to test this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does something like this look good?

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

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
Loading