yafs 0.0.3 → 0.0.4

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a4b12f8d927b65c8d4ebaaca97000c2634981ebd
4
- data.tar.gz: 0d3b33afc2c86a9836f6c2a7fed056cf6b177627
3
+ metadata.gz: 727f42ad904ad4b964734e7b781fadc39d49d6c4
4
+ data.tar.gz: 408a260d5fa3faac8a5300d0c5b47d4a71124dc8
5
5
  SHA512:
6
- metadata.gz: 5a1e3d09db2a1505d0644f3879aa2dbe06a5f47afe56b971ac139d1a3ca78d705f360e80d213ce22c50607c748e3f3d4803058702ae5b3c9e39348189369563c
7
- data.tar.gz: b14f0b4923ebf217ee75cb8e46f1017b155df21a0dc7159110dd5cb49858d4ef99d80a867c339a25796139531a9acf2880aa2c4049e6789b99a1ac3b9cbd779d
6
+ metadata.gz: 6701a72834539e7f381a5d81fe9ff1ff4c18b3b78a74b55a220a5b73c443f51b7e4e00ff1f628587995029be83b1101a35a49feabd73c32e54ff136a27534313
7
+ data.tar.gz: e84cf041c37ea207fe505e0f62c098d82e9dd949c5d94acacf19f6b726f2fc1574cca0ddc638ebed788fe7423d7348a84b1ef4464550792809bcfcbc447d5559
@@ -16,7 +16,8 @@ module Fake
16
16
  if request.path.eql?(@path) && request.request_method.eql?(@method.to_s.upcase)
17
17
  current_response = @responses.next
18
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)
19
+ current_response.evaluate()
20
+ Rack::Response.new([current_response.body], status=current_response.status, header=current_response.headers)
20
21
  end
21
22
  end
22
23
 
@@ -0,0 +1,24 @@
1
+
2
+ module Fake
3
+ class Response
4
+ attr_reader :body
5
+ attr_accessor :status
6
+ attr_accessor :headers
7
+
8
+ def initialize(body, status, headers, &block)
9
+ raise "Response created with body and block. Only other can be given" if body && block
10
+ @body = body
11
+ @status = status
12
+ @block = block
13
+ @headers = headers
14
+ end
15
+
16
+ def evaluate
17
+ if !@body && @block
18
+ @body = @block.call(self)
19
+ end
20
+ @body
21
+ end
22
+
23
+ end
24
+ end
@@ -6,20 +6,11 @@ require_relative './fake/request_handler.rb'
6
6
  require_relative './fake/requests.rb'
7
7
  require_relative './fake/request.rb'
8
8
  require_relative './fake/fake.rb'
9
+ require_relative './fake/response.rb'
9
10
 
10
11
  Thread.abort_on_exception = true
11
12
 
12
13
  module Fake
13
- class Response
14
- attr_reader :body, :status
15
-
16
- def initialize(body, status)
17
- @body = body
18
- @status = status
19
- end
20
-
21
- end
22
-
23
14
  #
24
15
  # Queue which returns last value forever
25
16
  #
@@ -48,8 +39,8 @@ module Fake
48
39
  #
49
40
  # DSL
50
41
  #
51
- def respond(body:nil, status:200)
52
- @request_handler.responses << Response.new(body, status)
42
+ def respond(body:nil, status:200, headers:{}, &block)
43
+ @request_handler.responses << Response.new(body, status, headers, &block)
53
44
  self
54
45
  end
55
46
  end
@@ -6,7 +6,7 @@ describe Fake::RequestHandler do
6
6
  context "when operation matches request operation" do
7
7
  it "returns response" do
8
8
  rh = Fake::RequestHandler.new(:get, '/home')
9
- rh.responses << Fake::Response.new("", "200 OK")
9
+ rh.responses << Fake::Response.new("", "200 OK", {})
10
10
  expect(rh.call(get_request('http://localhost/home'))).not_to eq nil
11
11
  end
12
12
  end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+
3
+
4
+ describe Fake::Response do
5
+
6
+ it "raises exception if body and block are given" do
7
+ expect { Fake::Response.new("ok", 200, {}) {} }.to raise_error
8
+ end
9
+
10
+ describe "#evaluate" do
11
+ it "makes the body for the response from the block" do
12
+ r = Fake::Response.new(nil, 200, {}) { "response" }
13
+ r.evaluate()
14
+ expect(r.body).to eq "response"
15
+ end
16
+
17
+ it "makes the body from the body param" do
18
+ r = Fake::Response.new("response", 200, {})
19
+ r.evaluate()
20
+ expect(r.body).to eq "response"
21
+ end
22
+
23
+ it "makes the response object available for the block" do
24
+ r = Fake::Response.new(nil, 200, {}) {|r| r.status = 201 }
25
+ r.evaluate()
26
+ expect(r.status).to eq 201
27
+
28
+ end
29
+ end
30
+
31
+ end
@@ -1,5 +1,6 @@
1
1
  require 'spec_helper'
2
2
  require 'httparty'
3
+ require 'pry'
3
4
 
4
5
  describe 'Fake Service' do
5
6
  let(:fs) { Fake::Service.new(port:4567) }
@@ -46,6 +47,16 @@ describe 'Fake Service' do
46
47
  end
47
48
  end
48
49
 
50
+ describe "Headers" do
51
+ it "sets headers to response" do
52
+ fs.get('/').respond(headers: {"x-header" => "someurl"})
53
+ fs.start
54
+ response = HTTParty.get('http://localhost:4567')
55
+ expect(response.headers).to include("x-header")
56
+ expect(response.headers['x-header']).to eq "someurl"
57
+ end
58
+ end
59
+
49
60
  describe "Paths" do
50
61
  describe "static paths" do
51
62
  it "routes requests to correct handler" do
@@ -75,7 +86,7 @@ describe 'Fake Service' do
75
86
 
76
87
  describe "Checking request parameters" do
77
88
  it "can verify single parameter" do
78
- fs.get('/cart')
89
+ fs.get('/cart').respond(body:'')
79
90
  fs.start
80
91
  HTTParty.get('http://localhost:4567/cart?id=5&status=pending')
81
92
  params = Fake::Requests.request(:get, '/cart').params
@@ -83,6 +94,7 @@ describe 'Fake Service' do
83
94
  expect(params.has_key? 'status')
84
95
  expect(params["id"]).to eq "5"
85
96
  expect(params["status"]).to eq "pending"
97
+
86
98
  end
87
99
 
88
100
  xit "can name a request to ease matching of request" do
@@ -150,4 +162,25 @@ describe 'Fake Service' do
150
162
  end
151
163
  end
152
164
 
165
+ describe "using block" do
166
+ it "uses block return value as response" do
167
+ fs.get('/').respond do
168
+ "my response"
169
+ end
170
+ fs.start
171
+ expect(HTTParty.get('http://localhost:4567').response.body).to eq "my response"
172
+ end
173
+
174
+ it "can access response object" do
175
+ fs.get('/').respond do |r|
176
+ r.status = 201
177
+ "response"
178
+ end
179
+ fs.start
180
+ expect(HTTParty.get('http://localhost:4567').response.code).to eq "201"
181
+ end
182
+
183
+
184
+ end
185
+
153
186
  end
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.3
4
+ version: 0.0.4
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-25 00:00:00.000000000 Z
11
+ date: 2016-01-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack
@@ -35,9 +35,11 @@ files:
35
35
  - lib/fake/request.rb
36
36
  - lib/fake/request_handler.rb
37
37
  - lib/fake/requests.rb
38
+ - lib/fake/response.rb
38
39
  - lib/fake/service.rb
39
40
  - lib/fake_service.rb
40
41
  - spec/fake/request_handler_spec.rb
42
+ - spec/fake/response_spec.rb
41
43
  - spec/features/fake_service_spec.rb
42
44
  - spec/helpers.rb
43
45
  - spec/spec_helper.rb
@@ -67,6 +69,7 @@ specification_version: 4
67
69
  summary: Yet another fake http service for e2e tests
68
70
  test_files:
69
71
  - spec/fake/request_handler_spec.rb
72
+ - spec/fake/response_spec.rb
70
73
  - spec/features/fake_service_spec.rb
71
74
  - spec/helpers.rb
72
75
  - spec/spec_helper.rb