apireaper 1.0.1 → 1.0.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.
- checksums.yaml +4 -4
- data/lib/apireaper/checker.rb +2 -60
- data/lib/apireaper/requester.rb +128 -0
- data/lib/apireaper/version.rb +1 -1
- data/spec/apireaper/checker_spec.rb +21 -53
- data/spec/apireaper/stubs/servers_stubs.rb +12 -2
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2b42796a8d05da4a07d24ad0618cc8e35d4e7761
|
4
|
+
data.tar.gz: 939d3457d1a1ab1b21baf562adda3f78115ba047
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dcd488923977db3fb1009349ac09661ac34307defe6c93aef2337c776c904da5f2cdf8eb24b599fcca0ba4a5831f3d177923b3dabc54ccb7fb6670a5bcbe31ff
|
7
|
+
data.tar.gz: b9b32361f123d54b3b7cd63c4c30d4a34b68bef4f88103123bd39a0fa2ce82fc000448cbd9e481da8d20a73a04625bade0de03bea8c2d5f599c8a95f3d2d3c09
|
data/lib/apireaper/checker.rb
CHANGED
@@ -25,16 +25,13 @@ module APIReaper
|
|
25
25
|
class Checker
|
26
26
|
# Class constructor method
|
27
27
|
def initialize(method, url, opts)
|
28
|
-
@
|
29
|
-
@uri = URI.parse(url)
|
28
|
+
@requester = APIReaper::Requester.new(method, url, opts)
|
30
29
|
@opts = opts
|
31
|
-
@http = Net::HTTP.new(@uri.host, @uri.port)
|
32
|
-
@http.use_ssl = @uri.scheme.eql?('https')
|
33
30
|
end
|
34
31
|
|
35
32
|
# Main method to run the check
|
36
33
|
def check
|
37
|
-
res =
|
34
|
+
res = @requester.request
|
38
35
|
check_response_code(res.code)
|
39
36
|
check_data_structure(res.body)
|
40
37
|
puts 'All checks passed' unless @opts['quiet']
|
@@ -73,60 +70,5 @@ module APIReaper
|
|
73
70
|
@opts['schema_file']
|
74
71
|
end
|
75
72
|
end
|
76
|
-
|
77
|
-
# Split the data string into a hash
|
78
|
-
def format_data
|
79
|
-
data = {}
|
80
|
-
unless @opts['data'].nil?
|
81
|
-
pairs = @opts['data'].split('&')
|
82
|
-
pairs.each do |pair|
|
83
|
-
(key, value) = pair.split('=')
|
84
|
-
data[key] = value
|
85
|
-
end
|
86
|
-
end
|
87
|
-
data
|
88
|
-
end
|
89
|
-
|
90
|
-
# DELETE method
|
91
|
-
def rest_request_delete
|
92
|
-
exit_with_error(5, 'DELETE is not yet implemented')
|
93
|
-
# TODO
|
94
|
-
end
|
95
|
-
|
96
|
-
# GET method
|
97
|
-
def rest_request_get
|
98
|
-
exit_with_error(5, 'GET is not yet implemented')
|
99
|
-
# TODO
|
100
|
-
end
|
101
|
-
|
102
|
-
# POST method
|
103
|
-
def rest_request_post
|
104
|
-
req = Net::HTTP::Post.new(@uri.request_uri)
|
105
|
-
unless @opts['header'].nil?
|
106
|
-
@opts['header'].split(',').each do |h|
|
107
|
-
(key, value) = h.split(':')
|
108
|
-
req[key] = value
|
109
|
-
end
|
110
|
-
end
|
111
|
-
req.set_form_data(format_data)
|
112
|
-
@http.request(req)
|
113
|
-
end
|
114
|
-
|
115
|
-
# PUT method
|
116
|
-
def rest_request_put
|
117
|
-
exit_with_error(5, 'PUT is not yet implemented')
|
118
|
-
# TODO
|
119
|
-
end
|
120
|
-
|
121
|
-
# Select request method
|
122
|
-
def select_request
|
123
|
-
case @method.upcase
|
124
|
-
when 'DELETE' then rest_request_delete
|
125
|
-
when 'GET' then rest_request_get
|
126
|
-
when 'POST' then rest_request_post
|
127
|
-
when 'PUT' then rest_request_put
|
128
|
-
else raise "#{@method.upcase} is an unknown method"
|
129
|
-
end
|
130
|
-
end
|
131
73
|
end
|
132
74
|
end
|
@@ -0,0 +1,128 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
#
|
3
|
+
# Copyright (c) 2017 Richard Delaplace, Vente-Privee.Com
|
4
|
+
#
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
+
# you may not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
# See the License for the specific language governing permissions and
|
15
|
+
# limitations under the License.
|
16
|
+
#
|
17
|
+
|
18
|
+
require 'net/http'
|
19
|
+
require 'uri'
|
20
|
+
require 'json'
|
21
|
+
|
22
|
+
module APIReaper
|
23
|
+
# This is a Net HTTP requester class.
|
24
|
+
class Requester
|
25
|
+
# Class constructor method
|
26
|
+
def initialize(method, url, opts)
|
27
|
+
@method = method
|
28
|
+
@uri = URI.parse(url)
|
29
|
+
@opts = opts
|
30
|
+
@http = Net::HTTP.new(@uri.host, @uri.port)
|
31
|
+
@http.use_ssl = @uri.scheme.eql?('https')
|
32
|
+
end
|
33
|
+
|
34
|
+
# Exit with the specified errno and message
|
35
|
+
def exit_with_error(errno, message)
|
36
|
+
puts message unless @opts['quiet']
|
37
|
+
exit errno
|
38
|
+
end
|
39
|
+
|
40
|
+
# Select the right data type
|
41
|
+
def format_data(request)
|
42
|
+
return request if @opts['data'].nil?
|
43
|
+
if json?(@opts['data']) then format_json_data(request)
|
44
|
+
elsif www_form?(@opts['data']) then format_www_form_data(request)
|
45
|
+
else raise 'Data type is unknown.'
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
# Set the headers given as argument
|
50
|
+
def format_header(request)
|
51
|
+
unless @opts['header'].nil?
|
52
|
+
@opts['header'].split(',').each do |h|
|
53
|
+
(key, value) = h.split(':')
|
54
|
+
request[key] = value
|
55
|
+
end
|
56
|
+
end
|
57
|
+
request
|
58
|
+
end
|
59
|
+
|
60
|
+
# Set json data as request body
|
61
|
+
def format_json_data(request)
|
62
|
+
request.body = @opts['data']
|
63
|
+
request
|
64
|
+
end
|
65
|
+
|
66
|
+
# Split the data string into a hash
|
67
|
+
def format_www_form_data(request)
|
68
|
+
data = {}
|
69
|
+
pairs = @opts['data'].split('&')
|
70
|
+
pairs.each do |pair|
|
71
|
+
(key, value) = pair.split('=')
|
72
|
+
data[key] = value
|
73
|
+
end
|
74
|
+
request.set_form_data(data)
|
75
|
+
request
|
76
|
+
end
|
77
|
+
|
78
|
+
# Check if the data string is a well formated json
|
79
|
+
def json?(str)
|
80
|
+
JSON.parse(str)
|
81
|
+
true
|
82
|
+
rescue JSON::ParserError
|
83
|
+
false
|
84
|
+
end
|
85
|
+
|
86
|
+
# Check if the data string is a www form like string
|
87
|
+
def www_form?(str)
|
88
|
+
str.match(/^\w+=\w+(&\w+=\w+)*$/).nil? ? false : true
|
89
|
+
end
|
90
|
+
|
91
|
+
# DELETE method
|
92
|
+
def rest_request_delete
|
93
|
+
exit_with_error(5, 'DELETE is not yet implemented')
|
94
|
+
# TODO
|
95
|
+
end
|
96
|
+
|
97
|
+
# GET method
|
98
|
+
def rest_request_get
|
99
|
+
exit_with_error(5, 'GET is not yet implemented')
|
100
|
+
# TODO
|
101
|
+
end
|
102
|
+
|
103
|
+
# POST method
|
104
|
+
def rest_request_post
|
105
|
+
req = Net::HTTP::Post.new(@uri.request_uri)
|
106
|
+
req = format_data(req)
|
107
|
+
req = format_header(req)
|
108
|
+
@http.request(req)
|
109
|
+
end
|
110
|
+
|
111
|
+
# PUT method
|
112
|
+
def rest_request_put
|
113
|
+
exit_with_error(5, 'PUT is not yet implemented')
|
114
|
+
# TODO
|
115
|
+
end
|
116
|
+
|
117
|
+
# Select request method
|
118
|
+
def request
|
119
|
+
case @method.upcase
|
120
|
+
when 'DELETE' then rest_request_delete
|
121
|
+
when 'GET' then rest_request_get
|
122
|
+
when 'POST' then rest_request_post
|
123
|
+
when 'PUT' then rest_request_put
|
124
|
+
else raise "#{@method.upcase} is an unknown method"
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
data/lib/apireaper/version.rb
CHANGED
@@ -26,77 +26,45 @@ def print_out(code)
|
|
26
26
|
end
|
27
27
|
|
28
28
|
describe APIReaper::Checker do # rubocop:disable Metrics/BlockLength
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
expect { start(self) }.to raise_error(SystemExit)
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
context 'check notexistingcmd http://api.test.yueyehua.net/ -q' do
|
38
|
-
it 'tries unimplemented commands.' do
|
39
|
-
expect { start(self) }.to raise_error(SystemExit)
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
context 'check post http://api.test.yueyehua.net/' do
|
44
|
-
it 'requests an API without extra arguments.' do
|
45
|
-
expect { start(self) }.to output(print_out(200)).to_stdout
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
context 'check post http://api.test.yueyehua.net/ -d k=v' do
|
50
|
-
it 'requests an API with datas.' do
|
51
|
-
expect { start(self) }.to output(print_out(201)).to_stdout
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
context 'check post http://api.test.yueyehua.net/ -h k:v' do
|
56
|
-
it 'requests an API with headers.' do
|
57
|
-
expect { start(self) }.to output(print_out(202)).to_stdout
|
58
|
-
end
|
59
|
-
end
|
60
|
-
|
61
|
-
context 'check post http://api.test.yueyehua.net/ -q' do
|
62
|
-
it 'requests an API with quiet option.' do
|
63
|
-
expect { start(self) }.to output('').to_stdout
|
29
|
+
context 'check post http://api.test.yueyehua.net/ -h k:v -d k=v' do
|
30
|
+
it 'requests an API and test the response body without defined schema.' do
|
31
|
+
expect { start(self) }.to output(print_out(203)).to_stdout
|
64
32
|
end
|
65
33
|
end
|
66
34
|
|
67
|
-
|
68
|
-
|
69
|
-
|
35
|
+
schema_file = 'spec/files/schema'
|
36
|
+
opts203r1 = "-h k:v -d k=v -F #{schema_file}"
|
37
|
+
context "check post http://api.test.yueyehua.net/ #{opts203r1}" do
|
38
|
+
it 'requests an API and test the response body with schema file.' do
|
39
|
+
expect { start(self) }.to output(print_out(203)).to_stdout
|
70
40
|
end
|
71
41
|
end
|
72
42
|
|
73
|
-
|
74
|
-
|
43
|
+
schema = File.read(schema_file)
|
44
|
+
opts203r2 = "-h k:v -d k=v -S #{schema}"
|
45
|
+
context "check post http://api.test.yueyehua.net/ #{opts203r2}" do
|
46
|
+
it 'requests an API and test the response body with a schema as option.' do
|
75
47
|
expect { start(self) }.to output(print_out(203)).to_stdout
|
76
48
|
end
|
77
49
|
end
|
78
50
|
|
79
51
|
schema_file = 'spec/files/schema'
|
80
|
-
|
81
|
-
context "check post http://api.
|
82
|
-
it 'requests an API and test the response body with schema file.' do
|
83
|
-
expect { start(self) }.to
|
52
|
+
opts204 = "-h k:v -d k=v -q -F #{schema_file}"
|
53
|
+
context "check post http://api.fail.yueyehua.net/ #{opts204}" do
|
54
|
+
it 'requests an API and test the response body with wrong schema file.' do
|
55
|
+
expect { start(self) }.to raise_error(SystemExit)
|
84
56
|
end
|
85
57
|
end
|
86
58
|
|
87
|
-
|
88
|
-
|
89
|
-
context "check post http://api.fail.yueyehua.net/ #{opts2}" do
|
90
|
-
it 'requests an API and test the response body with schema file.' do
|
59
|
+
context 'check post http://api.fail.yueyehua.net/ -h l:w -d l=w -q' do
|
60
|
+
it 'requests an API and receive 404 status code.' do
|
91
61
|
expect { start(self) }.to raise_error(SystemExit)
|
92
62
|
end
|
93
63
|
end
|
94
64
|
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
it 'requests an API and test the response body with a schema as option.' do
|
99
|
-
expect { start(self) }.to output(print_out(203)).to_stdout
|
65
|
+
context 'check post http://api.test.yueyehua.net/ -q' do
|
66
|
+
it 'requests an API with quiet option.' do
|
67
|
+
expect { start(self) }.to output('').to_stdout
|
100
68
|
end
|
101
69
|
end
|
102
70
|
end
|
@@ -23,20 +23,30 @@ end
|
|
23
23
|
|
24
24
|
RSpec.configure do |config|
|
25
25
|
config.before(:each) do
|
26
|
+
# requests an API without extra arguments
|
26
27
|
stub_request(:post, 'http://api.test.yueyehua.net/')
|
27
28
|
.to_return('status' => 200, 'body' => '{}', 'headers' => {})
|
29
|
+
# requests an API with headers
|
28
30
|
stub_request(:post, 'http://api.test.yueyehua.net/')
|
29
|
-
.with('
|
31
|
+
.with('headers' => { 'k' => 'v' })
|
30
32
|
.to_return('status' => 201, 'body' => '{}', 'headers' => {})
|
33
|
+
# requests an API with www_form type datas
|
31
34
|
stub_request(:post, 'http://api.test.yueyehua.net/')
|
32
|
-
.with('
|
35
|
+
.with('body' => { '3wfk' => '3wfv' })
|
33
36
|
.to_return('status' => 202, 'body' => '{}', 'headers' => {})
|
37
|
+
# requests an API with json type datas
|
38
|
+
stub_request(:post, 'http://api.test.yueyehua.net/')
|
39
|
+
.with('body' => { 'jsonk' => 'jsonv' })
|
40
|
+
.to_return('status' => 203, 'body' => '{}', 'headers' => {})
|
41
|
+
# requests an API and test the response body
|
34
42
|
stub_request(:post, 'http://api.test.yueyehua.net/')
|
35
43
|
.with('body' => { 'k' => 'v' }, 'headers' => { 'k' => 'v' })
|
36
44
|
.to_return('status' => 203, 'body' => body_file('body'), 'headers' => {})
|
45
|
+
# requests an API and test the response body with wrong schema file
|
37
46
|
stub_request(:post, 'http://api.fail.yueyehua.net/')
|
38
47
|
.with('body' => { 'k' => 'v' }, 'headers' => { 'k' => 'v' })
|
39
48
|
.to_return('status' => 204, 'body' => body_file('fail'), 'headers' => {})
|
49
|
+
# requests an API and receive 404 status code
|
40
50
|
stub_request(:post, 'http://api.fail.yueyehua.net/')
|
41
51
|
.with('body' => { 'l' => 'w' }, 'headers' => { 'l' => 'w' })
|
42
52
|
.to_return('status' => 404, 'body' => '{}', 'headers' => {})
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: apireaper
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Richard Delaplace
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-05-
|
11
|
+
date: 2017-05-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -143,6 +143,7 @@ files:
|
|
143
143
|
- lib/apireaper.rb
|
144
144
|
- lib/apireaper/checker.rb
|
145
145
|
- lib/apireaper/cli.rb
|
146
|
+
- lib/apireaper/requester.rb
|
146
147
|
- lib/apireaper/version.rb
|
147
148
|
- spec/apireaper/checker_spec.rb
|
148
149
|
- spec/apireaper/cli_spec.rb
|