2

I'm trying to test a small Sinatra app using rspec. I want to pass a rather complex payload and am running into issues i do not understand: my payload contains an array of hashes. When I run the actual application this will work as expected, yet when I use the post helper to run my tests, the array will contain a merged hash:

post(
  "/#{bot}/webhook",
  sessionId: "test-session-#{session_counter}",
  result: {
    contexts: [
      { some: 'fixture' },
      { name: 'generic', parameters: { facebook_sender_id: 'zuck-so-cool' } }
    ]
  }
)

In the sinatra handler I use params to access this payload:

post '/:bot/webhook' do |bot|
  do_something_with(params)
end

When I now look at the structure of params when running the test suite, I will see the following structure:

[{"some" => "fixture", "name" => "generic", "parameters" => {"facebook_sender_id" => "zuck-so-cool"}}]

which I do not really understand. Is this a syntax issue (me being a ruby noob), am I using params wrong, or is this a bug?

EDIT: So i found out this is an "issue" with the way that Rack::Test will serialize the given payload when not specifying how to (i.e. as form data). If I pass JSON and pass the correct headers it will do what I expect it to do:

post(
  "/#{bot}/webhook",
  {
      sessionId: "test-session-#{session_counter}",
      result: {
        contexts: [
          { some: 'fixture' },
          { name: 'generic', parameters: { facebook_sender_id: 'zuck-so-cool' } }
        ]
      }
  }.to_json,
  { 'HTTP_ACCEPT' => 'application/json', 'CONTENT_TYPE' => 'application/json' }
)

Still I am unsure of this is an issue with the passed data structure not being possible to be serialized into form data or if this is a bug in the way that Rack::Test serializes data.

m90
  • 11,434
  • 13
  • 62
  • 112
  • Because when the data is transmitted from your sever to the client, it has to be in JSON. Javascript doesn't understand a ruby hash. – thesecretmaster Apr 08 '17 at 10:59
  • It doesn't have to be JSON. From what I understand `Rack::Test` will try to serialize the hash into form data in case you do not specify a content type. If it wouldn't "understand" a hash, no data at all would arrive. It's just serialized and decoded incorrectly in this case. – m90 Apr 08 '17 at 12:52

1 Answers1

1

Looking at the relevant portion of the specs it looks like this is is expected behavior.

m90
  • 11,434
  • 13
  • 62
  • 112