relax 0.0.7 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/spec/query_spec.rb DELETED
@@ -1,60 +0,0 @@
1
- require File.dirname(__FILE__) + '/spec_helper'
2
-
3
- require 'relax/query'
4
-
5
- describe 'a query' do
6
- before(:each) do
7
- @uri = URI::parse('http://example.com/?action=search&query=keyword')
8
- @query = Relax::Query.new
9
- end
10
-
11
- it 'should convert to a query string' do
12
- @query[:action] = 'Search'
13
- @query[:query] = 'strings'
14
- @query.to_s.should eql('action=Search&query=strings')
15
- end
16
-
17
- it 'should convert its values to strings' do
18
- date = Date.today
19
- @query[:date] = date
20
- @query.to_s.should eql("date=#{date.to_s}")
21
- end
22
-
23
- it 'should escape its values using "+" instead of "%20"' do
24
- Relax::Query.send(:escape_value, 'two words').should == 'two+words'
25
- end
26
-
27
- it 'should sort its parameters' do
28
- @query[:charlie] = 3
29
- @query[:alpha] = 1
30
- @query[:bravo] = 2
31
- @query.to_s.should eql('alpha=1&bravo=2&charlie=3')
32
- end
33
-
34
- it 'should encode its parameter values' do
35
- @query[:spaces] = 'two words'
36
- @query[:url] = 'http://example.com/'
37
- @query.to_s.should eql('spaces=two+words&url=http%3A%2F%2Fexample.com%2F')
38
- end
39
-
40
- it 'should be able to parse query strings' do
41
- parsed_query = Relax::Query.parse(@uri)
42
- parsed_query[:action].should eql('search')
43
- parsed_query[:query].should eql('keyword')
44
- end
45
-
46
- it 'should parse key value pairs into only two parts' do
47
- parsed_query = Relax::Query.parse(URI.parse("http://example.com/?action=test=&foo=bar"))
48
- parsed_query[:action].should eql('test=')
49
- end
50
-
51
- it 'should unescape query string key-value pair keys' do
52
- parsed_query = Relax::Query.parse(URI.parse("http://example.com/?action%20helper=test"))
53
- parsed_query[:"action helper"].should eql('test')
54
- end
55
-
56
- it 'should unescape query string key-value pair values' do
57
- parsed_query = Relax::Query.parse(URI.parse("http://example.com/?action=test%20action"))
58
- parsed_query[:action].should eql('test action')
59
- end
60
- end
data/spec/request_spec.rb DELETED
@@ -1,114 +0,0 @@
1
- require File.dirname(__FILE__) + '/spec_helper'
2
-
3
- require 'relax/request'
4
-
5
- class Amount < Relax::Request
6
- parameter :amount
7
- parameter :currency
8
- end
9
-
10
- class TestRequest < Relax::Request
11
- parameter :action, :required => true
12
- parameter :token_id
13
- parameter :user_id
14
- parameter :amount, :type => Amount
15
- end
16
-
17
- class ChildRequest < TestRequest
18
- parameter :child_id
19
- end
20
-
21
- describe 'an option initialized request', :shared => true do
22
- it 'should have its values set by the options hash' do
23
- request = TestRequest.new(:action => 'FetchAll', :token_id => 123)
24
- request.action.should eql('FetchAll')
25
- request.token_id.should eql(123)
26
- request.user_id.should be_nil
27
- end
28
- end
29
-
30
- describe 'a request that converts to a query', :shared => true do
31
- before(:each) do
32
- @query = TestRequest.new(:action => 'Search', :token_id => 123).to_query
33
- end
34
-
35
- it 'should include its parameters in the query' do
36
- @query[:action].should eql('Search')
37
- @query[:token_id].should eql('123')
38
- @query[:user_id].should be_nil
39
- @query[:amount].should be_nil
40
- end
41
-
42
- it 'should only include parameters in the query if they are set' do
43
- @query.key?(:action).should be_true
44
- @query.key?(:token_id).should be_true
45
- @query.key?(:user_id).should be_false
46
- @query.key?(:amount).should be_false
47
- end
48
- end
49
-
50
- describe 'a normal request' do
51
- it_should_behave_like 'a request that converts to a query'
52
- it_should_behave_like 'an option initialized request'
53
-
54
- it 'should raise an exception if a required parameter is missing' do
55
- lambda {
56
- TestRequest.new(:token_id => 123).valid?
57
- }.should raise_error(Relax::MissingParameter)
58
- end
59
- end
60
-
61
- describe 'a template request' do
62
- it_should_behave_like 'a request that converts to a query'
63
- it_should_behave_like 'an option initialized request'
64
-
65
- before(:each) do
66
- # this syntax may need to go away unless we can find a way to make it work
67
- TestRequest[:api_key] = '123456'
68
- TestRequest[:secret] = 'shhh!'
69
- end
70
-
71
- it 'should always have the template values in its query' do
72
- request = TestRequest.new
73
- request.api_key.should eql('123456')
74
- request.secret.should eql('shhh!')
75
- end
76
-
77
- it 'should allow its template variables to be overridden' do
78
- request = TestRequest.new(:secret => 'abracadabra')
79
- request.api_key.should eql('123456')
80
- request.secret.should eql('abracadabra')
81
- end
82
-
83
- it 'should pass its template on to its children' do
84
- request = ChildRequest.new
85
- request.api_key.should eql('123456')
86
- request.secret.should eql('shhh!')
87
- end
88
-
89
- it 'should allow template parameters on its children that are additive' do
90
- ChildRequest[:query] = '1a2b3c'
91
- child = ChildRequest.new
92
- child.api_key.should eql('123456')
93
- child.secret.should eql('shhh!')
94
- child.query.should eql('1a2b3c')
95
-
96
- parent = TestRequest.new
97
- parent.api_key.should eql('123456')
98
- parent.secret.should eql('shhh!')
99
- parent.respond_to?(:query).should be_false
100
- end
101
- end
102
-
103
- describe 'a request with a custom type' do
104
- before(:each) do
105
- request = TestRequest.new(:action => 'Pay', :token_id => 123)
106
- request.amount = Amount.new(:amount => 3.50, :currency => 'USD')
107
- @query = request.to_query
108
- end
109
-
110
- it 'should add the type parameters to the query' do
111
- @query.key?(:"amount.amount").should be_true
112
- @query.key?(:"amount.currency").should be_true
113
- end
114
- end
@@ -1,98 +0,0 @@
1
- require File.dirname(__FILE__) + '/spec_helper'
2
-
3
-
4
- class BaseResponse < Relax::Response
5
- parameter :status, :required => true
6
- parameter :request_id, :element => :requestid, :type => :integer
7
- end
8
-
9
- class TestResponse < BaseResponse
10
- class Token < Relax::Response
11
- parameter :token_id, :element => :tokenid
12
- parameter :status
13
- end
14
-
15
- class Error < Relax::Response
16
- parser :hpricot
17
- parameter :code, :type => :integer
18
- parameter :message
19
- end
20
-
21
- parameter :valid_request, :element => :requestid, :attribute => :valid
22
- parameter :tokens, :collection => Token
23
- parameter :error, :type => Error
24
- end
25
-
26
-
27
- describe 'a response' do
28
- before(:each) do
29
- @response = Relax::Response.new(XML)
30
- end
31
-
32
- it 'should allow access to the root' do
33
- root = @response.root
34
- root.should be_an_instance_of(Hpricot::Elem)
35
- root.name.should eql('RESTResponse')
36
- end
37
-
38
- it 'should be checkable by the name of its root' do
39
- @response.is?(:RESTResponse).should be_true
40
- end
41
-
42
- it 'should allow access to an element by its name' do
43
- @response.element(:RequestId).should be_an_instance_of(Hpricot::Elem)
44
- end
45
-
46
- it 'should allow access to an element\'s elements by its name' do
47
- tokens = @response.elements(:Tokens)
48
- tokens.should be_an_instance_of(Hpricot::Elements)
49
- tokens.should_not be_empty
50
- end
51
-
52
- it 'should allow access to an element\'s value by its name' do
53
- token = Relax::Response.new(@response.elements(:Tokens).first)
54
- token.element(:TokenId).inner_text.should eql('JPMQARDVJK')
55
- token.element(:Status).inner_text.should eql('Active')
56
- end
57
-
58
- it 'should have a means of checking for the existence of a node' do
59
- @response.has?(:Status).should_not be_nil
60
- @response.has?(:Errors).should be_nil
61
- end
62
-
63
- it 'should be able to define children of Response without modifying parent' do
64
- Relax::Response.new(XML).respond_to?(:status).should be_false
65
- TestResponse.new(XML).respond_to?(:status).should be_true
66
- end
67
-
68
- it 'should automatically pull parameters from the XML' do
69
- response = TestResponse.new(XML)
70
- response.valid_request.should eql('true')
71
- response.tokens.length.should eql(2)
72
- response.tokens.first.status.should eql('Active')
73
- response.error.code.should eql(1)
74
- response.error.message.should eql('Failed')
75
- end
76
-
77
- it "should automatically pull its parent's parameters from the XML" do
78
- response = TestResponse.new(XML)
79
- response.status.should eql('Success')
80
- response.request_id.should eql(44287)
81
- end
82
-
83
- it 'should be relationally equivalent to its children' do
84
- (Relax::Response === TestResponse).should be_true
85
- end
86
-
87
- it 'should raise MissingParameter if required parameters are missing' do
88
- proc { TestResponse.new('') }.should raise_error(Relax::MissingParameter)
89
- end
90
-
91
- it 'should use the default parser when undefined' do
92
- TestResponse::Token.new('').parser_name.should ==:default
93
- end
94
-
95
- it 'should use the defined parser when given' do
96
- TestResponse::Error.new('').parser_name.should ==:hpricot
97
- end
98
- end
@@ -1,67 +0,0 @@
1
- require File.dirname(__FILE__) + '/spec_helper'
2
-
3
- require 'relax/symbolic_hash'
4
-
5
- describe 'a symbolic hash' do
6
- before(:each) do
7
- @url = 'http://example.com/'
8
- @query = Relax::SymbolicHash.new
9
- end
10
-
11
- it 'should be accessible via string or symbol keys' do
12
- @query[:amount] = 10
13
- @query[:amount].should == 10
14
- @query['amount'].should == 10
15
- end
16
-
17
- it 'should convert keys to symbols' do
18
- @query['symbol'] = 'aleph'
19
- @query[:symbol].should == 'aleph'
20
- end
21
-
22
- it 'should convert keys to symbols' do
23
- @query['symbol'] = 'aleph'
24
- @query[:symbol].should == 'aleph'
25
- end
26
-
27
- it 'should test for keys by symbol' do
28
- @query[:symbol] = 'aleph'
29
- @query.key?('symbol').should be_true
30
- end
31
-
32
- it 'should delete values with a symbolic key' do
33
- @query[:symbol] = 'aleph'
34
- @query.delete('symbol')
35
- @query.key?(:symbol).should be_false
36
- end
37
-
38
- it 'should be mergeable' do
39
- @query[:one] = 2
40
- merged_query = @query.merge({ :one => 1, :two => 2 })
41
- merged_query[:one].should == 1
42
- merged_query[:two].should == 2
43
- end
44
-
45
- it 'should be able to duplicate itself' do
46
- @query[:one] = 'uno'
47
- @query[:two] = 'dos'
48
- new_query = @query.dup
49
- new_query[:one].should == 'uno'
50
- new_query[:two].should == 'dos'
51
-
52
- @query[:three] == 'tres'
53
- new_query.key?(:three).should be_false
54
- end
55
-
56
- it 'should be able to get multiple values by symbol' do
57
- @query[:one] = 1
58
- @query[:two] = 2
59
- @query.values_at(:one, :two).should == [1, 2]
60
- end
61
-
62
- it 'should be instantiable with a hash' do
63
- query = Relax::SymbolicHash.new({ :one => 1, :two => 2 })
64
- query[:one].should == 1
65
- query[:two].should == 2
66
- end
67
- end