cucumber-rest-bdd 0.3.4.pre.alpha.pre.86

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,59 @@
1
+ require 'cucumber-api/response'
2
+ require 'cucumber-api/steps'
3
+
4
+ Then(/^the request (?:is|was) successful$/) do
5
+ raise %/Expected Successful response code 2xx but was #{@response.code}/ if @response.code < 200 || @response.code >= 300
6
+ end
7
+
8
+ Then(/^the request (?:is|was) redirected$/) do
9
+ raise %/Expected redirected response code 3xx but was #{@response.code}/ if @response.code < 300 || @response.code >= 400
10
+ end
11
+
12
+ Then(/^(?:it|the request) fail(?:s|ed)$/) do
13
+ raise %/Expected failed response code 4xx\/5xx but was #{@response.code}/ if @response.code < 400 || @response.code >= 600
14
+ end
15
+
16
+ Then(/^the request (?:is|was) successful and (?:a resource|.+) (?:is|was) created$/) do
17
+ steps %Q{Then the response status should be "201"}
18
+ end
19
+
20
+ Then(/^the request (?:is|was) successfully accepted$/) do
21
+ steps %Q{Then the response status should be "202"}
22
+ end
23
+
24
+ Then(/^the request (?:is|was) successful and (?:no|an empty) response body is returned$/) do
25
+ steps %Q{Then the response status should be "204"}
26
+ raise %/Expected the request body to be empty/ if !@response.body.empty?
27
+ end
28
+
29
+ Then(/^(?:it|the request) fail(?:s|ed) because it (?:is|was) invalid$/) do
30
+ steps %Q{Then the response status should be "400"}
31
+ end
32
+
33
+ Then(/^(?:it|the request) fail(?:s|ed) because (?:.+) (?:is|was|am|are) unauthori[sz]ed$/) do
34
+ steps %Q{Then the response status should be "401"}
35
+ end
36
+
37
+ Then(/^(?:it|the request) fail(?:s|ed) because (?:.+) (?:is|was) forbidden$/) do
38
+ steps %Q{Then the response status should be "403"}
39
+ end
40
+
41
+ Then(/^(?:it|the request) fail(?:s|ed) because the (?:.+) (?:is|was) not found$/) do
42
+ steps %Q{Then the response status should be "404"}
43
+ end
44
+
45
+ Then(/^(?:it|the request) fail(?:s|ed) because it (?:is|was) not allowed$/) do
46
+ steps %Q{Then the response status should be "405"}
47
+ end
48
+
49
+ Then(/^(?:it|the request) fail(?:s|ed) because there (?:is|was|has) a conflict(?: with .+)?$/) do
50
+ steps %Q{Then the response status should be "409"}
51
+ end
52
+
53
+ Then(/^(?:it|the request) fail(?:s|ed) because the (?:.+) (?:is|was|has) gone$/) do
54
+ steps %Q{Then the response status should be "410"}
55
+ end
56
+
57
+ Then(/^(?:it|the request) fail(?:s|ed) because the (?:.+) (?:is|was) not implemented$/) do
58
+ steps %Q{Then the response status should be "501"}
59
+ end
@@ -0,0 +1,4 @@
1
+ require 'cucumber-rest-bdd/steps/functional'
2
+ require 'cucumber-rest-bdd/steps/response'
3
+ require 'cucumber-rest-bdd/steps/resource'
4
+ require 'cucumber-rest-bdd/steps/status'
@@ -0,0 +1,138 @@
1
+ require 'active_support/inflector'
2
+
3
+ CAPTURE_INT = Transform(/^(?:zero|one|two|three|four|five|six|seven|eight|nine|ten)$/) do |v|
4
+ %w(zero one two three four five six seven eight nine ten).index(v)
5
+ end
6
+
7
+ FEWER_MORE_THAN = Transform(/^(?:(?:fewer|less|more) than|at (?:least|most))$/) do |v|
8
+ to_compare(v)
9
+ end
10
+
11
+ HAVE_SYNONYM = %{(?:has|have|having|contain|contains|containing|with)}
12
+
13
+ CMP_LESS_THAN = '<'
14
+ CMP_MORE_THAN = '>'
15
+ CMP_AT_LEAST = '>='
16
+ CMP_AT_MOST = '<='
17
+ CMP_EQUALS = '='
18
+
19
+ # take a number modifier string (fewer than, less than, etc) and return an operator '<', etc
20
+ def to_compare(compare)
21
+ return case compare
22
+ when 'fewer than' then CMP_LESS_THAN
23
+ when 'less than' then CMP_LESS_THAN
24
+ when 'more than' then CMP_MORE_THAN
25
+ when 'at least' then CMP_AT_LEAST
26
+ when 'at most' then CMP_AT_MOST
27
+ else CMP_EQUALS
28
+ end
29
+ end
30
+
31
+ # turn a comparison into a string
32
+ def compare_to_string(compare)
33
+ case compare
34
+ when CMP_LESS_THAN then 'fewer than '
35
+ when CMP_MORE_THAN then 'more than '
36
+ when CMP_AT_LEAST then 'at least '
37
+ when CMP_AT_MOST then 'at most '
38
+ when CMP_EQUALS then ''
39
+ else ''
40
+ end
41
+ end
42
+
43
+ # compare two numbers using the FEWER_MORE_THAN optional modifier
44
+ def num_compare(type, left, right)
45
+ case type
46
+ when CMP_LESS_THAN then left < right
47
+ when CMP_MORE_THAN then left > right
48
+ when CMP_AT_MOST then left <= right
49
+ when CMP_AT_LEAST then left >= right
50
+ when CMP_EQUALS then left == right
51
+ else left == right
52
+ end
53
+ end
54
+
55
+ def to_num(num)
56
+ if /^(?:zero|one|two|three|four|five|six|seven|eight|nine|ten)$/.match(num)
57
+ return %w(zero one two three four five six seven eight nine ten).index(num)
58
+ end
59
+ return num
60
+ end
61
+
62
+ module Boolean; end
63
+ class TrueClass; include Boolean; end
64
+ class FalseClass; include Boolean; end
65
+
66
+ module Enum; end
67
+ class String; include Enum; end
68
+
69
+ class String
70
+ def to_type(type)
71
+ # cannot use 'case type' which checks for instances of a type rather than type equality
72
+ if type == Boolean then !(self =~ /true|yes/i).nil?
73
+ elsif type == Enum then self.upcase.tr(" ", "_")
74
+ elsif type == Float then self.to_f
75
+ elsif type == Integer then self.to_i
76
+ elsif type == NilClass then nil
77
+ else self
78
+ end
79
+ end
80
+ end
81
+
82
+ def parse_type(type)
83
+ replacements = {
84
+ /^numeric$/i => 'integer',
85
+ /^int$/i => 'integer',
86
+ /^long$/i => 'integer',
87
+ /^number$/i => 'integer',
88
+ /^decimal$/i => 'float',
89
+ /^double$/i => 'float',
90
+ /^bool$/i => 'boolean',
91
+ /^null$/i => 'nil_class',
92
+ /^nil$/i => 'nil_class',
93
+ /^text$/i => 'string'
94
+ }
95
+ type.tr(' ', '_')
96
+ replacements.each { |k,v| type.gsub!(k, v) }
97
+ type
98
+ end
99
+
100
+ def get_resource(name)
101
+ resource = name.parameterize
102
+ resource = (ENV.has_key?('resource_single') && ENV['resource_single'] == 'true') ? resource.singularize : resource.pluralize
103
+ end
104
+
105
+ def get_root_json_path()
106
+ key = ENV.has_key?('data_key') && !ENV['data_key'].empty? ? %/$.#{ENV['data_key']}./ : "$."
107
+ end
108
+
109
+ def get_json_path(names)
110
+ return "#{get_root_json_path()}#{get_parameters(names).join('.')}"
111
+ end
112
+
113
+ def get_parameters(names)
114
+ names.split(':').map { |n| get_parameter(n) }
115
+ end
116
+
117
+ def get_parameter(name)
118
+ if name[0] == '`' && name[-1] == '`'
119
+ name = name[1..-2]
120
+ else
121
+ separator = ENV.has_key?('field_separator') ? ENV['field_separator'] : '_'
122
+ name = name.parameterize(separator: separator)
123
+ name = name.camelize(:lower) if (ENV.has_key?('field_camel') && ENV['field_camel'] == 'true')
124
+ end
125
+ name
126
+ end
127
+
128
+ def get_attributes(hashes)
129
+ attributes = hashes.each_with_object({}) do |row, hash|
130
+ name, value, type = row["attribute"], row["value"], row["type"]
131
+ value = resolve(value)
132
+ value.gsub!(/\\n/, "\n")
133
+ type = parse_type(type)
134
+ names = get_parameters(name)
135
+ new_hash = names.reverse.inject(value.to_type(type.camelize.constantize)) { |a, n| { n => a } }
136
+ hash.deep_merge!(new_hash)
137
+ end
138
+ end
@@ -0,0 +1,4 @@
1
+ def get_url(path)
2
+ raise %/Please set an 'endpoint' environment variable provided with the url of the api/ if !ENV.has_key?('endpoint')
3
+ url = %/#{ENV['endpoint']}#{path}/
4
+ end
@@ -0,0 +1 @@
1
+ require 'cucumber-rest-bdd/steps'