webmachine-test 0.3.0 → 0.4.0

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: 189b72179cd8a93c2bf0b9d207cba120d3a23d90
4
- data.tar.gz: d5208e1169a927d2fc6e205733c64b1ccbb9e19b
3
+ metadata.gz: dbb4219bac475070c93f974353e7fd69112c162b
4
+ data.tar.gz: 877f3a36c7d4a6f7e326bb3771f137b1ec7cefc5
5
5
  SHA512:
6
- metadata.gz: 04f460df1ec0baabd99ec75075d2b1daca8d258ed11ef6f90de9992971ee13740d655c38f2e753943c437fedd6ee706f52ba0bb720398c5df9a2643241303567
7
- data.tar.gz: 2bd2bc4fb9bd5582f80290729849fff7c3694794eff0eaddc397d5706b0f421f808199cb1a4d22a2386772e09d8c338f1d35b1af299ee57bf868762a4b699600
6
+ metadata.gz: 93fbb64af4c096b346d2d5f6551560b6b5acbeebb40d6c9aa04f27764de5ed426f193317913bcbbee31d5cdd6cc1705b58ea6d61a51b742b5dd184756a28e782
7
+ data.tar.gz: d742cc1bf715ea74b334afd4d0d5668d9913269bf5396605306c3e7a3beaefcf12ff3e02a0d7df83486b274ff0311d5d2b200353bfb6a5f6655d87ba3985e08d
data/Gemfile CHANGED
@@ -1,4 +1,4 @@
1
1
  source 'https://rubygems.org'
2
2
  gemspec
3
3
 
4
- gem 'webmachine', github: 'seancribbs/webmachine-ruby'
4
+ gem 'webmachine', github: 'webmachine/webmachine-ruby'
@@ -14,7 +14,7 @@ module Webmachine
14
14
  @session ||= Webmachine::Test::Session.new(app)
15
15
  end
16
16
 
17
- def_delegators :current_session, :request, :header, :headers, :body, :response,
17
+ def_delegators :current_session, :request, :header, :headers, :cookie, :session_cookies, :body, :response,
18
18
  :get, :post, :put, :patch, :delete,
19
19
  :options, :head
20
20
  end
@@ -10,6 +10,7 @@ module Webmachine
10
10
 
11
11
  def initialize(app)
12
12
  @headers = Webmachine::Headers.new
13
+ @session_cookies = {}
13
14
  @body = nil
14
15
  @req = nil
15
16
  @res = nil
@@ -31,6 +32,14 @@ module Webmachine
31
32
  @headers[name] = value
32
33
  end
33
34
 
35
+ def cookie(name, value)
36
+ @session_cookies[name] = value
37
+ end
38
+
39
+ def session_cookies
40
+ @session_cookies
41
+ end
42
+
34
43
  # Set the request body.
35
44
  def body(value)
36
45
  @body = value
@@ -56,21 +65,53 @@ module Webmachine
56
65
  add_query_params(uri, options[:params])
57
66
 
58
67
  # Set some default headers and merge the provided ones.
59
- @headers['Host'] = [uri.host, uri.port].compact.join(':') unless @headers['Host']
60
- @headers['Accept'] = '*/*' unless @headers['Accept']
68
+ @headers['Host'] ||= [uri.host, uri.port].compact.join(':')
69
+ @headers['Accept'] ||= '*/*'
70
+ @headers['Origin'] ||= @headers['Host']
71
+ add_cookies
61
72
 
62
73
  options[:headers] ||= {}
63
74
  options[:headers].each { |k, v| @headers[k] = v }
64
75
 
76
+
65
77
  @body ||= options[:body]
66
78
 
67
79
  @req = Webmachine::Request.new(method, uri, @headers, @body)
68
80
  @res = Webmachine::Response.new
69
81
 
70
82
  @app.dispatcher.dispatch(@req, @res)
83
+ update_session_cookies
71
84
  return @res
72
85
  end
73
86
 
87
+ def update_session_cookies
88
+ if @res.headers['Set-Cookie']
89
+ @res.headers['Set-Cookie'].reduce(@session_cookies) do |cookies, c|
90
+ cookie_info = c.split(';')
91
+ kv = Webmachine::Cookie.parse(cookie_info.shift)
92
+ cname = kv.keys.first
93
+ cval = kv.values.first
94
+ cookie = cookie_info.map(&:strip).reduce({value: cval}) do |cookie, part|
95
+ case part
96
+ when 'Secure'
97
+ cookie[:secure] = true
98
+ when 'HttpOnly'
99
+ cookie[:http_only] = true
100
+ when /Expires=(?<date>.*)/
101
+ cookie[:expires] = Time.parse($~[:date])
102
+ end
103
+ cookie
104
+ end
105
+ if cookie[:value].nil? or (cookie[:expires] and cookie[:expires] < Time.now)
106
+ cookies.delete cname
107
+ else
108
+ cookies.merge!({cname => cval})
109
+ end
110
+ cookies
111
+ end
112
+ end
113
+ end
114
+
74
115
  def add_query_params(uri, params)
75
116
  if params
76
117
  q = params.map do |k, v|
@@ -81,6 +122,14 @@ module Webmachine
81
122
  uri.query = uri.query ? [uri.query, q].join('&') : q
82
123
  end
83
124
  end
125
+
126
+ def add_cookies
127
+ as_strings = @session_cookies.map do |k,v|
128
+ "#{k}=#{v}"
129
+ end
130
+ cookies = as_strings.join("; ")
131
+ @headers['Cookie'] = cookies unless cookies.empty?
132
+ end
84
133
  end
85
134
  end
86
135
  end
@@ -1,5 +1,5 @@
1
1
  module Webmachine
2
2
  module Test
3
- VERSION = '0.3.0'
3
+ VERSION = '0.4.0'
4
4
  end
5
5
  end
@@ -4,6 +4,7 @@ class TestResource < Webmachine::Resource
4
4
  end
5
5
 
6
6
  def to_text
7
+ response.set_cookie 'TEST', 'VALUE'
7
8
  'OK'
8
9
  end
9
10
  end
@@ -151,4 +151,21 @@ describe Webmachine::Test::Session do
151
151
  request.body.should be_nil
152
152
  end
153
153
  end
154
+ describe "#cookie" do
155
+ it "sets a cookie" do
156
+ cookie('SOME_COOKIE', 'VALUE')
157
+ get '/'
158
+ request.headers['COOKIE'].should eq('SOME_COOKIE=VALUE')
159
+ end
160
+ it "forwards cookies sent by the server (like a browser)" do
161
+ get '/'
162
+ request.headers['COOKIE'].should be_nil
163
+ get '/'
164
+ request.headers['COOKIE'].should eq('TEST=VALUE')
165
+ end
166
+ it "shows available session cookies" do
167
+ get '/'
168
+ session_cookies['TEST'].should eq('VALUE')
169
+ end
170
+ end
154
171
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: webmachine-test
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bernd Ahlers
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-13 00:00:00.000000000 Z
11
+ date: 2017-02-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: webmachine
@@ -118,7 +118,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
118
118
  version: '0'
119
119
  requirements: []
120
120
  rubyforge_project:
121
- rubygems_version: 2.2.2
121
+ rubygems_version: 2.5.1
122
122
  signing_key:
123
123
  specification_version: 4
124
124
  summary: Test API for webmachine-ruby
@@ -126,7 +126,7 @@ test_files:
126
126
  - Gemfile
127
127
  - Rakefile
128
128
  - ".rspec"
129
- - spec/webmachine/test/session_spec.rb
130
- - spec/spec_helper.rb
131
129
  - spec/fixtures/traceable_resource.rb
132
130
  - spec/fixtures/test_resource.rb
131
+ - spec/spec_helper.rb
132
+ - spec/webmachine/test/session_spec.rb