yafs 0.0.1 → 0.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/fake/fake.rb +23 -0
- data/lib/fake/request_handler.rb +30 -0
- data/lib/fake/service.rb +29 -0
- data/lib/fake_service.rb +3 -39
- data/spec/fake/request_handler_spec.rb +28 -0
- data/spec/features/fake_service_spec.rb +10 -3
- data/spec/helpers.rb +11 -0
- data/spec/spec_helper.rb +1 -0
- metadata +9 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d06981b82e35798f328314dad61f9ff6d83d055c
|
4
|
+
data.tar.gz: b9de49f8c8ec49de0fc5688e299095c9e770b91a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 28d61ef207beec9f1beceddea67e37a2245084de4f1b2d2ae0876648ba9a26f0e5a7316e12cc1d38ff21b11793d742fd10e82873b87aa33131bad7613e52a277
|
7
|
+
data.tar.gz: 0cc17559b3c5711b2383313d2f2113b813d556aa9879212ef07caf55097e7ba4e33ffdda2c2d020f9cd4f536f021d41c90837b368a91cda5245612f375eff2a8
|
data/lib/fake/fake.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
module Fake
|
2
|
+
class << self
|
3
|
+
def start(port:8080, bind:"localhost")
|
4
|
+
@fake_service = Service.new(port:port, bind:bind)
|
5
|
+
@fake_service.start
|
6
|
+
end
|
7
|
+
|
8
|
+
def stop
|
9
|
+
@fake_service.stop
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.method_missing(method, *args, &block)
|
14
|
+
if [:post, :get].include?(method)
|
15
|
+
request_handler = RequestHandler.new(method, *args)
|
16
|
+
@fake_service.add_request_handler(request_handler)
|
17
|
+
RequestHandlerBuilder.new(request_handler)
|
18
|
+
else
|
19
|
+
super
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module Fake
|
2
|
+
class RequestHandler
|
3
|
+
attr_reader :responses
|
4
|
+
|
5
|
+
#
|
6
|
+
# Creates handler for http request
|
7
|
+
# method: :get, :post, :put etc
|
8
|
+
# path: Path of the request like '/home/something'
|
9
|
+
def initialize(method, path)
|
10
|
+
@method = method
|
11
|
+
@path = path
|
12
|
+
@responses = InfiniteQueue.new
|
13
|
+
end
|
14
|
+
|
15
|
+
def call(request)
|
16
|
+
if request.path.eql?(@path) && request.request_method.eql?(@method.to_s.upcase)
|
17
|
+
current_response = @responses.next
|
18
|
+
raise "FAKE service: No response set for request #{presentation}" if current_response.nil?
|
19
|
+
Rack::Response.new([current_response.body], status=current_response.status)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
def presentation
|
25
|
+
@path
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
|
data/lib/fake/service.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
module Fake
|
2
|
+
class Service
|
3
|
+
def initialize(port:8080, bind:"localhost")
|
4
|
+
@app = RackApp.new
|
5
|
+
@webrick_config = {Port:port, BindAddress:bind}
|
6
|
+
@server = Server.new
|
7
|
+
end
|
8
|
+
|
9
|
+
def add_request_handler(request_handler)
|
10
|
+
@app.add_request_handler(request_handler)
|
11
|
+
end
|
12
|
+
|
13
|
+
def get(path)
|
14
|
+
request_handler = RequestHandler.new(:get, path)
|
15
|
+
@app.add_request_handler(request_handler)
|
16
|
+
RequestHandlerBuilder.new(request_handler)
|
17
|
+
end
|
18
|
+
|
19
|
+
def start
|
20
|
+
@server.start(@app, @webrick_config)
|
21
|
+
end
|
22
|
+
|
23
|
+
def stop
|
24
|
+
@server.stop
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
|
data/lib/fake_service.rb
CHANGED
@@ -1,6 +1,9 @@
|
|
1
1
|
require 'rack'
|
2
2
|
require 'singleton'
|
3
3
|
require 'WEBrick'
|
4
|
+
require_relative './fake/service.rb'
|
5
|
+
require_relative './fake/request_handler.rb'
|
6
|
+
require_relative './fake/fake.rb'
|
4
7
|
|
5
8
|
Thread.abort_on_exception = true
|
6
9
|
|
@@ -35,22 +38,6 @@ module Fake
|
|
35
38
|
end
|
36
39
|
end
|
37
40
|
|
38
|
-
class RequestHandler
|
39
|
-
attr_reader :responses
|
40
|
-
|
41
|
-
def initialize(path)
|
42
|
-
@path = path
|
43
|
-
@responses = InfiniteQueue.new
|
44
|
-
end
|
45
|
-
|
46
|
-
def call(request)
|
47
|
-
return unless request.path.eql? @path
|
48
|
-
current_response = @responses.next
|
49
|
-
Rack::Response.new([current_response.body], status=current_response.status)
|
50
|
-
end
|
51
|
-
|
52
|
-
end
|
53
|
-
|
54
41
|
class RequestHandlerBuilder
|
55
42
|
def initialize(request_handler)
|
56
43
|
@request_handler = request_handler
|
@@ -112,29 +99,6 @@ module Fake
|
|
112
99
|
end
|
113
100
|
end
|
114
101
|
end
|
115
|
-
|
116
|
-
class Service
|
117
|
-
def initialize(port:8080, bind:"localhost")
|
118
|
-
@app = RackApp.new
|
119
|
-
@webrick_config = {Port:port, BindAddress:bind}
|
120
|
-
@server = Server.new
|
121
|
-
end
|
122
|
-
|
123
|
-
def get(path)
|
124
|
-
request_handler = RequestHandler.new(path)
|
125
|
-
@app.add_request_handler(request_handler)
|
126
|
-
RequestHandlerBuilder.new(request_handler)
|
127
|
-
end
|
128
|
-
|
129
|
-
def start
|
130
|
-
@server.start(@app, @webrick_config)
|
131
|
-
end
|
132
|
-
|
133
|
-
def stop
|
134
|
-
@server.stop
|
135
|
-
end
|
136
|
-
end
|
137
|
-
|
138
102
|
class Requests
|
139
103
|
|
140
104
|
attr_accessor :requests
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Fake::RequestHandler do
|
4
|
+
|
5
|
+
context "when path equals request handlers path" do
|
6
|
+
context "when operation matches request operation" do
|
7
|
+
it "returns response" do
|
8
|
+
rh = Fake::RequestHandler.new(:get, '/home')
|
9
|
+
rh.responses << Fake::Response.new("", "200 OK")
|
10
|
+
expect(rh.call(get_request('http://localhost/home'))).not_to eq nil
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
context "when operation does not match with request handler operation" do
|
15
|
+
it "returns nil" do
|
16
|
+
rh = Fake::RequestHandler.new(:get, '/home')
|
17
|
+
expect(rh.call(post_request('http://localhost/home'))).to eq nil
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
context "when path does not match with request handlers path" do
|
23
|
+
it "returns nil" do
|
24
|
+
rh = Fake::RequestHandler.new(:get, '/something')
|
25
|
+
expect(rh.call(get_request('http://localhost/home'))).to eq nil
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -20,13 +20,20 @@ describe 'Fake Service' do
|
|
20
20
|
end
|
21
21
|
|
22
22
|
describe "Simple service without parameters" do
|
23
|
-
|
24
|
-
|
25
|
-
fs.start
|
23
|
+
before(:each) { Fake.start(port:4567) }
|
24
|
+
after(:each) { Fake.stop }
|
26
25
|
|
26
|
+
it "can handle get" do
|
27
|
+
Fake.get('/').respond(body:"_response_")
|
27
28
|
response = HTTParty.get('http://localhost:4567')
|
28
29
|
expect(response.body).to eq "_response_"
|
29
30
|
end
|
31
|
+
|
32
|
+
it "can handle post" do
|
33
|
+
Fake.post('/').respond(body:"_response_")
|
34
|
+
response = HTTParty.post('http://localhost:4567')
|
35
|
+
expect(response.body).to eq "_response_"
|
36
|
+
end
|
30
37
|
end
|
31
38
|
|
32
39
|
describe "Response code" do
|
data/spec/helpers.rb
ADDED
data/spec/spec_helper.rb
CHANGED
@@ -18,6 +18,7 @@
|
|
18
18
|
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
19
19
|
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), *%w[.. lib]))
|
20
20
|
require 'fake_service'
|
21
|
+
require 'helpers'
|
21
22
|
RSpec.configure do |config|
|
22
23
|
# rspec-expectations config goes here. You can use an alternate
|
23
24
|
# assertion/expectation library such as wrong or the stdlib/minitest
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: yafs
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mika Lackman
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-04-
|
11
|
+
date: 2015-04-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rack
|
@@ -31,8 +31,13 @@ extensions: []
|
|
31
31
|
extra_rdoc_files: []
|
32
32
|
files:
|
33
33
|
- README.md
|
34
|
+
- lib/fake/fake.rb
|
35
|
+
- lib/fake/request_handler.rb
|
36
|
+
- lib/fake/service.rb
|
34
37
|
- lib/fake_service.rb
|
38
|
+
- spec/fake/request_handler_spec.rb
|
35
39
|
- spec/features/fake_service_spec.rb
|
40
|
+
- spec/helpers.rb
|
36
41
|
- spec/spec_helper.rb
|
37
42
|
homepage:
|
38
43
|
licenses:
|
@@ -59,5 +64,7 @@ signing_key:
|
|
59
64
|
specification_version: 4
|
60
65
|
summary: Yet another fake http service for e2e tests
|
61
66
|
test_files:
|
67
|
+
- spec/fake/request_handler_spec.rb
|
62
68
|
- spec/features/fake_service_spec.rb
|
69
|
+
- spec/helpers.rb
|
63
70
|
- spec/spec_helper.rb
|