spec/vacuum/request_spec.rb in vacuum-0.0.1 vs spec/vacuum/request_spec.rb in vacuum-0.1.0

- old
+ new

@@ -1,105 +1,84 @@ require 'spec_helper' module Vacuum describe Request do - let(:req) { Request.new('us') } + let(:req) do + Request.new :locale => :us, + :key => 'foo', + :secret => 'bar', + :tag => 'baz' + end - describe '#<<' do - before do - req.configure do |c| - c.key = 'foo' - c.tag = 'bar' - end + describe ".new" do + it 'raises an error if key is missing' do + expect do + Request.new :secret => 'foo', + :tag => 'bar' + end.to raise_error MissingKey + end - req.reset! + it 'raises an error if secret is missing' do + expect do + Request.new :key => 'foo', + :tag => 'bar' + end.to raise_error MissingSecret end - it 'merges parameters into the query' do - req << { 'Key' => 'value' } - - req.params['Key'].should eql 'value' + it 'raises an error if tag is missing' do + expect do + Request.new :key => 'foo', + :secret => 'bar' + end.to raise_error MissingTag end - it 'camelizes keys' do - req << { :some_key => 'value' } - - req.params.should have_key 'SomeKey' + it 'raises an error if locale is not valid' do + expect do + Request.new :key => 'foo', + :secret => 'bar', + :tag => 'baz', + :locale => 'bad' + end.to raise_error BadLocale end + end - it 'leaves camelized keys as is' do - req << { 'SomeKey' => 'value' } - - req.params.should have_key 'SomeKey' + describe '#build' do + it 'merges parameters into the query' do + req.build 'Key' => 'value' + req.params['Key'].should eql 'value' end - it 'casts numeric values to string' do - req << { 'Key' => 1 } - + it 'casts values to string' do + req.build 'Key' => 1 req.params['Key'].should eql '1' - end - it 'converts array values to string' do - req << { 'Key' => ['foo', 'bar'] } - + req.build 'Key' => ['foo', 'bar'] req.params['Key'].should eql 'foo,bar' end - it 'removes whitespace after commas in values' do - req << { 'Key' => 'foo, bar' } - - req.params['Key'].should eql 'foo,bar' + it 'returns self' do + req.build({}).should eql req end end - describe '#configure' do - it 'yields the locale' do - req.configure(&:class).should eql Locale + describe '#build!' do + it 'clears existing query' do + req.build 'Key' => 'value' + req.params.should have_key 'Key' + + req.build!.params.should_not have_key 'Key' end end describe '#get' do - before do - req.configure do |c| - c.key = 'foo' - c.secret = 'bar' - c.tag = 'baz' - end - end - it 'returns a response' do req.get.should be_a Response end - - it 'raises an error if secret is missing' do - req.configure { |c| c.secret = nil } - - expect { req.get }.to raise_error MissingSecret - end end describe '#params' do - before do - req.configure do |c| - c.key = 'foo' - c.tag = 'bar' - end - end - - it 'raises an error if key is missing' do - req.configure { |c| c.key = nil } - - expect { req.params }.to raise_error MissingKey - end - - it 'raises an error if tag is missing' do - req.configure { |c| c.tag = nil } - - expect { req.params }.to raise_error MissingTag - end - - it 'includes common request parameters' do + it 'includes shared request parameters' do req.params['Service'].should eql 'AWSECommerceService' end it 'includes credentials' do req.params.should have_key 'AWSAccessKeyId' @@ -108,76 +87,44 @@ it 'includes a timestamp' do req.params['Timestamp'].should =~ /^\d+-\d+-\d+T\d+:\d+:\d+Z$/ end - context 'when no API version is specified' do + context 'when no API version is given' do it 'includes the current API version' do req.params['Version'].should eql Request::CURRENT_API_VERSION end end - context 'when an API version is specified' do - it 'includes the specified API version' do - req << { 'Version' => '1' } + context 'when an API version is given' do + it 'includes the given API version' do + req.build 'Version' => '1' req.params['Version'].should eql '1' end end end - describe '#reset!' do - before do - req.configure do |c| - c.key = 'foo' - c.tag = 'bar' - end - end - - it 'resets the request parameters' do - req << { 'Key' => 'value' } - req.params.should have_key 'Key' - - req.reset! - req.params.should_not have_key 'Key' - end - end - describe '#url' do - before do - req.configure do |c| - c.key = 'foo' - c.secret = 'bar' - c.tag = 'baz' - end - end - it 'builds a URL' do req.url.should be_a URI::HTTP end it 'canonicalizes the request parameters' do req.url.query.should match /\w+=\w+&/ end it 'sorts the request parameters' do - req << { 'A' => 1 } + req.build 'A' => 1 req.url.query.should match /^A=1&/ end it 'URL-encodes values' do - req << { :key => 'foo,bar' } + req.build 'Key' => 'foo,bar' req.url.query.should match /foo%2Cbar/ end - it 'signs the query' do + it 'is signed' do req.url.query.should match /&Signature=/ - end - - it 'raises an error if no secret is specified' do - expect do - req.configure { |c| c.secret = nil } - req.url - end.to raise_error MissingSecret end end end end