active_forms 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -91,14 +91,30 @@ module ActiveForms
91
91
  end
92
92
 
93
93
  def verify_response(response)
94
- ActiveForms.log("[ActiveForms] Response: #{response}")
95
- if response["error"]
96
- begin
97
- klass = ("ActiveForms::" << response["error"]["code"].downcase.camelize).constantize
98
- raise klass.new(response["error"]["message"])
99
- rescue NameError, NoMethodError
100
- raise("Unknown exception: #{response["error"].inspect}")
94
+ exception = nil
95
+
96
+ begin
97
+ # force parsing response to detect parsing errors
98
+ response.parsed_response
99
+
100
+ ActiveForms.log("[ActiveForms] Response: #{response}")
101
+ if response["error"]
102
+ begin
103
+ klass = ("ActiveForms::" << response["error"]["code"].downcase.camelize).constantize
104
+ exception = klass.new(response["error"]["message"])
105
+ rescue NameError, NoMethodError
106
+ exception = Error.new("Unknown exception: #{response["error"].inspect}")
107
+ end
108
+ elsif response.response.code !~ /^20\d$/
109
+ exception = Error.new("Request returned status #{response.response.code} and does include properly formatted error message.")
101
110
  end
111
+ rescue MultiXml::ParseError
112
+ exception = ParseError.new($!.message)
113
+ end
114
+
115
+ unless exception.nil?
116
+ exception.response = response
117
+ raise exception
102
118
  end
103
119
  end
104
120
  end
@@ -1,3 +1,3 @@
1
1
  module ActiveForms
2
- VERSION = "0.3.0"
2
+ VERSION = "0.3.1"
3
3
  end
data/lib/active_forms.rb CHANGED
@@ -34,7 +34,9 @@ module ActiveForms
34
34
  end
35
35
  end
36
36
 
37
- class Error < StandardError; end
37
+ class Error < StandardError
38
+ attr_accessor :response
39
+ end
38
40
 
39
41
  class ApiKeyInvalid < Error; end
40
42
  class ApiSigInvalid < Error; end
@@ -56,6 +58,7 @@ module ActiveForms
56
58
  class MissingVendor < Error; end
57
59
  class NullResponse < Error; end
58
60
  class OptionNotFound < Error; end
61
+ class ParseError < Error; end
59
62
  class ResourceNotSupported < Error; end
60
63
  class TemplateNotFound < Error; end
61
64
  class Unauthorized < Error; end
@@ -38,7 +38,7 @@ class ActiveForms::ApplicationPrintTest < Test::Unit::TestCase
38
38
 
39
39
  context "when sent ActiveForms::ApplicationPrint.find with form code and application number" do
40
40
  setup do
41
- stub_get(/applicationprint/, success_response("get_applicationprint"))
41
+ stub_get(/applicationprint/, success_response("get_applicationprint.xml"))
42
42
  @application_print = ActiveForms::ApplicationPrint.find(:apiFormCode => "credit_card_application", :apiNumber => "23456")
43
43
  end
44
44
 
@@ -36,7 +36,7 @@ class ActiveForms::ApplicationTest < Test::Unit::TestCase
36
36
 
37
37
  context "when sent ActiveForms::Application.all with multi-response" do
38
38
  setup do
39
- stub_get(/applications/, success_response("get_applications"))
39
+ stub_get(/applications/, success_response("get_applications.xml"))
40
40
  @applications = ActiveForms::Application.all
41
41
  end
42
42
 
@@ -67,7 +67,7 @@ class ActiveForms::ApplicationTest < Test::Unit::TestCase
67
67
 
68
68
  context "when sent ActiveForms::Application.all with single-response" do
69
69
  setup do
70
- stub_get(/applications/, success_response("get_applications_single"))
70
+ stub_get(/applications/, success_response("get_applications_single.xml"))
71
71
  @applications = ActiveForms::Application.all
72
72
  end
73
73
 
@@ -38,7 +38,7 @@ class ActiveForms::EntryPrintoutTest < Test::Unit::TestCase
38
38
 
39
39
  context "when sent ActiveForms::EntryPrintout.find with form code and application number" do
40
40
  setup do
41
- stub_get(/entryprintout/, success_response("get_entryprintout"))
41
+ stub_get(/entryprintout/, success_response("get_entryprintout.xml"))
42
42
  @entry_printout = ActiveForms::EntryPrintout.find(:formCode => "credit_card_entry", :number => "23456")
43
43
  end
44
44
 
@@ -36,7 +36,7 @@ class ActiveForms::EntryTest < Test::Unit::TestCase
36
36
 
37
37
  context "when sent ActiveForms::Entry.all with multi-response" do
38
38
  setup do
39
- stub_get(/entries/, success_response("get_entries"))
39
+ stub_get(/entries/, success_response("get_entries.xml"))
40
40
  @entries = ActiveForms::Entry.all
41
41
  end
42
42
 
@@ -67,7 +67,7 @@ class ActiveForms::EntryTest < Test::Unit::TestCase
67
67
 
68
68
  context "when sent ActiveForms::Entry.all with single-response" do
69
69
  setup do
70
- stub_get(/entries/, success_response("get_entries_single"))
70
+ stub_get(/entries/, success_response("get_entries_single.xml"))
71
71
  @entries = ActiveForms::Entry.all
72
72
  end
73
73
 
@@ -87,7 +87,7 @@ class ActiveForms::EntryTest < Test::Unit::TestCase
87
87
 
88
88
  context "when post entry data by ActiveForms::Entry.create" do
89
89
  setup do
90
- stub_post(/entrydata/, success_response("post_entrydata"))
90
+ stub_post(/entrydata/, success_response("post_entrydata.xml"))
91
91
  @entry = ActiveForms::Entry.create :apiFormCode => 'credit_card_entry'
92
92
  end
93
93
 
@@ -109,7 +109,7 @@ class ActiveForms::EntryTest < Test::Unit::TestCase
109
109
  setup do
110
110
  @response = { :status => ["204", "No content"], :content_type => "text/xml"}
111
111
  stub_delete(/entrydata/, @response)
112
- stub_get(/entries/, success_response("get_entries"))
112
+ stub_get(/entries/, success_response("get_entries.xml"))
113
113
  @entries = ActiveForms::Entry.all
114
114
  end
115
115
 
@@ -10,7 +10,7 @@ class ActiveForms::FormTest < Test::Unit::TestCase
10
10
 
11
11
  context "when sent ActiveForms::Form.all with multi-response" do
12
12
  setup do
13
- stub_get(/forms/, success_response("get_forms"))
13
+ stub_get(/forms/, success_response("get_forms.xml"))
14
14
  @forms = ActiveForms::Form.all
15
15
  end
16
16
 
@@ -41,7 +41,7 @@ class ActiveForms::FormTest < Test::Unit::TestCase
41
41
 
42
42
  context "when sent ActiveForms::Form.all with single-response" do
43
43
  setup do
44
- stub_get(/forms/, success_response("get_forms_single"))
44
+ stub_get(/forms/, success_response("get_forms_single.xml"))
45
45
  @forms = ActiveForms::Form.all
46
46
  end
47
47
 
@@ -10,7 +10,7 @@ class ActiveForms::FormVersionTest < Test::Unit::TestCase
10
10
 
11
11
  context "when send ActiveForms::FormVersion.all with multi-response" do
12
12
  setup do
13
- stub_get(/forminfo/, success_response("get_forminfo"))
13
+ stub_get(/forminfo/, success_response("get_forminfo.xml"))
14
14
  @versions = ActiveForms::FormVersion.all :apiFormCode => 'hipoteka'
15
15
  end
16
16
 
@@ -111,5 +111,31 @@ class ActiveForms::RequestTest < Test::Unit::TestCase
111
111
  assert_equal @response.parsed_response, request.perform.parsed_response
112
112
  end
113
113
  end
114
+
115
+ context "Request that returns not properly formatted response with status != 20x" do
116
+ setup do
117
+ stub_get(/.+/, error_response("40x.html"))
118
+ @request = ActiveForms::Request.new(:get, "entries")
119
+ end
120
+
121
+ should "raise Error exception" do
122
+ assert_raise ActiveForms::Error, "Request returned status 401 and does include properly formatted error message." do
123
+ @request.perform
124
+ end
125
+ end
126
+ end
127
+
128
+ context "Request that returns invalid XML" do
129
+ setup do
130
+ stub_get(/.+/, error_response("parse_error.html"))
131
+ @request = ActiveForms::Request.new(:get, "entries")
132
+ end
133
+
134
+ should "raise ParseError exception" do
135
+ assert_raise ActiveForms::ParseError do
136
+ @request.perform
137
+ end
138
+ end
139
+ end
114
140
  end
115
141
  end
@@ -10,7 +10,7 @@ class ActiveForms::EntryTest < Test::Unit::TestCase
10
10
 
11
11
  context "when sent ActiveForms::Token.all with multi-response" do
12
12
  setup do
13
- stub_get(/formaccesstokens/, success_response("get_formaccesstokens"))
13
+ stub_get(/formaccesstokens/, success_response("get_formaccesstokens.xml"))
14
14
  @tokens = ActiveForms::Token.all :apiFormCode => 'hipoteka'
15
15
  end
16
16
 
@@ -0,0 +1,13 @@
1
+ <html><head>
2
+ <title>401 Authorization Required</title>
3
+ </head><body>
4
+ <h1>Authorization Required</h1>
5
+ <p>This server could not verify that you
6
+ are authorized to access the document
7
+ requested. Either you supplied the wrong
8
+ credentials (e.g., bad password), or your
9
+ browser doesn't understand how to supply
10
+ the credentials required.</p>
11
+ <hr/>
12
+ <address>Apache Server at activeforms.com Port 443</address>
13
+ </body></html>
@@ -0,0 +1,2 @@
1
+ <html><head>
2
+ <title
data/test/test_helper.rb CHANGED
@@ -53,7 +53,7 @@ class Test::Unit::TestCase
53
53
  response = {
54
54
  :status => ["200", "OK"],
55
55
  :content_type => "text/xml",
56
- :body => fixture(file || "success")
56
+ :body => fixture(file || "success.xml")
57
57
  }
58
58
  end
59
59
 
@@ -61,12 +61,12 @@ class Test::Unit::TestCase
61
61
  response = {
62
62
  :status => ["401", "Unauthorized"],
63
63
  :content_type => "text/xml",
64
- :body => fixture(file || "error")
64
+ :body => fixture(file || "error.xml")
65
65
  }
66
66
  end
67
67
 
68
68
  def fixture(file)
69
- File.read(File.join(File.dirname(__FILE__), "fixtures", "#{file}.xml"))
69
+ File.read(File.join(File.dirname(__FILE__), "fixtures", "#{file}"))
70
70
  end
71
71
  end
72
72
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_forms
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
4
+ hash: 17
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 3
9
- - 0
10
- version: 0.3.0
9
+ - 1
10
+ version: 0.3.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - "Micha\xC5\x82 Szajbe"
@@ -118,6 +118,7 @@ files:
118
118
  - test/active_forms/request_test.rb
119
119
  - test/active_forms/token_test.rb
120
120
  - test/active_forms_test.rb
121
+ - test/fixtures/40x.html
121
122
  - test/fixtures/error.xml
122
123
  - test/fixtures/get_applicationdata.xml
123
124
  - test/fixtures/get_applicationprint.xml
@@ -131,6 +132,7 @@ files:
131
132
  - test/fixtures/get_forminfo.xml
132
133
  - test/fixtures/get_forms.xml
133
134
  - test/fixtures/get_forms_single.xml
135
+ - test/fixtures/parse_error.html
134
136
  - test/fixtures/post_entrydata.xml
135
137
  - test/fixtures/success.xml
136
138
  - test/test_helper.rb
@@ -178,6 +180,7 @@ test_files:
178
180
  - test/active_forms/request_test.rb
179
181
  - test/active_forms/token_test.rb
180
182
  - test/active_forms_test.rb
183
+ - test/fixtures/40x.html
181
184
  - test/fixtures/error.xml
182
185
  - test/fixtures/get_applicationdata.xml
183
186
  - test/fixtures/get_applicationprint.xml
@@ -191,6 +194,7 @@ test_files:
191
194
  - test/fixtures/get_forminfo.xml
192
195
  - test/fixtures/get_forms.xml
193
196
  - test/fixtures/get_forms_single.xml
197
+ - test/fixtures/parse_error.html
194
198
  - test/fixtures/post_entrydata.xml
195
199
  - test/fixtures/success.xml
196
200
  - test/test_helper.rb