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

Query String passed when using POST #123

Closed
tannermares opened this issue Mar 20, 2014 · 9 comments
Closed

Query String passed when using POST #123

tannermares opened this issue Mar 20, 2014 · 9 comments

Comments

@tannermares
Copy link

Unless I am misunderstanding, the query_string parameter should only be passed on GET requests. I was having trouble this morning testing a POST due to it passing the query_string on top of my params that were being passed.

require 'spec_helper'
require 'rspec_api_documentation/dsl'

resource "User" do
  header 'Accept', 'application/json'
  header 'Content-Type', 'application/json'

  post "user/:id/get_my_foo.json" do
    parameter :foo_id, "Foo ID you want", required: true

    let(:id){ 1 }
    let)(:foo_id){ 2 }

    example_request 'Get the persons Foo' do
      # fails on request due to extra query_string
    end
  end
end

this is the error that occurs

Failure/Error: do_request
MultiJson::LoadError:
  795: unexpected token at 'foo_id=2'

I discovered I could resolve this by overriding the params by leting raw_post.

let(:raw_post){ {foo_id: 2}.to_json } 

I shouldn't have to do this according to the documentation though. Am I doing something wrong or is this a bug?

Thank you for your time. I love this project! Keep up the amazing work!

@tannermares tannermares changed the title query_string created when using POST query_string created when using POST Mar 20, 2014
@tannermares tannermares changed the title query_string created when using POST Query String created when using POST Mar 20, 2014
@tannermares tannermares changed the title Query String created when using POST Query String passed when using POST Mar 20, 2014
@oestrich
Copy link
Contributor

This is failing because you're doing

  header 'Content-Type', 'application/json'

Rails is expecting the POST body to be JSON, instead it's sending application/x-www-form-urlencoded. It's not actually the query string. You can make this nicer by doing

let(:raw_post) { params.to_json }

RAD will take care of generating that params hash for you.

@oliverbarnes
Copy link

@oestrich I ran into the same problem - if the content-type is json, shouldn't RAD default to posting the params in json accordingly?

@andreasklinger
Copy link

FYI: I would have assumed the same

The whole raw_post approach feels like a hack.

  • I can't let() withing an example.
  • I don't really wanna define low-level post values. I actually just want to pass parameters

@andreasklinger
Copy link

FYI i wrote a monkey patch for this in my app (normally i would do a fork/fix/PR but we have too many conflicting gems right now with rad master so i just did this for now - but maybe it helps somebody or can be intro'd into master)

./monkey_patch_rad.rb:

module RspecApiDocumentation::DSL::Endpoint
  def do_request(extra_params = {})
    @extra_params = extra_params

    params_or_body = nil
    path_or_query = path

    if method == :get && !query_string.blank?
      path_or_query += "?#{query_string}"
    else
      if respond_to?(:raw_post)
        params_or_body = raw_post
      elsif headers['Content-Type'] == 'application/json'
        params_or_body = params.to_json
      else
        params_or_body = params
      end
    end

    rspec_api_documentation_client.send(method, path_or_query, params_or_body, headers)
  end
end

and in the specs (or in some helper)

require 'rspec_api_documentation/dsl'
require_relative '../monkey_patch_rad.rb'

@oestrich
Copy link
Contributor

oestrich commented Jul 9, 2014

We've talked about it before and I don't think I want to have RAD peak into headers and change the post body for you. I prefer doing a shared context so it is more clear and less "magical."

shared_context :json do
  header "Content-Type", "application/json"
  let(:raw_post) { params.to_json }
end

describe "Orders" do
  include_context :json
  post "/orders" do
    example_request "Creating an order" do
      # ...
    end
  end
end

@andreasklinger
Copy link

thanks - will think about it.

personally i see "request bodys are json" more like a configuration setting for my http client than a flag for my tests. therefore i would be personally ok with a bit of magic (or setting)

@oestrich
Copy link
Contributor

oestrich commented Jul 9, 2014

@andreasklinger What do you think of #142?

@andreasklinger
Copy link

love it. i think that's the way to go

@oestrich
Copy link
Contributor

Closing this now that #142 is on master.

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

4 participants