<commit_before>require "spec_helper"

describe Sinatra::Schema::ParamParsing do
  before do
    @rack_app = Sinatra.new do
      helpers Sinatra::Schema::ParamParsing
      post("/") do
        MultiJson.encode(parse_params($properties))
      end
    end
  end

  describe "JSON encoded request params" do
    before do
      header "Content-Type", "application/json"
    end

    it "handles invalid json" do
      assert_raises(RuntimeError, "Invalid JSON") do
        post "/", "{"
      end
    end

    it "considers an empty body an empty hash" do
      post "/", ""
      assert_equal Hash.new, last_json
    end

    it "just parses the json" do
      params = { "foo" => "bar", "baz" => 42 }
      post "/", MultiJson.encode(params)
      assert_equal params, last_json
    end
  end

  describe "form-encoded params" do
    it "casts values according to the schema" do
      $properties = {
        some_text: Sinatra::Schema::Definition.new(type: "string"),
        some_bool: Sinatra::Schema::Definition.new(type: "boolean"),
      }
      post "/", some_text: "true", some_bool: "true"
      assert_equal({ "some_text" => "true", "some_bool" => true }, last_json)
    end

    it "leaves params without the corresponding property untouched" do
      $properies = {}
      params = { "foo" => "bar" }
      post "/", params
      assert_equal params, last_json
    end

    it "leaves null params" do<commit_msg>fix typo<commit_after>require "spec_helper"

describe Sinatra::Schema::ParamParsing do
  before do
    @rack_app = Sinatra.new do
      helpers Sinatra::Schema::ParamParsing
      post("/") do
        MultiJson.encode(parse_params($properties))
      end
    end
  end

  describe "JSON encoded request params" do
    before do
      header "Content-Type", "application/json"
    end

    it "handles invalid json" do
      assert_raises(RuntimeError, "Invalid JSON") do
        post "/", "{"
      end
    end

    it "considers an empty body an empty hash" do
      post "/", ""
      assert_equal Hash.new, last_json
    end

    it "just parses the json" do
      params = { "foo" => "bar", "baz" => 42 }
      post "/", MultiJson.encode(params)
      assert_equal params, last_json
    end
  end

  describe "form-encoded params" do
    it "casts values according to the schema" do
      $properties = {
        some_text: Sinatra::Schema::Definition.new(type: "string"),
        some_bool: Sinatra::Schema::Definition.new(type: "boolean"),
      }
      post "/", some_text: "true", some_bool: "true"
      assert_equal({ "some_text" => "true", "some_bool" => true }, last_json)
    end

    it "leaves params without the corresponding property untouched" do
      $properties = {}
      params = { "foo" => "bar" }
      post "/", params
      assert_equal params, last_json
    end

    it "leaves null params" do
{'fraction_non_alphanumeric': 0.11665495432185524, 'fraction_numerical': 0.0014054813773717498, 'mean_word_length': 2.622137404580153, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4481818', 'n_tokens_mistral': 883, 'n_tokens_neox': 851, 'n_words': 258}