Skip to content
This repository
Browse code

Refactor request json params parsing tests

  • Loading branch information...
commit 38a723e6ea770eb8a902ee85ef40f6a22e7cd1b8 1 parent ac4bf11
Joshua Peek josh authored
45 actionpack/test/controller/request/json_params_parsing_test.rb
... ... @@ -0,0 +1,45 @@
  1 +require 'abstract_unit'
  2 +
  3 +class JsonParamsParsingTest < ActionController::IntegrationTest
  4 + class TestController < ActionController::Base
  5 + class << self
  6 + attr_accessor :last_request_parameters
  7 + end
  8 +
  9 + def parse
  10 + self.class.last_request_parameters = request.request_parameters
  11 + head :ok
  12 + end
  13 + end
  14 +
  15 + def teardown
  16 + TestController.last_request_parameters = nil
  17 + end
  18 +
  19 + test "parses json params for application json" do
  20 + assert_parses(
  21 + {"person" => {"name" => "David"}},
  22 + "{\"person\": {\"name\": \"David\"}}", { 'CONTENT_TYPE' => 'application/json' }
  23 + )
  24 + end
  25 +
  26 + test "parses json params for application jsonrequest" do
  27 + assert_parses(
  28 + {"person" => {"name" => "David"}},
  29 + "{\"person\": {\"name\": \"David\"}}", { 'CONTENT_TYPE' => 'application/jsonrequest' }
  30 + )
  31 + end
  32 +
  33 + private
  34 + def assert_parses(expected, actual, headers = {})
  35 + with_routing do |set|
  36 + set.draw do |map|
  37 + map.connect ':action', :controller => "json_params_parsing_test/test"
  38 + end
  39 +
  40 + post "/parse", actual, headers
  41 + assert_response :ok
  42 + assert_equal(expected, TestController.last_request_parameters)
  43 + end
  44 + end
  45 +end
22 actionpack/test/controller/request_test.rb
@@ -764,25 +764,3 @@ def parse_body(body)
764 764 ActionController::Request.new(env).request_parameters
765 765 end
766 766 end
767   -
768   -class JsonParamsParsingTest < ActiveSupport::TestCase
769   - def test_hash_params_for_application_json
770   - person = parse_body({:person => {:name => "David"}}.to_json,'application/json')[:person]
771   - assert_kind_of Hash, person
772   - assert_equal 'David', person['name']
773   - end
774   -
775   - def test_hash_params_for_application_jsonrequest
776   - person = parse_body({:person => {:name => "David"}}.to_json,'application/jsonrequest')[:person]
777   - assert_kind_of Hash, person
778   - assert_equal 'David', person['name']
779   - end
780   -
781   - private
782   - def parse_body(body,content_type)
783   - env = { 'rack.input' => StringIO.new(body),
784   - 'CONTENT_TYPE' => content_type,
785   - 'CONTENT_LENGTH' => body.size.to_s }
786   - ActionController::Request.new(env).request_parameters
787   - end
788   -end

0 comments on commit 38a723e

Please sign in to comment.
Something went wrong with that request. Please try again.