Back to Repositories

Testing Hash to Parameter Conversion Implementation in HTTParty

This test suite validates HTTParty’s hash conversion functionality, specifically focusing on parameter normalization and transformation for HTTP requests. The tests verify the library’s ability to convert Ruby hashes into URL-encoded parameter strings, handling nested structures and various data types.

Test Coverage Overview

The test suite provides comprehensive coverage of HTTParty’s hash conversion mechanisms:

  • Hash to URL parameter string conversion
  • Nested hash structure handling
  • Array parameter encoding
  • Empty array handling
  • Single value parameter conversion

Implementation Analysis

The testing approach utilizes RSpec’s describe/context/it blocks to organize test cases logically. Tests implement expectation-based assertions to verify the correct URL encoding of various data structures, including complex nested hashes with arrays and simple key-value pairs.

The implementation focuses on two main methods: to_params and normalize_param, testing their behavior with different input types.

Technical Details

Testing tools and configuration:

  • RSpec as the testing framework
  • spec_helper for test environment setup
  • HTTParty::HashConversions module under test
  • URL encoding validation
  • String comparison for parameter verification

Best Practices Demonstrated

The test suite demonstrates several testing best practices:

  • Hierarchical test organization using describe and context blocks
  • Clear test case isolation
  • Comprehensive edge case coverage
  • Descriptive test naming
  • Consistent assertion patterns

jnunemaker/httparty

spec/httparty/hash_conversions_spec.rb

            
require 'spec_helper'

RSpec.describe HTTParty::HashConversions do
  describe ".to_params" do
    it "creates a params string from a hash" do
      hash = {
        name: "bob",
        address: {
          street: '111 ruby ave.',
          city: 'ruby central',
          phones: ['111-111-1111', '222-222-2222']
        }
      }
      expect(HTTParty::HashConversions.to_params(hash)).to eq("name=bob&address%5Bstreet%5D=111%20ruby%20ave.&address%5Bcity%5D=ruby%20central&address%5Bphones%5D%5B%5D=111-111-1111&address%5Bphones%5D%5B%5D=222-222-2222")
    end

    context "nested params" do
      it 'creates a params string from a hash' do
        hash = { marketing_event: { marketed_resources: [ {type:"product", id: 57474842640 } ] } }
        expect(HTTParty::HashConversions.to_params(hash)).to eq("marketing_event%5Bmarketed_resources%5D%5B%5D%5Btype%5D=product&marketing_event%5Bmarketed_resources%5D%5B%5D%5Bid%5D=57474842640")
      end
    end
  end

  describe ".normalize_param" do
    context "value is an array" do
      it "creates a params string" do
        expect(
          HTTParty::HashConversions.normalize_param(:people, ["Bob Jones", "Mike Smith"])
        ).to eq("people%5B%5D=Bob%20Jones&people%5B%5D=Mike%20Smith&")
      end
    end

    context "value is an empty array" do
      it "creates a params string" do
        expect(
          HTTParty::HashConversions.normalize_param(:people, [])
        ).to eq("people%5B%5D=&")
      end
    end

    context "value is hash" do
      it "creates a params string" do
        expect(
          HTTParty::HashConversions.normalize_param(:person, { name: "Bob Jones" })
        ).to eq("person%5Bname%5D=Bob%20Jones&")
      end
    end

    context "value is a string" do
      it "creates a params string" do
        expect(
          HTTParty::HashConversions.normalize_param(:name, "Bob Jones")
        ).to eq("name=Bob%20Jones&")
      end
    end
  end
end