http-mock-server 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f4b44a4091d6546ca6be584fd530cc51f1c17274
4
+ data.tar.gz: 63e3f4d49cac17d2bee75001e2d9d9812621251d
5
+ SHA512:
6
+ metadata.gz: d8a7b37fea4e2a8ef36d77586bf243ceb93f579963c74780c4bb90d6cefbb7aad96b640b2e7b62cf593d93f81779bd59feff50261da509480c0ce14e58c1d696
7
+ data.tar.gz: d5a7d905096892035c2b0e1b8eba3814db53c9939325eca319f492794973f6b3726616cc592fdfcad9526e6276dcb30f89af889ebcf443c2739edcfc8530be85
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ _misc/
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'sinatra'
4
+
5
+ gem 'pry'
data/Gemfile.lock ADDED
@@ -0,0 +1,28 @@
1
+ GEM
2
+ remote: https://rubygems.org/
3
+ specs:
4
+ coderay (1.1.2)
5
+ method_source (0.9.0)
6
+ mustermann (1.0.2)
7
+ pry (0.11.3)
8
+ coderay (~> 1.1.0)
9
+ method_source (~> 0.9.0)
10
+ rack (2.0.4)
11
+ rack-protection (2.0.1)
12
+ rack
13
+ sinatra (2.0.1)
14
+ mustermann (~> 1.0)
15
+ rack (~> 2.0)
16
+ rack-protection (= 2.0.1)
17
+ tilt (~> 2.0)
18
+ tilt (2.0.8)
19
+
20
+ PLATFORMS
21
+ ruby
22
+
23
+ DEPENDENCIES
24
+ pry
25
+ sinatra
26
+
27
+ BUNDLED WITH
28
+ 1.16.1
data/LICENSE.txt ADDED
@@ -0,0 +1,7 @@
1
+ Copyright (c) 2018 Mattia Roccoberton
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,85 @@
1
+ # HTTP Mock Server [![Gem Version](https://badge.fury.io/rb/http-mock-server.svg)](https://badge.fury.io/rb/http-mock-server)
2
+
3
+ A Ruby HTTP Mock Server based on Sinatra. JSON responses.
4
+
5
+ Features:
6
+ - simple config YAML file
7
+ - routes reloaded every time (but only the updated ones)
8
+ - string interpolations on response body (even *binding.pry* to inspect a request)
9
+
10
+ ## Install
11
+
12
+ `gem install http-mock-server`
13
+
14
+ ## Usage
15
+
16
+ - Create a *mock.yml* config file in the current directory (see the [example](mock.yml))
17
+ - Execute: `http-mock-server`
18
+
19
+ ## Config example
20
+
21
+ ```yml
22
+ config:
23
+ # no_cors: true # don't send CORS headers
24
+ port: 8080 # server port
25
+ # timeout: 10 # response timeout
26
+ verbose: true # shows request params
27
+ not_found:
28
+ body:
29
+ message: This is not the path you are looking for...
30
+ routes:
31
+ -
32
+ get: '/posts'
33
+ headers:
34
+ 'A-Random-Header': Something
35
+ body:
36
+ message: List of posts
37
+ -
38
+ get: '/posts/:id'
39
+ body:
40
+ content: A random number {rand} !
41
+ extra:
42
+ today: Today is {DateTime.now}
43
+ request: Post id {params[:id]} - request path {request.path}
44
+ more:
45
+ is_first: "conditional check {params[:id] == '1' ? 'first' : 'other'}"
46
+ -
47
+ get: '/pry'
48
+ body:
49
+ message: '{binding.pry}'
50
+ -
51
+ post: '/posts'
52
+ status: 201
53
+ body:
54
+ code: 201
55
+ result: Ok
56
+ -
57
+ delete: '*'
58
+ status: 405
59
+ body:
60
+ message: Please don't do it
61
+ -
62
+ options: '*'
63
+ status: 200
64
+ headers:
65
+ 'Access-Control-Allow-Origin': '*'
66
+ 'Access-Control-Allow-Methods': 'HEAD,GET,PUT,DELETE,OPTIONS'
67
+ 'Access-Control-Allow-Headers': 'X-Requested-With, X-HTTP-Method-Override, Content-Type, Cache-Control, Accept'
68
+ 'Access-Control-Expose-Headers': 'X-Total-Count'
69
+ ```
70
+
71
+ ## Notes
72
+
73
+ - Routes are loaded from the Yaml config at each request, but it updates only the exiting routes; new / old routes are ignored, you have to restart *http-mock-server* to include them
74
+
75
+ ## Do you like it? Star it!
76
+
77
+ If you use this component just star it. A developer is more motivated to improve a project when there is some interest.
78
+
79
+ ## Contributors
80
+
81
+ - [Mattia Roccoberton](http://blocknot.es) - creator, maintainer
82
+
83
+ ## License
84
+
85
+ [MIT](LICENSE.txt)
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ ENV['http_mock_server_bin'] = '1'
4
+
5
+ require 'rack'
6
+ require 'tilt'
7
+ require 'http-mock-server'
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path( '../lib', __FILE__ )
3
+ $LOAD_PATH.unshift( lib ) unless $LOAD_PATH.include?( lib )
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'http-mock-server'
7
+ spec.version = '0.1.0'
8
+ spec.authors = [ 'Mattia Roccoberton' ]
9
+ spec.email = 'mat@blocknot.es'
10
+ spec.homepage = 'https://github.com/blocknotes/http-mock-server'
11
+ spec.summary = 'HTTP Mock Server'
12
+ spec.description = 'A Ruby HTTP Mock Server based on Sinatra'
13
+ spec.platform = Gem::Platform::RUBY
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.required_ruby_version = '>= 2.0.0'
22
+
23
+ spec.add_runtime_dependency 'pry', '~> 0.11'
24
+ spec.add_runtime_dependency 'sinatra', '~> 2.0'
25
+ end
@@ -0,0 +1,117 @@
1
+ require 'pry'
2
+ require 'sinatra/base'
3
+ require 'yaml'
4
+
5
+ CONFIG_FILE = 'mock.yml'
6
+ MIME_JSON = ['application/json']
7
+
8
+ $config = YAML.load File.read(CONFIG_FILE) rescue begin
9
+ puts "Config file not found: #{CONFIG_FILE}"
10
+ exit 1
11
+ end
12
+ $config['config'] ||= {}
13
+ $config['routes'] ||= []
14
+
15
+ class HttpMockServer < Sinatra::Base
16
+ METHODS = %w(connect delete get head options patch post put trace)
17
+
18
+ # set :environment, :development
19
+ set :port, $config['config']['port'] if $config['config']['port']
20
+ set :server_settings, timeout: $config['config']['timeout'] || 0
21
+
22
+ configure do
23
+ end
24
+
25
+ before do
26
+ content_type :json
27
+ unless $config['config']['no_cors']
28
+ headers(
29
+ 'Access-Control-Allow-Origin' => '*',
30
+ 'Access-Control-Allow-Methods' => 'POST, GET, PUT, DELETE, OPTIONS',
31
+ 'Access-Control-Allow-Headers' => 'Origin, Content-Type, Accept, Authorization, Token',
32
+ )
33
+ end
34
+ end
35
+
36
+ $config['routes'].each_with_index do |rt, i|
37
+ method = nil
38
+ METHODS.each do |m|
39
+ method = m if rt[m]
40
+ break if method
41
+ end
42
+ if method
43
+ send method, rt[method] do
44
+ route = reload_route i
45
+ code = route['status'] || 200
46
+ log code, method.upcase, request.path
47
+ ( route['headers'] || {} ).each do |k, v|
48
+ response.headers[k] = v
49
+ end
50
+ status code
51
+ if route['body']
52
+ content = route['body'].dup
53
+ traverse! content
54
+ content.to_json
55
+ end
56
+ end
57
+ end
58
+ end
59
+
60
+ if $config['not_found']
61
+ not_found do
62
+ route = reload_route(-1)
63
+ code = route['status'] || 404
64
+ log code, request.path, ''
65
+ ( route['headers'] || {} ).each do |k, v|
66
+ response.headers[k] = v
67
+ end
68
+ status code
69
+ if route['body']
70
+ content = route['body'].dup
71
+ traverse! content
72
+ content.to_json
73
+ end
74
+ end
75
+ end
76
+
77
+ def log( code, method, path )
78
+ print "#{DateTime.now.strftime('%F %T')} - [#{code}] #{method} #{path}\n"
79
+ if $config['config']['verbose']
80
+ if MIME_JSON.include? env['CONTENT_TYPE']
81
+ request.body.rewind
82
+ data = JSON.parse request.body.read
83
+ print " PARAMS_JSON: #{data.to_s}\n" unless data.empty?
84
+ else
85
+ pars = params.dup
86
+ pars.delete 'captures'
87
+ pars.delete 'splat'
88
+ print " PARAMS_DATA: #{pars.to_s}\n" if pars.any?
89
+ end
90
+ end
91
+ end
92
+
93
+ def interpolate( string )
94
+ return '' unless string
95
+ vars = string.scan( /{(.*?)}/ ).flatten.map{|t| eval(t)}
96
+ string.gsub( /{(.*?)}/, '%s' ) % vars
97
+ end
98
+
99
+ def reload_route( route_id )
100
+ config = YAML.load File.read(CONFIG_FILE) || {}
101
+ return config['not_found'] || {} if route_id < 0
102
+ return {} if !config['routes'] || !config['routes'][route_id]
103
+ config['routes'][route_id]
104
+ end
105
+
106
+ def traverse!( hash )
107
+ hash.each do |k, v|
108
+ if v.is_a?(Hash)
109
+ traverse! v
110
+ else
111
+ hash[k] = interpolate v
112
+ end
113
+ end
114
+ end
115
+
116
+ run! if app_file == $0 || ENV['http_mock_server_bin']
117
+ end
data/mock.yml ADDED
@@ -0,0 +1,48 @@
1
+ ---
2
+ config:
3
+ # no_cors: true # don't send CORS headers
4
+ port: 8080 # server port
5
+ # timeout: 10 # response timeout
6
+ verbose: true # shows request params
7
+ not_found:
8
+ body:
9
+ message: This is not the path you are looking for...
10
+ routes:
11
+ -
12
+ get: '/posts'
13
+ headers:
14
+ 'A-Random-Header': Something
15
+ body:
16
+ message: List of posts
17
+ -
18
+ get: '/posts/:id'
19
+ body:
20
+ content: A random number {rand} !
21
+ extra:
22
+ today: Today is {DateTime.now}
23
+ request: Post id {params[:id]} - request path {request.path}
24
+ more:
25
+ is_first: "conditional check {params[:id] == '1' ? 'first' : 'other'}"
26
+ -
27
+ get: '/pry'
28
+ body:
29
+ message: '{binding.pry}'
30
+ -
31
+ post: '/posts'
32
+ status: 201
33
+ body:
34
+ code: 201
35
+ result: Ok
36
+ -
37
+ delete: '*'
38
+ status: 405
39
+ body:
40
+ message: Please don't do it
41
+ -
42
+ options: '*'
43
+ status: 200
44
+ headers:
45
+ 'Access-Control-Allow-Origin': '*'
46
+ 'Access-Control-Allow-Methods': 'HEAD,GET,PUT,DELETE,OPTIONS'
47
+ 'Access-Control-Allow-Headers': 'X-Requested-With, X-HTTP-Method-Override, Content-Type, Cache-Control, Accept'
48
+ 'Access-Control-Expose-Headers': 'X-Total-Count'
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: http-mock-server
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Mattia Roccoberton
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-04-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: pry
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.11'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.11'
27
+ - !ruby/object:Gem::Dependency
28
+ name: sinatra
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.0'
41
+ description: A Ruby HTTP Mock Server based on Sinatra
42
+ email: mat@blocknot.es
43
+ executables:
44
+ - http-mock-server
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - Gemfile
50
+ - Gemfile.lock
51
+ - LICENSE.txt
52
+ - README.md
53
+ - bin/http-mock-server
54
+ - http-mock-server.gemspec
55
+ - lib/http-mock-server.rb
56
+ - mock.yml
57
+ homepage: https://github.com/blocknotes/http-mock-server
58
+ licenses:
59
+ - MIT
60
+ metadata: {}
61
+ post_install_message:
62
+ rdoc_options: []
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: 2.0.0
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ requirements: []
76
+ rubyforge_project:
77
+ rubygems_version: 2.6.14
78
+ signing_key:
79
+ specification_version: 4
80
+ summary: HTTP Mock Server
81
+ test_files: []