webtester 0.1.2 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 32953f9df768bc4a9e53c4c1b636668bd23ff6e4
4
- data.tar.gz: a823800207767458c00ca5f8a057d80f262bee79
3
+ metadata.gz: ae26b6e2eb7322d6a5b966a77bb0567a7dadecdf
4
+ data.tar.gz: 1ded93b0d0cdc80b35d4fc3118d55a943d5df35c
5
5
  SHA512:
6
- metadata.gz: bd7aad2b034dbce9def4f6d9776f962cf124fb529892abe3c9eae54585dd20904cc683058ea8d6386beb0b65ead9feec24d1a3455911837b316e97399531c08e
7
- data.tar.gz: 45877c9df6c170ceea1585889d86e6dcf1697dfaf0eaefe7d2ca96035b531bdf7ee0acf876804e3a177da95e2819fe14e6e9f0df5188e0fb84f0dbeb186abfc9
6
+ metadata.gz: cb7b8584e4433c484c73f6a8031f2a8cb58c367d53901c365d9e526511722b099ea2924e9f7931d3c563c6d59a68bdc8746c2cd4f641429e6d28adef8c6b6387
7
+ data.tar.gz: 243ec30339b7b3f68866a10b7f08845747d6fe4c1cc7446d7dbb28ffc6785d2da798abbf2c0a7a3fb450e8424c952213ac89a323ac64460a1941a15fe1447a0a
data/lib/assertion.rb CHANGED
@@ -10,14 +10,14 @@ module WT
10
10
  attr_accessor :expected_body_parts
11
11
  attr_accessor :unexpected_body_parts
12
12
 
13
- def initialize(expected_codes: '200',
13
+ def initialize(expected_codes: [],
14
14
  unexpected_codes: [],
15
15
  expected_headers: [],
16
16
  unexpected_headers: [],
17
17
  expected_body: nil,
18
18
  unexpected_body: nil,
19
- expected_body_parts: nil,
20
- unexpected_body_parts: nil)
19
+ expected_body_parts: [],
20
+ unexpected_body_parts: [])
21
21
  @expected_codes = expected_codes
22
22
  @unexpected_codes = unexpected_codes
23
23
  @expected_headers = expected_headers
@@ -90,7 +90,7 @@ module WT
90
90
  end
91
91
 
92
92
  def contain_body_part(body_parts)
93
- if body_part.kind_of?(Array)
93
+ if body_parts.kind_of?(Array)
94
94
  @expected_body_parts = @expected_body_parts | body_parts
95
95
  else
96
96
  @expected_body_parts << body_parts
@@ -112,24 +112,36 @@ module WT
112
112
  private
113
113
 
114
114
  def test_code(response)
115
- raise ExpectedCodeNotFoundException.new(response.code) unless @expected_codes.include?(response.code) or @expected_headers.length == 0
116
- raise UnexpectedCodeFoundException.new(response.code) if @unexpected_codes.include?(response.code)
115
+ raise ExpectedCodeNotFoundException.new(@expected_codes, response.code) unless @expected_codes.include?("#{response.code}") or @expected_codes.include?("#{response.code}".to_i)
116
+ raise UnexpectedCodeFoundException.new(response.code) if @unexpected_codes.include?("#{response.code}") or @unexpected_codes.include?("#{response.code}".to_i)
117
117
  end
118
118
 
119
119
  def test_headers(response)
120
120
 
121
121
  @expected_headers.each do |header|
122
- h_name = (header[:name]) ? header[:name].downcase : ''
123
- h_value = (header[:value]) ? header[:value].downcase : ''
124
122
 
125
- raise ExpectedHeadersNotFoundException.new(header) unless (response[h_name] and response[h_name] == h_value)
123
+
124
+ header.each do |k, v|
125
+ h_name = k.downcase
126
+ h_value = v.downcase
127
+
128
+ r_value = '???'
129
+ r_value = response[h_name].downcase if response[h_name]
130
+
131
+ raise ExpectedHeadersNotFoundException.new(header) unless (r_value == h_value)
132
+ end
126
133
  end
127
134
 
128
135
  @unexpected_headers.each do |header|
129
- h_name = (header[:name]) ? header[:name].downcase : ''
130
- h_value = (header[:value]) ? header[:value].downcase : ''
136
+ header.each do |k, v|
137
+ h_name = k.downcase
138
+ h_value = v.downcase
131
139
 
132
- raise UnexpectedHeadersFoundException.new(header) if (response[h_name] and response[h_name] == h_value)
140
+ r_value = '???'
141
+ r_value = response[h_name].downcase if response[h_name]
142
+
143
+ raise UnexpectedHeadersNotFoundException.new(header) if (r_value == h_value)
144
+ end
133
145
  end
134
146
  end
135
147
 
@@ -140,15 +152,15 @@ module WT
140
152
 
141
153
  def test_body_part(response)
142
154
 
143
- if @expected_body_parts
155
+ if @expected_body_parts.length > 0
144
156
  @expected_body_parts.each do |part|
145
- raise ExpectedCodeNotFoundException.new(part) unless response.body.include?(part)
157
+ raise ExpectedCodeNotFoundException.new(part) unless part.empty? or response.body.include?(part)
146
158
  end
147
159
  end
148
160
 
149
- if @unexpected_body_parts
161
+ if @unexpected_body_parts.length > 0
150
162
  @unexpected_body_parts.each do |part|
151
- raise UnexpectedBodyPartFoundException.new(part) if response.body.include?(part)
163
+ raise UnexpectedBodyPartFoundException.new(part) if not part.empty? and response.body.include?(part)
152
164
  end
153
165
  end
154
166
  end
@@ -162,10 +174,11 @@ module WT
162
174
 
163
175
  class ExpectedCodeNotFoundException < Exception
164
176
 
177
+ attr_reader :expected_codes
165
178
  attr_reader :code
166
179
 
167
- def initialize(code)
168
- super("Expected code not found in response.")
180
+ def initialize(expected_codes, code)
181
+ super("Expected code (#{expected_codes.join(' or ')}) not found in response. Code found was: #{code}")
169
182
  @code = code
170
183
  end
171
184
  end
@@ -224,7 +237,7 @@ module WT
224
237
  end
225
238
  end
226
239
 
227
- class UnexpectedBodyPartException < Exception
240
+ class UnexpectedBodyPartFoundException < Exception
228
241
  attr_reader :body_part
229
242
 
230
243
  def initialize(body_part)
data/lib/request.rb CHANGED
@@ -63,7 +63,7 @@ module WT
63
63
 
64
64
  host_ip = host;
65
65
 
66
- unless host.match(/([0-9]{1,3}){4}/)
66
+ unless host.match(/^[0-9]{1,3}(\.[0-9]{1,3}){3}$/)
67
67
  WT::Log.debug("\t* resolving #{host}...")
68
68
 
69
69
  begin
data/lib/result.rb CHANGED
@@ -2,10 +2,12 @@ module WT
2
2
  class Result
3
3
  attr_accessor :request
4
4
  attr_accessor :response
5
+ attr_accessor :error
5
6
 
6
- def initialize(request, response)
7
+ def initialize(request, response, error = nil)
7
8
  @request = request
8
9
  @response = response
10
+ @error = error
9
11
  end
10
12
  end
11
13
  end
data/lib/test.rb CHANGED
@@ -2,25 +2,42 @@ require 'request'
2
2
  require 'cookie'
3
3
  require 'web_session'
4
4
  require 'assertion'
5
+ require 'yaml'
5
6
 
6
7
  module WT
7
8
  class Test
8
9
  attr_accessor :name
9
10
  attr_accessor :steps
10
11
 
11
- def initialize(name: 'New Test', steps: [])
12
+ def initialize(name: 'New Test')
12
13
  @name = name
13
- @steps = steps
14
+ @steps = []
14
15
  end
15
16
 
16
- def add_step(request, assertions = [])
17
+ def self.load_from_file(filename)
18
+ yaml_content = YAML.load(File.read(filename))
17
19
 
18
- asserts = assertions
19
- asserts << WT::Assertion.new().code_is_equal('200') unless asserts.length > 0
20
+
21
+
22
+ test = WT::Test.new(name: yaml_content['test']['name'])
23
+
24
+ yaml_content['test']['steps'].each do |step|
25
+ request = WT::Test.request_from_yaml(step['request'])
26
+ assert = WT::Test.assertion_from_yaml(step['request']['response'])
27
+
28
+ test.add_step(request, assert)
29
+ end
30
+
31
+ return test
32
+ end
33
+
34
+ def add_step(request, assertion)
35
+
36
+ assert = assertion || WT::Assertion.new().code_is_equal('200')
20
37
 
21
38
  step = {
22
39
  :request => request,
23
- :assertions => assertions
40
+ :assertion => assert
24
41
  }
25
42
 
26
43
  @steps << step
@@ -32,25 +49,49 @@ module WT
32
49
  session = WT::WebSession.new
33
50
 
34
51
  @steps.each do |step|
35
- WT::Log.debug("\t * session.request()")
36
-
37
- error = nil
52
+ result = nil
38
53
 
39
54
  begin
40
55
  result = session.request(step[:request], false)
41
56
 
42
- step[:assertions].each do |assertion|
43
- WT::Log.debug("\t * assertion.assert(#{result.response.code})")
44
- assertion.assert(result.response)
45
- end
57
+ WT::Log.debug("\t * assertion.assert(#{result.response.code})")
58
+ step[:assertion].assert(result.response)
46
59
  rescue Exception => ex
47
- error = ex
60
+ result = WT::Result.new(step[:request], nil, nil) unless result
61
+ result.error = ex
48
62
  end
49
63
 
50
- block.call(step[:request], result, error)
64
+ block.call(result)
51
65
 
52
- break if error
66
+ break if result.error
53
67
  end
54
68
  end
69
+
70
+ private
71
+
72
+ def self.request_from_yaml(yaml_request)
73
+ type = yaml_request['type']
74
+ url = yaml_request['url']
75
+ body = {}
76
+ body = yaml_request['body'] if yaml_request['body']
77
+
78
+ request = WT::Request.new(type, url, body)
79
+ end
80
+
81
+ def self.assertion_from_yaml(yaml_response)
82
+ assert = nil
83
+
84
+ if yaml_response
85
+ assert = WT::Assertion.new()
86
+ assert.code_is_equal(yaml_response['expected_codes']) if yaml_response['expected_codes']
87
+ assert.code_is_not_equal(yaml_response['unexpected_codes']) if yaml_response['unexpected_codes']
88
+ assert.contain_headers(yaml_response['expected_headers']) if yaml_response['expected_headers']
89
+ assert.not_contain_headers(yaml_response['unexpected_headers']) if yaml_response['unexpected_headers']
90
+ assert.contain_body_part(yaml_response['expected_body_parts']) if yaml_response['expected_body_parts']
91
+ assert.not_contain_body_part(yaml_response['unexpected_body_parts']) if yaml_response['unexpected_body_parts']
92
+ end
93
+
94
+ assert
95
+ end
55
96
  end
56
97
  end
data/lib/web_session.rb CHANGED
@@ -34,8 +34,6 @@ module WT
34
34
 
35
35
  result = req.exec
36
36
 
37
- WT::Log.debug("\t * result => #{result}")
38
-
39
37
  if result.response != nil
40
38
  all_cookies = result.response['Set-Cookie']
41
39
 
@@ -68,8 +66,6 @@ module WT
68
66
  private
69
67
 
70
68
  def find_form_fields(content)
71
- WT::Log.debug(">> WT::WebSession::find_form_fields()")
72
-
73
69
  form_fields = {}
74
70
 
75
71
  str = content
@@ -94,14 +90,10 @@ module WT
94
90
  str = str[eidx, str.length]
95
91
  end
96
92
 
97
- WT::Log.debug("<< WT::WebSession::find_form_fields()")
98
-
99
93
  return form_fields
100
94
  end
101
95
 
102
96
  def extract_form_field_attrs(str)
103
- WT::Log.debug(">> WT::WebSession::extract_form_field_attrs()")
104
-
105
97
  attrs = {}
106
98
  regexp=/<input\s+.*(name|value)="([^"]*)".*\s+(name|value)="([^"]*)"\s*.*\/>/
107
99
 
@@ -110,8 +102,6 @@ module WT
110
102
  attrs["#{$3}"] = "#{$4}"
111
103
  end
112
104
 
113
- WT::Log.debug("<< WT::WebSession::extract_form_field_attrs()")
114
-
115
105
  return attrs
116
106
  end
117
107
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: webtester
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Diego F. Nascimento
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-20 00:00:00.000000000 Z
11
+ date: 2015-08-28 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: " A simple API to create HTTP Request tests. "
14
14
  email: diego.fnascimento@gmail.com