http-mock-server 0.1.8 → 0.2.0
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/Gemfile.lock +3 -1
- data/README.md +1 -1
- data/Rakefile +13 -0
- data/lib/http-mock-server.rb +51 -52
- data/lib/version.rb +1 -1
- data/spec/responses_spec.rb +1 -1
- data/spec/spec_helper.rb +3 -1
- metadata +16 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d6dd3fe9be69948b4bbcae31fbb102c131112d3c
|
4
|
+
data.tar.gz: 392938c67f005c827cb384106e5a3ed0ae96ed8d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e3b721d0ae8936878bd5730a9890bdc0c3296bcaa616a20781238c32eac94e2227766d11a2ac4b647c11dc9d92eddf24d704ab3ad124c3e4f821356dabe6e95a
|
7
|
+
data.tar.gz: bc73399efb8e6ffb136c1d7f84b6384823dd66558dce8f493cf4cf26c960e09e2bb1b10be597c1e9c3456e10f56221569eaba27c28c9a9810115e2404dce9e1f
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
http-mock-server (0.
|
4
|
+
http-mock-server (0.2.0)
|
5
5
|
pry (~> 0.11)
|
6
6
|
sinatra (~> 2.0)
|
7
7
|
|
@@ -20,6 +20,7 @@ GEM
|
|
20
20
|
rack
|
21
21
|
rack-test (1.0.0)
|
22
22
|
rack (>= 1.0, < 3)
|
23
|
+
rake (12.3.1)
|
23
24
|
rspec (3.7.0)
|
24
25
|
rspec-core (~> 3.7.0)
|
25
26
|
rspec-expectations (~> 3.7.0)
|
@@ -46,6 +47,7 @@ PLATFORMS
|
|
46
47
|
DEPENDENCIES
|
47
48
|
http-mock-server!
|
48
49
|
rack-test (~> 1.0)
|
50
|
+
rake (~> 12.3)
|
49
51
|
rspec (~> 3.7)
|
50
52
|
|
51
53
|
BUNDLED WITH
|
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# HTTP Mock Server [](https://badge.fury.io/rb/http-mock-server)
|
1
|
+
# HTTP Mock Server [](https://badge.fury.io/rb/http-mock-server) [](https://travis-ci.org/blocknotes/http-mock-server)
|
2
2
|
|
3
3
|
A Ruby HTTP Mock Server based on Sinatra. JSON responses.
|
4
4
|
|
data/Rakefile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
begin
|
2
|
+
require 'rspec/core/rake_task'
|
3
|
+
|
4
|
+
RSpec::Core::RakeTask.new(:spec) do |t|
|
5
|
+
t.rspec_opts = '--format documentation'
|
6
|
+
# t.rspec_opts << ' more options'
|
7
|
+
# t.rcov = true
|
8
|
+
end
|
9
|
+
|
10
|
+
task default: :spec
|
11
|
+
rescue LoadError
|
12
|
+
puts '! rspec load error'
|
13
|
+
end
|
data/lib/http-mock-server.rb
CHANGED
@@ -1,18 +1,18 @@
|
|
1
|
-
# coding: utf-8
|
2
1
|
require 'pry'
|
3
2
|
require 'sinatra/base'
|
4
3
|
require 'yaml'
|
5
4
|
|
6
|
-
require_relative File.expand_path(
|
5
|
+
require_relative File.expand_path('../version.rb', __FILE__)
|
7
6
|
|
8
7
|
$config_file = ENV['MOCK_CONFIG']
|
9
|
-
|
8
|
+
unless ARGV.empty?
|
10
9
|
case ARGV[0]
|
11
10
|
when '-v'
|
12
11
|
puts "http-mock-server v#{MOCK_SERVER_VERSION}"
|
13
12
|
exit 0
|
14
13
|
when '-h'
|
15
|
-
puts "http-mock-server v#{MOCK_SERVER_VERSION}\
|
14
|
+
puts "http-mock-server v#{MOCK_SERVER_VERSION}\n"
|
15
|
+
puts 'Syntax: http-mock-server config.yml'
|
16
16
|
exit 0
|
17
17
|
else
|
18
18
|
if File.exist?(ARGV[0])
|
@@ -23,22 +23,25 @@ if ARGV.length > 0
|
|
23
23
|
end
|
24
24
|
$config_file ||= 'mock.yml'
|
25
25
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
26
|
+
begin
|
27
|
+
$config = YAML.safe_load File.read($config_file)
|
28
|
+
rescue StandardError => _ignored
|
29
|
+
puts "Config file not found: #{$config_file}"
|
30
|
+
exit 1
|
31
|
+
end
|
30
32
|
$config['config'] ||= {}
|
31
33
|
$config['config']['namespace'] ||= ''
|
32
34
|
$config['routes'] ||= []
|
33
35
|
|
36
|
+
# Main app class
|
34
37
|
class HttpMockServer < Sinatra::Base
|
35
|
-
MIME_JSON = ['application/json']
|
38
|
+
MIME_JSON = ['application/json'].freeze
|
36
39
|
CORS_HEADERS = {
|
37
40
|
'Access-Control-Allow-Origin' => '*',
|
38
41
|
'Access-Control-Allow-Methods' => 'POST, GET, PUT, DELETE, OPTIONS',
|
39
|
-
'Access-Control-Allow-Headers' => 'Origin, Content-Type, Accept, Authorization, Token'
|
40
|
-
}
|
41
|
-
METHODS = %w
|
42
|
+
'Access-Control-Allow-Headers' => 'Origin, Content-Type, Accept, Authorization, Token'
|
43
|
+
}.freeze
|
44
|
+
METHODS = %w[connect delete get head options patch post put trace].freeze
|
42
45
|
|
43
46
|
# set :environment, :development
|
44
47
|
set :port, $config['config']['port'] if $config['config']['port']
|
@@ -49,9 +52,7 @@ class HttpMockServer < Sinatra::Base
|
|
49
52
|
|
50
53
|
before do
|
51
54
|
content_type :json
|
52
|
-
unless $config['config']['no_cors']
|
53
|
-
headers CORS_HEADERS
|
54
|
-
end
|
55
|
+
headers(CORS_HEADERS) unless $config['config']['no_cors']
|
55
56
|
end
|
56
57
|
|
57
58
|
$config['routes'].each_with_index do |rt, i|
|
@@ -60,20 +61,19 @@ class HttpMockServer < Sinatra::Base
|
|
60
61
|
method = m if rt[m]
|
61
62
|
break if method
|
62
63
|
end
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
end
|
64
|
+
next unless method
|
65
|
+
send method, $config['config']['namespace'] + rt[method] do
|
66
|
+
route = reload_route i
|
67
|
+
code = route['status'] || 200
|
68
|
+
log code, method.upcase, request.path
|
69
|
+
(route['headers'] || {}).each do |k, v|
|
70
|
+
response.headers[k] = v
|
71
|
+
end
|
72
|
+
status code
|
73
|
+
if route['body']
|
74
|
+
content = route['body'].dup
|
75
|
+
traverse! content
|
76
|
+
content.to_json
|
77
77
|
end
|
78
78
|
end
|
79
79
|
end
|
@@ -83,7 +83,7 @@ class HttpMockServer < Sinatra::Base
|
|
83
83
|
route = reload_route(-1)
|
84
84
|
code = route['status'] || 404
|
85
85
|
log code, request.path, ''
|
86
|
-
(
|
86
|
+
(route['headers'] || {}).each do |k, v|
|
87
87
|
response.headers[k] = v
|
88
88
|
end
|
89
89
|
status code
|
@@ -95,43 +95,42 @@ class HttpMockServer < Sinatra::Base
|
|
95
95
|
end
|
96
96
|
end
|
97
97
|
|
98
|
-
def log(
|
99
|
-
print "#{
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
end
|
98
|
+
def log(code, method, path)
|
99
|
+
print "#{Time.now.strftime('%F %T')} - [#{code}] #{method} #{path}\n"
|
100
|
+
return unless $config['config']['verbose']
|
101
|
+
if MIME_JSON.include? env['CONTENT_TYPE']
|
102
|
+
request.body.rewind
|
103
|
+
data = JSON.parse request.body.read
|
104
|
+
print " PARAMS_JSON: #{data}\n" unless data.empty?
|
105
|
+
else
|
106
|
+
pars = params.dup
|
107
|
+
pars.delete 'captures'
|
108
|
+
pars.delete 'splat'
|
109
|
+
print " PARAMS_DATA: #{pars}\n" if pars.any?
|
111
110
|
end
|
112
111
|
end
|
113
112
|
|
114
|
-
def interpolate(
|
115
|
-
vars = input.to_s.scan(
|
116
|
-
input.to_s.gsub(
|
113
|
+
def interpolate(input)
|
114
|
+
vars = input.to_s.scan(/\#{(.*?)}/).flatten.map { |t| eval(t) }
|
115
|
+
input.to_s.gsub(/\#{(.*?)}/, '%s') % vars
|
117
116
|
end
|
118
117
|
|
119
|
-
def reload_route(
|
120
|
-
config = YAML.
|
118
|
+
def reload_route(route_id)
|
119
|
+
config = YAML.safe_load File.read($config_file) || {}
|
121
120
|
return config['not_found'] || {} if route_id < 0
|
122
121
|
return {} if !config['routes'] || !config['routes'][route_id]
|
123
122
|
config['routes'][route_id]
|
124
123
|
end
|
125
124
|
|
126
|
-
def traverse!(
|
125
|
+
def traverse!(hash)
|
127
126
|
hash.each do |k, v|
|
128
127
|
if v.is_a?(Hash) || v.is_a?(Array)
|
129
128
|
traverse! v
|
130
|
-
|
131
|
-
hash[k] = interpolate v
|
129
|
+
elsif v
|
130
|
+
hash[k] = interpolate v
|
132
131
|
end
|
133
132
|
end
|
134
133
|
end
|
135
134
|
|
136
|
-
run! if app_file == $
|
135
|
+
run! if app_file == $PROGRAM_NAME || ENV['http_mock_server_bin']
|
137
136
|
end
|
data/lib/version.rb
CHANGED
@@ -1 +1 @@
|
|
1
|
-
MOCK_SERVER_VERSION = '0.
|
1
|
+
MOCK_SERVER_VERSION = '0.2.0'.freeze
|
data/spec/responses_spec.rb
CHANGED
@@ -5,7 +5,7 @@ RSpec.describe HttpMockServer do
|
|
5
5
|
get '/'
|
6
6
|
conf = $config['not_found']
|
7
7
|
expect(last_response.body).to eq conf['body'].to_json
|
8
|
-
expect(last_response.status).to eq
|
8
|
+
expect(last_response.status).to eq conf['status'] || 404
|
9
9
|
unless $config['config']['no_cors']
|
10
10
|
expect(HttpMockServer::CORS_HEADERS.to_a - last_response.headers.to_a).to be_empty
|
11
11
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -8,7 +8,9 @@ require_relative File.expand_path '../../lib/http-mock-server.rb', __FILE__
|
|
8
8
|
|
9
9
|
module RSpecMixin
|
10
10
|
include Rack::Test::Methods
|
11
|
-
def app
|
11
|
+
def app
|
12
|
+
HttpMockServer
|
13
|
+
end
|
12
14
|
end
|
13
15
|
|
14
16
|
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: http-mock-server
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mattia Roccoberton
|
@@ -52,6 +52,20 @@ dependencies:
|
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '1.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '12.3'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '12.3'
|
55
69
|
- !ruby/object:Gem::Dependency
|
56
70
|
name: rspec
|
57
71
|
requirement: !ruby/object:Gem::Requirement
|
@@ -77,6 +91,7 @@ files:
|
|
77
91
|
- Gemfile.lock
|
78
92
|
- LICENSE.txt
|
79
93
|
- README.md
|
94
|
+
- Rakefile
|
80
95
|
- bin/http-mock-server
|
81
96
|
- lib/http-mock-server.rb
|
82
97
|
- lib/version.rb
|