zipper 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,16 +2,12 @@ class ZipperGenerator < Rails::Generators::Base
2
2
  def create_files
3
3
  create_file "config/initializers/zipper_initializer.rb", "Zipper.files = []"
4
4
  rakefile("zipper.rake") do
5
- %Q{
6
- namespace :zipper do
7
- desc "Zipper API Testing"
8
- task :test => :environment do
9
-
10
- Zipper.run
11
-
12
- end
13
- end
14
- }
5
+ %Q{namespace :zipper do
6
+ desc "Zipper API Testing"
7
+ task :test => :environment do
8
+ Zipper.run
9
+ end
10
+ end}
15
11
  end
16
12
  end
17
13
  end
data/lib/zipper.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'pocket'
2
+ require 'json'
2
3
 
3
4
  module Zipper
4
5
  DEFAULT_PORT = 3001
@@ -29,7 +30,7 @@ module Zipper
29
30
 
30
31
 
31
32
  def run
32
- system "rails s --environment=test --port=#{self.instance.port} -d"
33
+ system "rails s --environment=test --port=#{self.port} -d"
33
34
  system "rake db:test:purge"
34
35
  system "rake db:test:load"
35
36
  pid = File.read("tmp/pids/server.pid")
@@ -37,14 +38,16 @@ module Zipper
37
38
  self.files.each do |file|
38
39
  system "ruby test/zipper/#{file}.rb"
39
40
  end
40
-
41
+
41
42
  system "kill -s 9 " + pid
43
+
44
+ File.open( "docs/zipper.json" , 'a' ) {|f| f.write( @docs.to_json ) }
42
45
  end
43
46
 
44
-
45
47
 
46
48
  def test(url = "", options = {})
47
49
 
50
+ url = url || ""
48
51
 
49
52
  # inferences
50
53
  _URI = URI.parse(url)
@@ -63,7 +66,7 @@ module Zipper
63
66
  :key => _key,
64
67
  :method => :get,
65
68
  :data => nil,
66
- :status => 200,
69
+ :assertions => { :status => 200 },
67
70
  :username => nil,
68
71
  :password => nil,
69
72
  :use_ssl => false
@@ -73,6 +76,7 @@ module Zipper
73
76
  # merge passed in options with default options
74
77
  _options = _options.merge(options)
75
78
 
79
+ # infer user
76
80
  if _options[:user]
77
81
  uid = _options[:user]
78
82
  user = (self.users)[uid]
@@ -80,8 +84,11 @@ module Zipper
80
84
  _un = user[:username]
81
85
  _pw = user[:password]
82
86
  end
87
+
88
+ # print test title
89
+ p "TESTING: " + _options[:title]
83
90
 
84
-
91
+ # make request
85
92
  _result = Pocket.make_request(_URL, {
86
93
  :method => _options[:method],
87
94
  :username => _un || _options[:username],
@@ -90,66 +97,100 @@ module Zipper
90
97
  :data => _options[:data]
91
98
  })
92
99
 
93
-
94
-
95
-
96
-
97
-
100
+
101
+
102
+ # test assertions
103
+ _assertions = _options[:assertions]
104
+
105
+ if _assertions[:status] && _result.code.to_i != _assertions[:status]
106
+ self.fail "Status should have been #{_assertions[:status]} but was #{_result.code}"
107
+ end
108
+
109
+
110
+
111
+ if _assertions[:body]
112
+ b = _assertions[:body]
113
+ if b.is_a?(TrueClass)
114
+ if _result.body.length < 1
115
+ self.fail "Body should have contained content but didn't"
116
+ end
117
+ elsif b.is_a?(FalseClass)
118
+ if _result.body.length > 0
119
+ self.fail "Body should have been blank but wasn't"
120
+ end
121
+ elsif b != _result.body
122
+ self.fail "Body should have been #{b} but wasn't"
123
+ end
124
+ end
125
+
126
+
127
+
128
+ if _assertions[:attributes]
129
+
130
+ if _result.body.length < 1
131
+ self.fail "Attempted to check for an attribute but the response body was blank"
132
+ return
133
+ end
134
+
135
+ attrs = _assertions[:attributes]
136
+ object = JSON.parse _result.body
137
+ attrs.each do |a|
138
+ if !object[a.to_s]
139
+ self.fail "The response object should have contained the attribute #{a} but didn't"
140
+ end
141
+ end
142
+ end
143
+
144
+
145
+
146
+
98
147
  # create example
99
- # _curl_string = []
100
- # _curl_string << "curl"
101
- #
102
- # if _options[:username] && _options[:password]
103
- # _curl_string << _options[:username] + ":" + _options[:password]
104
- # end
105
- #
106
- # _curl_string << '-H "Accept: application/json" -H "Content-Type: application/json"'
107
- #
108
- # if _options[:data]
109
- # _curl_string << "-d '" + _options[:data] + "'"
110
- # end
111
- #
112
- # if _options[:method] && _options[:method] != "GET"
113
- # _curl_string << "-X $method"
114
- # end
115
- #
116
- # _curl_string << "$docs_base_url/$api_url";
117
- # _curl_string << "\n"
118
-
119
-
120
-
121
-
148
+ _curl_string = []
149
+ _curl_string << "curl"
150
+
151
+ if _options[:username] && _options[:password]
152
+ _curl_string << "#{_options[:username]}:#{_options[:password]}"
153
+ end
154
+
155
+ _curl_string << '-H "Accept: application/json" -H "Content-Type: application/json"'
156
+
157
+ if _options[:data]
158
+ _curl_string << "-d '#{_options[:data]}'"
159
+ end
160
+
161
+ if _options[:method] && _options[:method] != "GET"
162
+ _curl_string << "-X $method"
163
+ end
164
+
165
+ _curl_string << "$docs_base_url/$api_url";
166
+ _curl_string << "\n"
167
+
168
+
122
169
  # create doc json
123
- # doc = {
124
- # :resource => _options[:resource],
125
- # :title => _options[:title],
126
- # :key => _options[:key],
127
- # :method => _options[:method],
128
- # :url => _options[:url],
129
- # :request_body => _options[:data],
130
- # :status => _options[:status],
131
- # :response_body => _result.to_json,
132
- # :example => _curl_string.join(" ")
133
- # }
134
- #
135
- # # write to docs file
136
- # file_path = Rails.root.join("docs/zipper_api.json")
137
- # current_json_string = File.open( file_path , "rb" ).read
138
- #
139
- # docs_json = []
140
- # begin
141
- # docs_json = JSON.parse current_json_string
142
- # rescue
143
- # docs_json = []
144
- # end
145
- #
146
- # docs_json << doc_json
147
- # File.open( file_path , 'a' ) {|f| f.write( docs_json ) }
170
+ @docs ||= []
171
+ @docs << {
172
+ :resource => _options[:resource],
173
+ :title => _options[:title],
174
+ :key => _options[:key],
175
+ :method => _options[:method],
176
+ :url => _options[:url],
177
+ :request_body => _options[:data],
178
+ :status => _options[:status],
179
+ :response_body => _result.to_json,
180
+ :example => _curl_string.join(" ")
181
+ }
148
182
 
149
183
  _result
150
184
 
151
185
  end
152
186
 
187
+
188
+ def fail(message)
189
+ p "*********************FAILED*********************"
190
+ p message
191
+ p "************************************************"
192
+ end
193
+
153
194
  end
154
195
 
155
196
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zipper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -15,7 +15,7 @@ date: 2011-11-24 00:00:00.000000000Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: pocket
18
- requirement: &70160204471100 !ruby/object:Gem::Requirement
18
+ requirement: &70286691586260 !ruby/object:Gem::Requirement
19
19
  none: false
20
20
  requirements:
21
21
  - - ! '>='
@@ -23,7 +23,18 @@ dependencies:
23
23
  version: '0'
24
24
  type: :runtime
25
25
  prerelease: false
26
- version_requirements: *70160204471100
26
+ version_requirements: *70286691586260
27
+ - !ruby/object:Gem::Dependency
28
+ name: json
29
+ requirement: &70286691585700 !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ! '>='
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: *70286691585700
27
38
  description: API Testing Done Right
28
39
  email: atomkirk@gmail.com
29
40
  executables: []