vergilet / repost

Redirect using POST method
https://vergilet.github.io/repost
MIT License
82 stars 12 forks source link

RSpec Testing #28

Open caseyprovost opened 1 year ago

caseyprovost commented 1 year ago

Howdy! This is less of a bug and more of a question. I've been loving this gem as it solves some pains around a lot of the OAuth work I have been doing recently. Question is this: In system tests or controller RSpec tests...what is the best way to assert the behavior when the response is a redirect_post?

What I've done up until now is assert that the an instance of ApplicationController receives redirect_post with expected arguments, but it would be cool to assert that in a nicer way and even perhaps be able to stub that response so the controller still responds with a payload.

tejanium commented 1 month ago

Hi, I'm not the author, but what I did in my test was to abstract the follow redirect_post part.

In a test, a true normal redirect_to isn't usually followed as well. You have to explicitly call follow_redirect!

Since the redirect_post is a fake redirect, and just a form submission with JS, in my capybara tests, I've introduced a helper similar to follow_redirect!

Here's the implementation

def follow_post_redirect!
  # The POST redirect is just a form with JS submission.
  # This `redirect_post` is handled by https://github.com/vergilet/repost.
  # This helper simulates the JS auto-submission part and "follow" the redirect.
  # In the real browser, JS will submit the form instead of clicking a button
  click_on 'Continue'
end

so in my test, I can use it

it 'send the user to a correct page' do
  visit '/page/that/call/redirect_post'

  follow_post_redirect!

  assert_equal '/destination/page', current_page
end

I'm using minitest, but the concept should be the same