Skip to content
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

get transforms to post if params given #2502

Closed
exocode opened this issue May 23, 2021 · 5 comments
Closed

get transforms to post if params given #2502

exocode opened this issue May 23, 2021 · 5 comments

Comments

@exocode
Copy link

exocode commented May 23, 2021

What Ruby, Rails and RSpec versions are you using?

Ruby version: ruby 2.7.2p137 (2020-10-01 revision 5445e04352) [x86_64-darwin20]
Rails version: Rails 6.1.3.1
RSpec version:

RSpec 3.10
  - rspec-core 3.10.1
  - rspec-expectations 3.10.1
  - rspec-mocks 3.10.2
  - rspec-rails 5.0.1
  - rspec-support 3.10.2

Observed behaviour

routes.rb

get '/users/:id/billing_addresses', to: "billing_addresses#index"

my spec.rb

 get "/users/#{user.id}/billing_addresses",
          params: { some_param: some_param }, # <---- transforms get request to a post request
          headers: valid_headers,
          as: :json

Started POST "/users/2/billing_addresses"

If I remove params: { some_param: some_param } like so:

 get "/users/#{user.id}/billing_addresses",
          headers: valid_headers,
          as: :json

it keeps a get request
Started GET "/users/2/billing_addresses"

Expected behaviour

 get "/users/#{user.id}/billing_addresses",
          headers: valid_headers,
          as: :json

should trigger

Started GET "/users/2/billing_addresses"

@pirj
Copy link
Member

pirj commented May 23, 2021

get is a Rails helper method. I suggest you to check with them.
GET requests semantically do not include body, and as: implies you're setting body's content type.
Do you mean to say "request JSON results"?

@benoittgt
Copy link
Member

Hello

Did you tried with the parameter as a hash?

# frozen_string_literal: true

require "bundler/inline"

gemfile(true) do
  source "https://rubygems.org"

  git_source(:github) { |repo| "https://github.com/#{repo}.git" }

  # Activate the gem you are reporting the issue against.
  gem "rails", "6.1.0"
  gem "rspec-rails"
end

require "rspec-rails"
require "action_controller/railtie"

class TestApp < Rails::Application
  config.root = __dir__
  config.hosts << "example.org"
  config.session_store :cookie_store, key: "cookie_store_key"
  secrets.secret_key_base = "secret_key_base"

  config.logger = Logger.new($stdout)
  Rails.logger  = config.logger

  routes.draw do
    get "/" => "test#index"
  end
end

class TestController < ActionController::Base
  include Rails.application.routes.url_helpers

  def index
    render plain: "Param : #{params["test"]}"
  end
end

require 'rspec/autorun'

RSpec.describe 'additions', type: :request do
  include Rack::Test::Methods

  let(:valid_headers) { { "Authorization" => "foo" } }

  it do
    get "/",
      { "test" => "Foo" },
      headers: valid_headers,
      as: :json
    expect(last_response.body).to include("Param : Foo")
  end

  private

  def app
    Rails.application
  end
end

@kucho
Copy link

kucho commented Oct 3, 2022

I was able to reproduce the issue with an identical code. What @pirj was saying is defined in here.

I tried @benoittgt's script and it worked. However, I couldn't get my test passing.

After checking line by line, I finally found it. Somehow this line include Rack::Test::Methods fixes my test case:

RSpec.describe "/movies", type: :request do
  include Rack::Test::Methods
  
  describe "GET /index" do
    it "returns the last page only" do
      Movie.create!(title: "Ignored Movie")
      batman_movie = Movie.create!(title: "Batman")
      get movies_url, {page: {size: 1, number: 2}}, as: :json
      movies = JSON.parse(last_response.body)["movies"]
      expect(movies.size).to eq 1
      expect(movies.last["title"]).to eq batman_movie.title
    end
  end
end

EDIT: I thought everything was passing but now request and response are empty 🤔. I had to use last_response from the previous script.


I'm running rspec main:

  %w[rspec-core rspec-expectations rspec-mocks rspec-rails rspec-support].each do |lib|
    gem lib, git: "https://github.com/rspec/#{lib}.git", branch: "main"
  end

Here's my locked version:

GIT
  remote: https://github.com/rspec/rspec-core.git
  revision: 71823ba11ec17a73b25bdc24ebab195494c270dc
  branch: main
  specs:
    rspec-core (3.12.0.pre)
      rspec-support (= 3.12.0.pre)

GIT
  remote: https://github.com/rspec/rspec-expectations.git
  revision: 311aaf245f2c5493572bf683b8c441cb5f7e44c8
  branch: main
  specs:
    rspec-expectations (3.12.0.pre)
      diff-lcs (>= 1.2.0, < 2.0)
      rspec-support (= 3.12.0.pre)

GIT
  remote: https://github.com/rspec/rspec-mocks.git
  revision: 2c70bfbb5bc0f59525032615c8fa989dd1ade243
  branch: main
  specs:
    rspec-mocks (3.12.0.pre)
      diff-lcs (>= 1.2.0, < 2.0)
      rspec-support (= 3.12.0.pre)

GIT
  remote: https://github.com/rspec/rspec-rails.git
  revision: 6b4969ef8e20cbd21c51840c158b91f90630041c
  branch: main
  specs:
    rspec-rails (6.0.0.pre)
      actionpack (>= 6.1)
      activesupport (>= 6.1)
      railties (>= 6.1)
      rspec-core (= 3.12.0.pre)
      rspec-expectations (= 3.12.0.pre)
      rspec-mocks (= 3.12.0.pre)
      rspec-support (= 3.12.0.pre)

GIT
  remote: https://github.com/rspec/rspec-support.git
  revision: 5d653dfd130d45a742ab2300662b624e191f76a0
  branch: main
  specs:
    rspec-support (3.12.0.pre)

@kucho
Copy link

kucho commented Oct 3, 2022

I don't think it's a bug anymore. The explanation is here: rails/rails@af1680f. I think it could be more visible somehow. Anyways, after deleting the explicit as: :json everything is back to normal.

@JonRowe JonRowe closed this as completed Oct 10, 2022
@JonRowe
Copy link
Member

JonRowe commented Oct 10, 2022

Closing as this seems the intended behaviour on Rails end.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants