http-mock-server 0.1.8 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cb0f2449583ac229194f86d041ad5b2ac166b601
4
- data.tar.gz: 4445f809a0e6c2781881fedc4f0dea39f4c856f8
3
+ metadata.gz: d6dd3fe9be69948b4bbcae31fbb102c131112d3c
4
+ data.tar.gz: 392938c67f005c827cb384106e5a3ed0ae96ed8d
5
5
  SHA512:
6
- metadata.gz: 2381f980f8415d24bdb265e2c9c6aa95a922e4e401c318290c7188dbb2d0fa03186da212d2102d5e03ecc51c102457f8ddfc875ea78622ffdd85b3957541273f
7
- data.tar.gz: 103a9330960c8f675d994985221945ff4ca6054e740663ab33b895367bf0fd6a9005bd559179430e3d66a402f8fd443a532572cf7f52b1c0de861993e79028a9
6
+ metadata.gz: e3b721d0ae8936878bd5730a9890bdc0c3296bcaa616a20781238c32eac94e2227766d11a2ac4b647c11dc9d92eddf24d704ab3ad124c3e4f821356dabe6e95a
7
+ data.tar.gz: bc73399efb8e6ffb136c1d7f84b6384823dd66558dce8f493cf4cf26c960e09e2bb1b10be597c1e9c3456e10f56221569eaba27c28c9a9810115e2404dce9e1f
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- http-mock-server (0.1.8)
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 [![Gem Version](https://badge.fury.io/rb/http-mock-server.svg)](https://badge.fury.io/rb/http-mock-server)
1
+ # HTTP Mock Server [![Gem Version](https://badge.fury.io/rb/http-mock-server.svg)](https://badge.fury.io/rb/http-mock-server) [![Build Status](https://travis-ci.org/blocknotes/http-mock-server.svg)](https://travis-ci.org/blocknotes/http-mock-server)
2
2
 
3
3
  A Ruby HTTP Mock Server based on Sinatra. JSON responses.
4
4
 
@@ -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
@@ -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( '../version.rb', __FILE__ )
5
+ require_relative File.expand_path('../version.rb', __FILE__)
7
6
 
8
7
  $config_file = ENV['MOCK_CONFIG']
9
- if ARGV.length > 0
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}\nSyntax: http-mock-server config.yml"
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
- $config = YAML.load File.read($config_file) rescue begin
27
- puts "Config file not found: #{$config_file}"
28
- exit 1
29
- end
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(connect delete get head options patch post put trace)
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
- if method
64
- send method, $config['config']['namespace'] + rt[method] do
65
- route = reload_route i
66
- code = route['status'] || 200
67
- log code, method.upcase, request.path
68
- ( route['headers'] || {} ).each do |k, v|
69
- response.headers[k] = v
70
- end
71
- status code
72
- if route['body']
73
- content = route['body'].dup
74
- traverse! content
75
- content.to_json
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
- ( route['headers'] || {} ).each do |k, v|
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( code, method, path )
99
- print "#{DateTime.now.strftime('%F %T')} - [#{code}] #{method} #{path}\n"
100
- if $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.to_s}\n" unless data.empty?
105
- else
106
- pars = params.dup
107
- pars.delete 'captures'
108
- pars.delete 'splat'
109
- print " PARAMS_DATA: #{pars.to_s}\n" if pars.any?
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( input )
115
- vars = input.to_s.scan( /\#{(.*?)}/ ).flatten.map{|t| eval(t)}
116
- input.to_s.gsub( /\#{(.*?)}/, '%s' ) % vars
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( route_id )
120
- config = YAML.load File.read($config_file) || {}
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!( hash )
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
- else
131
- hash[k] = interpolate v if 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 == $0 || ENV['http_mock_server_bin']
135
+ run! if app_file == $PROGRAM_NAME || ENV['http_mock_server_bin']
137
136
  end
@@ -1 +1 @@
1
- MOCK_SERVER_VERSION = '0.1.8'
1
+ MOCK_SERVER_VERSION = '0.2.0'.freeze
@@ -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 (conf['status'] || 404)
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
@@ -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() HttpMockServer end
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.1.8
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