wsdsl 0.4.1 → 0.4.2
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.
- data/README.md +6 -1
- data/VERSION +1 -1
- data/lib/json_response_verification.rb +16 -3
- data/spec/json_response_verification_spec.rb +36 -4
- data/wsdsl.gemspec +1 -1
- metadata +1 -1
data/README.md
CHANGED
@@ -125,7 +125,7 @@ It would be described as follows:
|
|
125
125
|
node.datetime :created_at
|
126
126
|
node.object :team do |team|
|
127
127
|
team.integer :id
|
128
|
-
team.float :score
|
128
|
+
team.float :score, :null => true
|
129
129
|
end
|
130
130
|
end
|
131
131
|
end
|
@@ -138,6 +138,11 @@ following meta attributes are available:
|
|
138
138
|
* type (refers to the type of object described, valuable when using JSON
|
139
139
|
cross OO based apps.
|
140
140
|
|
141
|
+
JSON response validation can be done using an optional module.
|
142
|
+
Look at the spec/json_response_verification_spec.rb file for a complete
|
143
|
+
example. The goal of this module is to help automate API testing by
|
144
|
+
validating the data structure of the returned object.
|
145
|
+
|
141
146
|
## Test suite
|
142
147
|
|
143
148
|
This library comes with a test suite requiring Ruby 1.9.2
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.4.
|
1
|
+
0.4.2
|
@@ -75,18 +75,31 @@ module JSONResponseVerification
|
|
75
75
|
if el_or_attr.is_a?(WSDSL::Response::Element)
|
76
76
|
"#{el_or_attr.name || 'top level'} Node/Object/Element is missing"
|
77
77
|
elsif type_error
|
78
|
-
"#{el_or_attr.name || el_or_attr.inspect} was of wrong type"
|
78
|
+
error = "#{el_or_attr.name || el_or_attr.inspect} was of wrong type, expected #{el_or_attr.type}"
|
79
|
+
if el_or_attr.name
|
80
|
+
error << " and the value was: #{hash[el_or_attr.name.to_s]}"
|
81
|
+
end
|
82
|
+
error
|
79
83
|
else
|
80
84
|
"#{el_or_attr.name || el_or_attr.inspect} is missing in #{hash.inspect}"
|
81
85
|
end
|
82
86
|
end
|
83
87
|
|
84
88
|
def valid_hash_type?(hash, prop_template)
|
89
|
+
name = prop_template.name.to_s
|
90
|
+
attribute = hash[name]
|
91
|
+
# Check for nullity
|
92
|
+
if attribute.nil? && prop_template.opts[:null] != true
|
93
|
+
return false
|
94
|
+
end
|
85
95
|
type = prop_template.type
|
86
96
|
return true if type.nil?
|
87
97
|
rule = ParamsVerification.type_validations[type.to_sym]
|
88
|
-
|
89
|
-
|
98
|
+
if rule.nil?
|
99
|
+
puts "Don't know how to validate attributes of type #{type}" if type.to_sym != :string
|
100
|
+
return true
|
101
|
+
end
|
102
|
+
|
90
103
|
attribute.to_s =~ rule
|
91
104
|
end
|
92
105
|
|
@@ -25,7 +25,7 @@ describe "JSON response verification" do
|
|
25
25
|
service.response do |response|
|
26
26
|
response.object do |user|
|
27
27
|
user.integer :id
|
28
|
-
user.string :name
|
28
|
+
user.string :name, :null => true
|
29
29
|
user.datetime :created_at
|
30
30
|
user.object :creds do |creds|
|
31
31
|
creds.integer :id
|
@@ -42,7 +42,7 @@ describe "JSON response verification" do
|
|
42
42
|
node.integer :id
|
43
43
|
node.string :name
|
44
44
|
node.boolean :admin, :doc => "true if the user is an admin"
|
45
|
-
node.string :state, :doc => "test"
|
45
|
+
node.string :state, :doc => "test", :null => true
|
46
46
|
node.datetime :last_login_at
|
47
47
|
end
|
48
48
|
end
|
@@ -69,7 +69,7 @@ describe "JSON response verification" do
|
|
69
69
|
def valid_response(namespaced=true)
|
70
70
|
response = {
|
71
71
|
"id" => 1,
|
72
|
-
"name" => "matt",
|
72
|
+
"name" => "matt",
|
73
73
|
"created_at" => "2011-09-22T16:32:46-07:00",
|
74
74
|
"creds" => { "id" => 42, "price" => 2010.07, "enabled" => false }
|
75
75
|
}
|
@@ -80,11 +80,13 @@ describe "JSON response verification" do
|
|
80
80
|
{"users" => [
|
81
81
|
{"id" => 1,
|
82
82
|
"admin" => true,
|
83
|
+
"name" => "Heidi",
|
83
84
|
"state" => "retired",
|
84
85
|
"last_login_at" => "2011-09-22T22:46:35-07:00"
|
85
86
|
},
|
86
87
|
{"id" => 2,
|
87
88
|
"admin" => false,
|
89
|
+
"name" => "Giana",
|
88
90
|
"state" => "playing",
|
89
91
|
"last_login_at" => "2011-09-22T22:46:35-07:00"
|
90
92
|
}]
|
@@ -95,11 +97,13 @@ describe "JSON response verification" do
|
|
95
97
|
{"users" => [
|
96
98
|
{"id" => 1,
|
97
99
|
"admin" => true,
|
100
|
+
"name" => "Heidi",
|
98
101
|
"state" => "retired",
|
99
102
|
"pets" => []
|
100
103
|
},
|
101
104
|
{"id" => 2,
|
102
105
|
"admin" => false,
|
106
|
+
"name" => "Giana",
|
103
107
|
"state" => "playing",
|
104
108
|
"pets" => [{"id" => 23, "name" => "medor"}, {"id" => 34, "name" => "rex"}]
|
105
109
|
}]
|
@@ -109,6 +113,7 @@ describe "JSON response verification" do
|
|
109
113
|
|
110
114
|
it "should validate the response" do
|
111
115
|
valid, errors = @service.validate_hash_response(valid_response)
|
116
|
+
errors.should == []
|
112
117
|
valid.should be_true
|
113
118
|
errors.should be_empty
|
114
119
|
end
|
@@ -121,7 +126,7 @@ describe "JSON response verification" do
|
|
121
126
|
errors.should_not be_empty
|
122
127
|
end
|
123
128
|
|
124
|
-
it "should detect that a property type is wrong" do
|
129
|
+
it "should detect that a property integer type is wrong" do
|
125
130
|
response = valid_response
|
126
131
|
response["user"]["id"] = 'test'
|
127
132
|
valid, errors = @service.validate_hash_response(response)
|
@@ -131,6 +136,28 @@ describe "JSON response verification" do
|
|
131
136
|
errors.first.should match(/wrong type/)
|
132
137
|
end
|
133
138
|
|
139
|
+
it "should detect that an integer attribute value is nil" do
|
140
|
+
response = valid_response
|
141
|
+
response["user"]["id"] = nil
|
142
|
+
valid, errors = @service.validate_hash_response(response)
|
143
|
+
valid.should be_false
|
144
|
+
errors.should_not be_empty
|
145
|
+
errors.first.should match(/id/)
|
146
|
+
errors.first.should match(/wrong type/)
|
147
|
+
end
|
148
|
+
|
149
|
+
it "should detect that a string attribute value is nil [bug]" do
|
150
|
+
response = valid_response
|
151
|
+
response["user"]["name"] = nil
|
152
|
+
# puts response.inspect
|
153
|
+
valid, errors = @service.validate_hash_response(response)
|
154
|
+
valid.should be_false
|
155
|
+
errors.should_not be_empty
|
156
|
+
errors.first.should match(/name/)
|
157
|
+
errors.first.should match(/wrong type/)
|
158
|
+
end
|
159
|
+
|
160
|
+
|
134
161
|
it "should detect that a nested object is missing" do
|
135
162
|
response = valid_response
|
136
163
|
response["user"].delete("creds")
|
@@ -140,6 +167,11 @@ describe "JSON response verification" do
|
|
140
167
|
errors.first.should match(/missing/)
|
141
168
|
end
|
142
169
|
|
170
|
+
it "should validate non namespaced responses" do
|
171
|
+
valid, errors = @second_service.validate_hash_response(valid_response(false))
|
172
|
+
valid.should be_true
|
173
|
+
end
|
174
|
+
|
143
175
|
it "should validate nil attributes if marked as nullable" do
|
144
176
|
response = valid_response(false)
|
145
177
|
response["name"] = nil
|
data/wsdsl.gemspec
CHANGED