fake_service 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +18 -0
- data/Gemfile +12 -0
- data/LICENSE.txt +22 -0
- data/README.md +36 -0
- data/Rakefile +5 -0
- data/bin/fake_service +17 -0
- data/fake_service.gemspec +25 -0
- data/lib/fake_service.rb +3 -0
- data/lib/fake_service/middleware.rb +28 -0
- data/lib/fake_service/server.rb +38 -0
- data/lib/fake_service/version.rb +3 -0
- data/test/fake_service_test.rb +83 -0
- data/test/test.yml +31 -0
- data/test/test_helper.rb +9 -0
- metadata +119 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d14e7289238db896370b25fd0afd69a16d5392b2
|
4
|
+
data.tar.gz: c4a1eb565a54200ebbe2bdc3b3174d4429843098
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6a2145ad6b97c98bf9e0303c31bfc20efc7680d643a01c0e6b03798830de1d60fdde4d8dade81eba7a54eeafa92a6575280e5b37f0b803a8c9b0da3b50652ca1
|
7
|
+
data.tar.gz: 704554575859b589a19794eecb22730fa0e3c2f49ce9ba3b82278ea88336e89717b7b3f3619118339763de17ec0f91b92123a5bcd39b1bf8f382c1e5fc9b0cde
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 TODO: Write your name
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# FakeService
|
2
|
+
|
3
|
+
Fake service for [reqres](https://github.com/antonversal/reqres).
|
4
|
+
It takes generated file by [reqres](https://github.com/antonversal/reqres),
|
5
|
+
define actions and
|
6
|
+
runs server.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
gem 'fake_service'
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install fake_service
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
$ fake_service path/to/file
|
25
|
+
|
26
|
+
fake_service behaves like Rack::Server and can accept different options:
|
27
|
+
|
28
|
+
$ fake_service path/to/file -D -p 4567 -P path/to/pid
|
29
|
+
|
30
|
+
## Contributing
|
31
|
+
|
32
|
+
1. Fork it
|
33
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
34
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
35
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
36
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/bin/fake_service
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
$:.unshift File.expand_path("../../lib", __FILE__)
|
4
|
+
|
5
|
+
require "fake_service"
|
6
|
+
|
7
|
+
yaml_path = ARGV.shift
|
8
|
+
|
9
|
+
unless yaml_path && File.exists?(yaml_path)
|
10
|
+
raise "Yaml file is not found! Please set yaml file: fake_service /pat/to/file."
|
11
|
+
end
|
12
|
+
|
13
|
+
FakeService::Server.set(:file_path, yaml_path)
|
14
|
+
|
15
|
+
options = Rack::Server::Options.new.parse!(ARGV)
|
16
|
+
options.merge!(app: FakeService::Server)
|
17
|
+
Rack::Server.start(options)
|
@@ -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
|
+
require 'fake_service/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "fake_service"
|
8
|
+
spec.version = FakeService::VERSION
|
9
|
+
spec.authors = ["Anton Versal"]
|
10
|
+
spec.email = ["ant.ver@gmail.com"]
|
11
|
+
spec.description = %q{Fake service for reqres}
|
12
|
+
spec.summary = %q{Fake service for reqres}
|
13
|
+
spec.homepage = "https://github.com/antonversal/fake_service"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
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.add_dependency "sinatra"
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
23
|
+
spec.add_development_dependency "rake"
|
24
|
+
spec.add_development_dependency "rack-test"
|
25
|
+
end
|
data/lib/fake_service.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# This middleware allows to define all methods from yaml file once.
|
2
|
+
module FakeService
|
3
|
+
class Middleware
|
4
|
+
def initialize(app)
|
5
|
+
@app = app
|
6
|
+
end
|
7
|
+
|
8
|
+
# defines actions for each request in yaml file.
|
9
|
+
def define_actions
|
10
|
+
unless @action_defined
|
11
|
+
hash = YAML.load(File.read(@file_path))
|
12
|
+
hash.each do |k, v|
|
13
|
+
v.each do |key, value|
|
14
|
+
@app.class.define_action!(value["request"], value["response"])
|
15
|
+
end
|
16
|
+
end
|
17
|
+
@action_defined = true
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
# Rack call interface.
|
22
|
+
def call(env)
|
23
|
+
@file_path ||= @app.settings.file_path
|
24
|
+
self.define_actions
|
25
|
+
@app.call(env)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
require 'sinatra/base'
|
3
|
+
require 'json'
|
4
|
+
require File.expand_path '../middleware', __FILE__
|
5
|
+
|
6
|
+
module FakeService
|
7
|
+
class Server < Sinatra::Base
|
8
|
+
#set this attribute from runner
|
9
|
+
#set :file_path, path_to_file
|
10
|
+
|
11
|
+
use Middleware
|
12
|
+
|
13
|
+
# Checks for bodies equality. json bodies keys can have different orders.
|
14
|
+
def equal_json_bodies?(expected_body, body)
|
15
|
+
JSON.parse(expected_body) == JSON.parse(body)
|
16
|
+
end
|
17
|
+
|
18
|
+
# Checks for request comes with right header.
|
19
|
+
def right_header?(expected, actual)
|
20
|
+
expected.inject(true) do |memo, (k, v)|
|
21
|
+
memo && v == actual[k]
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
# define action.
|
26
|
+
def self.define_action!(expected_request, expected_response)
|
27
|
+
action = expected_request["method"].downcase
|
28
|
+
url = expected_request["full_path"]
|
29
|
+
send(action, url, provides: :json) do
|
30
|
+
pass unless right_header?(expected_request["headers"], request.env)
|
31
|
+
pass unless equal_json_bodies?(expected_request["body"], request.body.read)
|
32
|
+
headers expected_response["headers"]
|
33
|
+
status expected_response["code"]
|
34
|
+
expected_response["body"]
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
require File.expand_path '../test_helper.rb', __FILE__
|
2
|
+
|
3
|
+
class FakeServiceTest < MiniTest::Unit::TestCase
|
4
|
+
include Rack::Test::Methods
|
5
|
+
FakeService::Server.set(:file_path, File.expand_path('../test.yml', __FILE__))
|
6
|
+
|
7
|
+
def app
|
8
|
+
FakeService::Server
|
9
|
+
end
|
10
|
+
|
11
|
+
def valid_header
|
12
|
+
{"CONTENT_TYPE" => 'application/json',
|
13
|
+
'ACCEPT' => "application/json",
|
14
|
+
'HTTP_AUTHORIZATION' => "Token token=\"14d75ec70594d026de22f02502e74be9\""}
|
15
|
+
end
|
16
|
+
|
17
|
+
def valid_body
|
18
|
+
'{"name":"Bar","description":"FooBar"}'
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_retuning_right_code
|
22
|
+
post '/v1/foos', valid_body, valid_header
|
23
|
+
assert_equal 201, last_response.status
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_retuning_right_code_json_independet_place
|
27
|
+
post '/v1/foos', '{"description":"FooBar","name":"Bar"}', valid_header
|
28
|
+
assert_equal 201, last_response.status
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_reject_if_body_not_match
|
32
|
+
post '/v1/foos', '{"description":"FooBar"}', valid_header
|
33
|
+
assert_equal 404, last_response.status
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_returns_body
|
37
|
+
post '/v1/foos', valid_body, valid_header
|
38
|
+
expected = '{"id":974435878,"name":"Bar","description":"FooBar"}'
|
39
|
+
assert_equal expected, last_response.body
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_wrong_authorization_header
|
43
|
+
header = valid_header.merge("HTTP_AUTHORIZATION" => "123")
|
44
|
+
post '/v1/foos', valid_body, header
|
45
|
+
assert_equal 404, last_response.status
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_wrong_accept
|
49
|
+
header = valid_header.merge("ACCEPT" => "123")
|
50
|
+
post '/v1/foos', valid_body, header
|
51
|
+
assert_equal 404, last_response.status
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_wrong_content_type
|
55
|
+
header = valid_header.merge("CONTENT_TYPE" => "123")
|
56
|
+
post '/v1/foos', valid_body, header
|
57
|
+
assert_equal 404, last_response.status
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_when_one_header_is_missing
|
61
|
+
header = valid_header.delete_if { |k, v| k =="HTTP_AUTHORIZATION" }
|
62
|
+
post '/v1/foos', valid_body, header
|
63
|
+
assert_equal 404, last_response.status
|
64
|
+
end
|
65
|
+
|
66
|
+
def test_haders_in_responce
|
67
|
+
post '/v1/foos', valid_body, valid_header
|
68
|
+
headers = {
|
69
|
+
"X-Frame-Options" => "SAMEORIGIN",
|
70
|
+
"X-XSS-Protection" => "1; mode=block",
|
71
|
+
"X-Content-Type-Options" => "nosniff",
|
72
|
+
"X-UA-Compatible" => "chrome=1",
|
73
|
+
"Location" => "http://www.example.com/v1/foos",
|
74
|
+
"Content-Type" => "text/html; charset=utf-8",
|
75
|
+
"ETag" => "\"3ac3a959a1a3466cd1a1e4f5939e339b\"",
|
76
|
+
"Cache-Control" => "max-age=0, private, must-revalidate",
|
77
|
+
"X-Request-Id" => "45efbba1-a3b9-4651-81a3-819290570a80",
|
78
|
+
"X-Runtime" => "0.028181",
|
79
|
+
"Content-Length" => "52"
|
80
|
+
}
|
81
|
+
assert_equal headers, last_response.headers
|
82
|
+
end
|
83
|
+
end
|
data/test/test.yml
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
---
|
2
|
+
ManageFooTest:
|
3
|
+
test_creates_foo:
|
4
|
+
request:
|
5
|
+
url: http://www.example.com/v1/foos
|
6
|
+
full_path: /v1/foos
|
7
|
+
params:
|
8
|
+
name: Bar
|
9
|
+
description: FooBar
|
10
|
+
action: create
|
11
|
+
controller: v1/foos
|
12
|
+
method: POST
|
13
|
+
body: '{"name":"Bar","description":"FooBar"}'
|
14
|
+
headers:
|
15
|
+
HTTP_AUTHORIZATION: Token token="14d75ec70594d026de22f02502e74be9"
|
16
|
+
CONTENT_TYPE: application/json
|
17
|
+
ACCEPT: application/json
|
18
|
+
response:
|
19
|
+
headers:
|
20
|
+
X-Frame-Options: SAMEORIGIN
|
21
|
+
X-XSS-Protection: 1; mode=block
|
22
|
+
X-Content-Type-Options: nosniff
|
23
|
+
X-UA-Compatible: chrome=1
|
24
|
+
Location: http://www.example.com/v1/foos
|
25
|
+
Content-Type: text/html; charset=utf-8
|
26
|
+
ETag: '"3ac3a959a1a3466cd1a1e4f5939e339b"'
|
27
|
+
Cache-Control: max-age=0, private, must-revalidate
|
28
|
+
X-Request-Id: 45efbba1-a3b9-4651-81a3-819290570a80
|
29
|
+
X-Runtime: '0.028181'
|
30
|
+
code: '201'
|
31
|
+
body: '{"id":974435878,"name":"Bar","description":"FooBar"}'
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,119 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fake_service
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Anton Versal
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-10-24 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: sinatra
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.3'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.3'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rack-test
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: Fake service for reqres
|
70
|
+
email:
|
71
|
+
- ant.ver@gmail.com
|
72
|
+
executables:
|
73
|
+
- fake_service
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- .gitignore
|
78
|
+
- Gemfile
|
79
|
+
- Gemfile.lock
|
80
|
+
- LICENSE.txt
|
81
|
+
- README.md
|
82
|
+
- Rakefile
|
83
|
+
- bin/fake_service
|
84
|
+
- fake_service.gemspec
|
85
|
+
- lib/fake_service.rb
|
86
|
+
- lib/fake_service/middleware.rb
|
87
|
+
- lib/fake_service/server.rb
|
88
|
+
- lib/fake_service/version.rb
|
89
|
+
- test/fake_service_test.rb
|
90
|
+
- test/test.yml
|
91
|
+
- test/test_helper.rb
|
92
|
+
homepage: https://github.com/antonversal/fake_service
|
93
|
+
licenses:
|
94
|
+
- MIT
|
95
|
+
metadata: {}
|
96
|
+
post_install_message:
|
97
|
+
rdoc_options: []
|
98
|
+
require_paths:
|
99
|
+
- lib
|
100
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - '>='
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
requirements: []
|
111
|
+
rubyforge_project:
|
112
|
+
rubygems_version: 2.1.9
|
113
|
+
signing_key:
|
114
|
+
specification_version: 4
|
115
|
+
summary: Fake service for reqres
|
116
|
+
test_files:
|
117
|
+
- test/fake_service_test.rb
|
118
|
+
- test/test.yml
|
119
|
+
- test/test_helper.rb
|