Permalink
Browse files

tests for international package request and lookup

  • Loading branch information...
1 parent a9e7769 commit f3b92d8674ba7d14cd2b17b2bab56f114d2ceab8 Nicholas Yianilos committed Sep 17, 2012
@@ -0,0 +1,57 @@
+require 'spec_helper'
+
+module USPS::Request
+ describe InternationalShippingRatesLookup do
+ it "uses the IntlRateV2 API settings" do
+ InternationalShippingRatesLookup.tap do |klass|
+ klass.secure.should be_false
+ klass.api.should == 'IntlRateV2'
+ klass.tag.should == 'IntlRateV2Request'
+ end
+ end
+
+ it "requires at least one package" do
+ expect {
+ request = InternationalShippingRatesLookup.new
+ }.to raise_exception(ArgumentError)
+ end
+
+ let(:required_properties) do
+ {
+ :id => "42",
+ :country => "Japan",
+ :mail_type => "LargeEnvelope",
+ :pounds => 3,
+ :ounces => 5,
+ :container => 'RECTANGULAR',
+ :size => 'LARGE',
+ }
+ end
+
+ def package_xml_from_attributes(attributes)
+ package = Package::InternationalPackage.new(attributes)
+ request = InternationalShippingRatesLookup.new(package)
+ Nokogiri::XML.parse(request.build)
+ end
+
+ it "builds a valid XML request for USPS IntlRateV2" do
+ package_properties = required_properties.merge(
+ :height => 13,
+ :width => 10,
+ :length => 15
+ )
+ package_xml = package_xml_from_attributes(package_properties)
+
+ package_xml.search('Package').count.should == 1
+ first_package = package_xml.search('Package').first
+ first_package.attr('ID').should == "42"
+ first_package.search('Pounds').text.should == '3'
+ first_package.search('Ounces').text.should == '5'
+ first_package.search('Container').text.should == 'RECTANGULAR'
+ first_package.search('Size').text.should == 'LARGE'
+ first_package.search('Height').text.should == '13'
+ first_package.search('Width').text.should == '10'
+ first_package.search('Length').text.should == '15'
+ end
+ end
+end
@@ -1,20 +0,0 @@
-require 'spec_helper'
-
-module USPS::Request
- describe InternationalShippingRatesLookup do
- USPS.username = '414REENH3307'
- it "uses the RateV4 API settings" do
- package = Package::InternationalPackage.new(
- :country => "Romania", :id => 3, :mail_type => 'ALL', :pounds => 7, :ounces => 0, :container => 'RECTANGULAR', :size => 'LARGE',
- :width => 12, :height => 13, :length => 12
- )
- lookup = InternationalShippingRatesLookup.new(package)
- response = lookup.send!
- response.packages.each do |package|
- package.services.each do |service|
- puts "#{service.id} - #{service.rate} - #{service.description}"
- end
- end
- end
- end
-end
@@ -0,0 +1,41 @@
+require 'spec_helper'
+
+module USPS::Request::Package
+ describe USPS::Request::Package::InternationalPackage do
+ VALID_PROPERTIES = {
+ :id => 3,
+ :country => "France",
+ :mail_type => "Package",
+ :pounds => 5,
+ :ounces => 4,
+ :container => 'RECTANGULAR',
+ :size => 'REGULAR'
+ }
+
+ VALID_PROPERTIES.keys.each do |prop|
+ it "requires #{prop}" do
+ expect {
+ package = InternationalPackage.new(required_properties.dup.delete prop)
+ }.to raise_exception
+ end
+ end
+
+ it "properties can be set with an initialization hash and/or block" do
+ package = InternationalPackage.new :id => 3, :country => "France" do |p|
+ p.mail_type = "FlatRate"
+ p.pounds = 4
+ p.ounces = 0
+ p.container = "RECTANGULAR"
+ p.size = "REGULAR"
+ end
+
+ package.id.should == 3
+ package.country.should == "France"
+ package.mail_type.should == "FlatRate"
+ package.pounds.should == 4
+ package.ounces.should == 0
+ package.container.should == "RECTANGULAR"
+ package.size.should == "REGULAR"
+ end
+ end
+end

0 comments on commit f3b92d8

Please sign in to comment.