yafs 0.0.7 → 0.0.8

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: 92873718936b67d9105e769541532b8f6ddf927d
4
- data.tar.gz: 8edff93dc54b1fecad4716d3f4d1cbdc2e602b1d
3
+ metadata.gz: 05afd0bde033e12ab8fe7343d0e0942c9ec8fe88
4
+ data.tar.gz: 99f5bedab418c6dff2bdcba91490e13b854a1296
5
5
  SHA512:
6
- metadata.gz: 77689755ac037feacb34545ac83c65650de96181d80efa6bec45651a3a529901a618ba984df1b28ef1e5038f99c6fcee2248c7ddc6abfa5a974181e63704d374
7
- data.tar.gz: 9990bc6d017672556d40b27a30625468f2578466bcdf6acf52b13e6f62608d361a0edd810957407cc9229a636fc5d3c1f47bcd3bd01c019376f4c4fbb45ec379
6
+ metadata.gz: a7136068281561d14e3813b2d45bab51ad84645002db5ad5e8c35a8df59ecbc9ba0dae41058148cad8ddedf56e781f8342574f65868fdb08ac88c3faf0a1bd03
7
+ data.tar.gz: 8010fcf6bd214cc95add20fcd6c23a8bbc06b5bea22a44e3b911081668645f02b29025f056771d2e48bab73456205ddfb9f8deec26d9edeb6a1ef9eba4c3783c
data/README.md CHANGED
@@ -0,0 +1,17 @@
1
+ # FAKE
2
+
3
+ ## simple example
4
+ ```ruby
5
+ describe "Simple service without parameters" do
6
+ before(:each) { Fake.start(port:4568) }
7
+ after(:each) { Fake.stop }
8
+
9
+ it "can handle get" do
10
+ Fake.get('/').respond(body:"_response_")
11
+ response = HTTParty.get('http://localhost:4568')
12
+ expect(response.body).to eq "_response_"
13
+ end
14
+ end
15
+ ```
16
+
17
+
@@ -27,4 +27,5 @@ module Fake
27
27
  end
28
28
  end
29
29
 
30
+
30
31
  end
@@ -1,3 +1,6 @@
1
+ require 'URI'
2
+ require 'rack'
3
+
1
4
  module Fake
2
5
  class RequestHandler
3
6
  attr_reader :responses
@@ -9,7 +12,9 @@ module Fake
9
12
  # path: Path of the request like '/home/something'
10
13
  def initialize(method, path)
11
14
  @method = method
12
- @path = Path.new(path)
15
+ uri = URI.parse(path)
16
+ @params = parse_params(uri)
17
+ @path = Path.new(uri.path)
13
18
  @responses = InfiniteQueue.new
14
19
  @body = nil
15
20
  end
@@ -26,12 +31,23 @@ module Fake
26
31
  private
27
32
  def should_serve?(request)
28
33
  should_serve = @path.eql?(request.path) && request.request_method.eql?(@method.to_s.upcase)
34
+ if should_serve && @params != {}
35
+ should_serve = request.params == @params
36
+ end
29
37
  if should_serve && @body != nil
30
38
  should_serve = @body == request.body_string
31
39
  end
32
40
  should_serve
33
41
  end
34
42
 
43
+ def parse_params(uri)
44
+ if uri.query
45
+ Rack::Utils.parse_nested_query(uri.query)
46
+ else
47
+ {}
48
+ end
49
+ end
50
+
35
51
  def presentation
36
52
  @path
37
53
  end
@@ -0,0 +1,29 @@
1
+ module Fake
2
+ class Server
3
+
4
+ def start(rack_app, webrick_config)
5
+ return unless @server_thread.nil?
6
+ mutex = Mutex.new
7
+ server_started = ConditionVariable.new
8
+ @server_thread = Thread.new(rack_app, webrick_config) do |app, config|
9
+ @server = WEBrick::HTTPServer.new(config)
10
+ @server.mount "/", Rack::Handler::WEBrick, app
11
+ server_started.signal
12
+ @server.start
13
+ end
14
+ mutex.synchronize do
15
+ server_started.wait(mutex)
16
+ end
17
+ end
18
+
19
+ def stop
20
+ unless @server_thread.nil?
21
+ @server.shutdown
22
+ @server_thread.join if @server_thread.alive?
23
+ @server_thread = nil
24
+ end
25
+ end
26
+ end
27
+ end
28
+
29
+
@@ -10,12 +10,6 @@ module Fake
10
10
  @app.add_request_handler(request_handler)
11
11
  end
12
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
13
  def start
20
14
  @server.start(@app, @webrick_config)
21
15
  end
@@ -1,6 +1,7 @@
1
1
  require 'rack'
2
2
  require 'singleton'
3
3
  require 'WEBrick'
4
+ require_relative './fake/server.rb'
4
5
  require_relative './fake/service.rb'
5
6
  require_relative './fake/request_handler.rb'
6
7
  require_relative './fake/requests.rb'
@@ -33,11 +34,11 @@ module Fake
33
34
  end
34
35
 
35
36
  class RequestHandlerBuilder
37
+
36
38
  def initialize(request_handler)
37
39
  @request_handler = request_handler
38
40
  end
39
41
 
40
-
41
42
  #
42
43
  # DSL
43
44
  #
@@ -81,30 +82,5 @@ module Fake
81
82
  end
82
83
  end
83
84
 
84
- class Server
85
-
86
- def start(rack_app, webrick_config)
87
- return unless @server_thread.nil?
88
- mutex = Mutex.new
89
- server_started = ConditionVariable.new
90
- @server_thread = Thread.new(rack_app, webrick_config) do |app, config|
91
- @server = WEBrick::HTTPServer.new(config)
92
- @server.mount "/", Rack::Handler::WEBrick, app
93
- server_started.signal
94
- @server.start
95
- end
96
- mutex.synchronize do
97
- server_started.wait(mutex)
98
- end
99
- end
100
-
101
- def stop
102
- unless @server_thread.nil?
103
- @server.shutdown
104
- @server_thread.join if @server_thread.alive?
105
- @server_thread = nil
106
- end
107
- end
108
- end
109
85
  end
110
86
 
@@ -33,4 +33,40 @@ describe Fake::RequestHandler do
33
33
  expect(rh.call(get_request('http://localhost/home'))).to eq nil
34
34
  end
35
35
  end
36
+
37
+ context "when path contains query parameters" do
38
+ context "when path and parameters match" do
39
+ it "returns the response" do
40
+ rh = Fake::RequestHandler.new(:get, '/home?param=1')
41
+ rh.responses << Fake::Response.new("", "200 OK", {})
42
+ expect(rh.call(get_request('http://localhost/home?param=1'))).not_to eq nil
43
+ end
44
+ end
45
+
46
+ context "when path matches but parameter does not match" do
47
+ it "returns nil" do
48
+ rh = Fake::RequestHandler.new(:get, '/home?param=1')
49
+ rh.responses << Fake::Response.new("", "200 OK", {})
50
+ expect(rh.call(get_request('http://localhost/home?param=2'))).to eq nil
51
+ end
52
+ end
53
+ end
54
+
55
+ context "when path is not well formatted with query params" do
56
+ context "when path and query matches" do
57
+ it "returns the response" do
58
+ rh = Fake::RequestHandler.new(:get, '/home/?param=1')
59
+ rh.responses << Fake::Response.new("", "200 OK", {})
60
+ expect(rh.call(get_request('http://localhost/home/?param=1'))).not_to eq nil
61
+ end
62
+ end
63
+ context "when path matches but query does not" do
64
+ it "returns the response" do
65
+ rh = Fake::RequestHandler.new(:get, '/home/?param=2')
66
+ rh.responses << Fake::Response.new("", "200 OK", {})
67
+ expect(rh.call(get_request('http://localhost/home/?param=1'))).to eq nil
68
+ end
69
+ end
70
+
71
+ end
36
72
  end
@@ -7,11 +7,11 @@ describe 'Fake Service' do
7
7
  after(:each) { fs.stop }
8
8
 
9
9
  describe "configuration" do
10
- let(:fs) { Fake::Service.new(bind:'127.0.0.1') }
11
10
  it "can be bind to different addresses" do
12
- fs.get('/').respond(body:"ok")
13
- fs.start
11
+ Fake.start(bind:'127.0.0.1', port:8080)
12
+ Fake.get('/').respond(body:"ok")
14
13
  expect(HTTParty.get('http://127.0.0.1:8080').body).to eq "ok"
14
+ Fake.stop
15
15
  end
16
16
  end
17
17
 
@@ -30,6 +30,11 @@ describe 'Fake Service' do
30
30
  expect(response.body).to eq "_response_"
31
31
  end
32
32
 
33
+ it "can handle shitty path" do
34
+ Fake.get('/path/?id=5').respond(body: 'ok')
35
+ expect(HTTParty.get('http://localhost:4568/path/?id=5').body).to eq 'ok'
36
+ end
37
+
33
38
  it "can handle post" do
34
39
  Fake.post('/').respond(body:"_response_")
35
40
  response = HTTParty.post('http://localhost:4568')
@@ -47,41 +52,44 @@ describe 'Fake Service' do
47
52
 
48
53
  describe "Response code" do
49
54
  it "returns assigned response code" do
50
- fs.get('/').respond(status:404)
51
- fs.start
52
-
55
+ Fake.start(port:4567)
56
+ Fake.get('/').respond(status:404)
53
57
  response = HTTParty.get('http://localhost:4567')
54
58
  expect(response.code).to eq 404
59
+ Fake.stop
55
60
  end
56
61
  end
57
62
 
58
63
  describe "Headers" do
59
64
  it "sets headers to response" do
60
- fs.get('/').respond(headers: {"x-header" => "someurl"})
61
- fs.start
65
+ Fake.start(port:4567)
66
+ Fake.get('/').respond(headers: {"x-header" => "someurl"})
62
67
  response = HTTParty.get('http://localhost:4567')
63
68
  expect(response.headers).to include("x-header")
64
69
  expect(response.headers['x-header']).to eq "someurl"
70
+ Fake.stop
65
71
  end
66
72
  end
67
73
 
68
74
  describe "Paths" do
69
75
  describe "static paths" do
70
76
  it "routes requests to correct handler" do
71
- fs.get('/cart').respond(body:"1")
72
- fs.get('/order/new').respond(body:"2")
73
- fs.start
77
+ Fake.start(port:4567)
78
+ Fake.get('/cart').respond(body:"1")
79
+ Fake.get('/order/new').respond(body:"2")
74
80
 
75
81
  expect(HTTParty.get('http://localhost:4567/cart' ).response.body).to eq "1"
76
82
  expect(HTTParty.get('http://localhost:4567/order/new').response.body).to eq "2"
83
+ Fake.stop
77
84
  end
78
85
  end
79
86
 
80
87
  describe "dynamic paths" do
81
88
  it "routes request to match path variables" do
82
- fs.get('/cart/:id/status').respond(body:"ok")
83
- fs.start
89
+ Fake.start(port:4567)
90
+ Fake.get('/cart/:id/status').respond(body:"ok")
84
91
  expect(HTTParty.get('http://localhost:4567/cart/path/status').response.body).to eq "ok"
92
+ Fake.stop
85
93
  end
86
94
  end
87
95
 
@@ -93,9 +101,10 @@ describe 'Fake Service' do
93
101
  Fake.stop
94
102
  end
95
103
 
96
- xit "can respond based on query parameter" do
104
+ it "can respond based on query parameter" do
97
105
  # what about specific parameters vs some parameter
98
- fs.get('/cart/option').param('id=5').respond("ok") # Matches /cart/option?id=5,
106
+ Fake.get('/cart/option?id=5').respond(body: "ok")
107
+ expect(HTTParty.get('http://localhost:4567/cart/option?id=5').response.body).to eq "ok"
99
108
  end
100
109
 
101
110
  it "can resbond based on specific body" do
@@ -108,22 +117,24 @@ describe 'Fake Service' do
108
117
 
109
118
  describe "Checking request parameters" do
110
119
  it "can verify single parameter" do
111
- fs.get('/cart').respond(body:'')
112
- fs.start
120
+ Fake.start(port:4567)
121
+ Fake.get('/cart').respond(body:'')
113
122
  HTTParty.get('http://localhost:4567/cart?id=5&status=pending')
114
123
  params = Fake::Requests.request(:get, '/cart').params
115
124
  expect(params.has_key? 'id')
116
125
  expect(params.has_key? 'status')
117
126
  expect(params["id"]).to eq "5"
118
127
  expect(params["status"]).to eq "pending"
128
+ Fake.stop
119
129
 
120
130
  end
121
131
 
122
132
  xit "can name a request to ease matching of request" do
123
- fs.get('/cart', name: :my_cart_request)
124
- fs.start
133
+ Fake.start
134
+ Fake.get('/cart', name: :my_cart_request)
125
135
  HTTParty.get('http://localhost:4567/cart?id=5&status=pending')
126
136
  expect(Fake::Requests.request(:my_cart_request)).not_to be nil
137
+ Fake.stop
127
138
  end
128
139
 
129
140
  context "from post request" do
@@ -173,32 +184,34 @@ describe 'Fake Service' do
173
184
  describe "Setting responses" do
174
185
  describe "chaining responses" do
175
186
  it "returns responses in order" do
176
- fs.get('/cart').respond(body:"1").respond(body:"2").respond(body:"3")
177
- fs.start
187
+ Fake.start(port:4567)
188
+ Fake.get('/cart').respond(body:"1").respond(body:"2").respond(body:"3")
178
189
  expect(HTTParty.get('http://localhost:4567/cart').response.body).to eq "1"
179
190
  expect(HTTParty.get('http://localhost:4567/cart').response.body).to eq "2"
180
191
  expect(HTTParty.get('http://localhost:4567/cart').response.body).to eq "3"
181
192
  expect(HTTParty.get('http://localhost:4567/cart').response.body).to eq "3"
182
193
  expect(HTTParty.get('http://localhost:4567/cart').response.body).to eq "3"
194
+ Fake.stop
183
195
  end
184
196
  end
185
197
  end
186
198
 
187
199
  describe "using block" do
188
200
  it "uses block return value as response" do
189
- fs.get('/').respond do
201
+ Fake.start(port:4567)
202
+ Fake.get('/').respond do
190
203
  "my response"
191
204
  end
192
- fs.start
193
205
  expect(HTTParty.get('http://localhost:4567').response.body).to eq "my response"
206
+ Fake.stop
194
207
  end
195
208
 
196
209
  it "can access response object" do
197
- fs.get('/').respond do |r|
210
+ Fake.start(port:4567)
211
+ Fake.get('/').respond do |r|
198
212
  r.status = 201
199
213
  "response"
200
214
  end
201
- fs.start
202
215
  expect(HTTParty.get('http://localhost:4567').response.code).to eq "201"
203
216
  end
204
217
 
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.7
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mika Lackman
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-05-19 00:00:00.000000000 Z
11
+ date: 2017-06-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack
@@ -37,6 +37,7 @@ files:
37
37
  - lib/fake/request_handler.rb
38
38
  - lib/fake/requests.rb
39
39
  - lib/fake/response.rb
40
+ - lib/fake/server.rb
40
41
  - lib/fake/service.rb
41
42
  - lib/fake_service.rb
42
43
  - spec/fake/path_spec.rb
@@ -65,7 +66,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
65
66
  version: '0'
66
67
  requirements: []
67
68
  rubyforge_project:
68
- rubygems_version: 2.2.2
69
+ rubygems_version: 2.5.1
69
70
  signing_key:
70
71
  specification_version: 4
71
72
  summary: Yet another fake http service for e2e tests
@@ -76,4 +77,3 @@ test_files:
76
77
  - spec/features/fake_service_spec.rb
77
78
  - spec/helpers.rb
78
79
  - spec/spec_helper.rb
79
- has_rdoc: