rack-test_app 1.0.0

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.
@@ -0,0 +1,168 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ ###
4
+ ### $Release: 1.0.0 $
5
+ ### $Copyright: copyright(c) 2015 kuwata-lab.com all rights reserved $
6
+ ### $License: MIT License $
7
+ ###
8
+
9
+
10
+ require_relative '../../test_helper'
11
+
12
+
13
+ describe Rack::TestApp::Util do
14
+
15
+
16
+ describe '.#percent_encode()' do
17
+
18
+ it "[!a96jo] encodes string into percent encoding format." do
19
+ expected = "%5Bxxx%5D"
20
+ assert_equal expected, Rack::TestApp::Util.percent_encode('[xxx]')
21
+ end
22
+
23
+ end
24
+
25
+
26
+ describe '.#percent_decode()' do
27
+
28
+ it "[!kl9sk] decodes percent encoded string." do
29
+ expected = "[xxx]"
30
+ assert_equal expected, Rack::TestApp::Util.percent_decode('%5Bxxx%5D')
31
+ end
32
+
33
+ end
34
+
35
+
36
+ describe '.#build_query_string()' do
37
+
38
+ it "[!098ac] returns nil when argument is nil." do
39
+ expected = nil
40
+ assert_equal expected, Rack::TestApp::Util.build_query_string(nil)
41
+ end
42
+
43
+ it "[!z9ds2] returns argument itself when it is a string." do
44
+ query = "a=10&b=20"
45
+ expected = query
46
+ assert_equal expected, Rack::TestApp::Util.build_query_string(query)
47
+ end
48
+
49
+ it "[!m5yyh] returns query string when Hash or Array passed." do
50
+ query = {a: 10, b: 20}
51
+ expected = "a=10&b=20"
52
+ assert_equal expected, Rack::TestApp::Util.build_query_string(query)
53
+ #
54
+ query = [[:a, 10], ["b", 20]]
55
+ expected = "a=10&b=20"
56
+ assert_equal expected, Rack::TestApp::Util.build_query_string(query)
57
+ end
58
+
59
+ it "[!nksh3] raises ArgumentError when passed value except nil, string, hash or array." do
60
+ ex = assert_raises ArgumentError do
61
+ Rack::TestApp::Util.build_query_string(123)
62
+ end
63
+ assert_equal ex.message, "Hash or Array expected but got 123."
64
+ end
65
+
66
+ end
67
+
68
+
69
+ describe '.#parse_set_cookie()' do
70
+
71
+ it "[!hvvu4] parses 'Set-Cookie' header value and returns hash object." do
72
+ set_cookie = "name1=value1; Domain=localhost; Path=/; Expires=Monday, 15-Aug-2005 15:52:01 UTC; Max-Age=3600; Secure; HttpOnly"
73
+ expected = {
74
+ :name => "name1",
75
+ :value => "value1",
76
+ :domain => "localhost",
77
+ :path => "/",
78
+ :expires => "Monday, 15-Aug-2005 15:52:01 UTC",
79
+ :max_age => 3600,
80
+ :secure => true,
81
+ :httponly => true,
82
+ }
83
+ actual = Rack::TestApp::Util.parse_set_cookie(set_cookie)
84
+ assert_equal expected, actual
85
+ end
86
+
87
+ it "[!q1h29] sets true as value for Secure or HttpOnly attribute." do
88
+ set_cookie = "name1=value1; secure; httponly"
89
+ expected = {
90
+ :name => "name1",
91
+ :value => "value1",
92
+ :secure => true,
93
+ :httponly => true,
94
+ }
95
+ actual = Rack::TestApp::Util.parse_set_cookie(set_cookie)
96
+ assert_equal expected, actual
97
+ end
98
+
99
+ it "[!50iko] raises error when attribute value specified for Secure or HttpOnly attirbute." do
100
+ set_cookie = "name1=value1; Secure=1"
101
+ ex = assert_raises(TypeError) { Rack::TestApp::Util.parse_set_cookie(set_cookie) }
102
+ assert_equal "Secure=1: unexpected attribute value.", ex.message
103
+ #
104
+ set_cookie = "name1=value1; HttpOnly=1"
105
+ ex = assert_raises(TypeError) { Rack::TestApp::Util.parse_set_cookie(set_cookie) }
106
+ assert_equal "HttpOnly=1: unexpected attribute value.", ex.message
107
+ end
108
+
109
+ it "[!sucrm] raises error when attribute value is missing when neighter Secure nor HttpOnly." do
110
+ set_cookie = "name1=value1; Path"
111
+ ex = assert_raises(TypeError) { Rack::TestApp::Util.parse_set_cookie(set_cookie) }
112
+ assert_equal "Path: attribute value expected but not specified.", ex.message
113
+ end
114
+
115
+ it "[!f3rk7] converts string into integer for Max-Age attribute." do
116
+ set_cookie = "name1=value1; Max-Age=3600"
117
+ c = Rack::TestApp::Util.parse_set_cookie(set_cookie)
118
+ assert_equal 3600, c[:max_age]
119
+ assert_kind_of Fixnum, c[:max_age]
120
+ end
121
+
122
+ it "[!wgzyz] raises error when Max-Age attribute value is not a positive integer." do
123
+ set_cookie = "name1=value1; Max-Age=30sec"
124
+ ex = assert_raises(TypeError) { Rack::TestApp::Util.parse_set_cookie(set_cookie) }
125
+ assert_equal "Max-Age=30sec: positive integer expected.", ex.message
126
+ end
127
+
128
+ it "[!8xg63] raises ArgumentError when unknown attribute exists." do
129
+ set_cookie = "name1=value1; MaxAge=3600"
130
+ ex = assert_raises(TypeError) { Rack::TestApp::Util.parse_set_cookie(set_cookie) }
131
+ assert_equal "MaxAge=3600: unknown cookie attribute.", ex.message
132
+ end
133
+
134
+ end
135
+
136
+
137
+ describe '.#randstr_b64()' do
138
+
139
+ it "[!yq0gv] returns random string, encoded with urlsafe base64." do
140
+ arr = (1..1000).map { Rack::TestApp::Util.randstr_b64() }
141
+ assert_equal 1000, arr.sort.uniq.length
142
+ arr.each do |s|
143
+ assert_match /\A[-\w]+\z/, s
144
+ assert_equal 27, s.length
145
+ end
146
+ end
147
+
148
+ end
149
+
150
+
151
+ describe '.#guess_content_type()' do
152
+
153
+ it "[!xw0js] returns content type guessed from filename." do
154
+ assert_equal "text/html" , Rack::TestApp::Util.guess_content_type("foo.html")
155
+ assert_equal "image/jpeg" , Rack::TestApp::Util.guess_content_type("foo.jpg")
156
+ assert_equal "application/json", Rack::TestApp::Util.guess_content_type("foo.json")
157
+ assert_equal "application/vnd.ms-excel", Rack::TestApp::Util.guess_content_type("foo.xls")
158
+ end
159
+
160
+ it "[!dku5c] returns 'application/octet-stream' when failed to guess content type." do
161
+ assert_equal "application/octet-stream", Rack::TestApp::Util.guess_content_type("foo.rbc")
162
+ assert_equal "application/octet-stream", Rack::TestApp::Util.guess_content_type("foo")
163
+ end
164
+
165
+ end
166
+
167
+
168
+ end
@@ -0,0 +1,182 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ ###
4
+ ### $Release: 1.0.0 $
5
+ ### $Copyright: copyright(c) 2015 kuwata-lab.com all rights reserved $
6
+ ### $License: MIT License $
7
+ ###
8
+
9
+
10
+ require_relative '../../test_helper'
11
+
12
+
13
+ describe Rack::TestApp::Wrapper do
14
+
15
+ app = proc {|env|
16
+ text = "#{env['REQUEST_METHOD']} #{env['PATH_INFO']}"
17
+ text << "?#{env['QUERY_STRING']}" unless env['QUERY_STRING'].to_s.empty?
18
+ text = "" if env['REQUEST_METHOD'] == 'HEAD'
19
+ [200, {"Content-Type"=>"text/plain;charset=utf-8"}, [text]]
20
+ }
21
+ app = Rack::Lint.new(app)
22
+
23
+
24
+ describe '#initialize()' do
25
+
26
+ it "[!zz9yg] takes app and optional env objects." do
27
+ env = {"HTTPS"=>"on"}
28
+ wrapper = Rack::TestApp::Wrapper.new(app, env)
29
+ assert_equal app, wrapper.instance_variable_get('@app')
30
+ assert_equal env, wrapper.instance_variable_get('@env')
31
+ #
32
+ wrapper = Rack::TestApp::Wrapper.new(app)
33
+ assert_equal app, wrapper.instance_variable_get('@app')
34
+ assert_equal nil, wrapper.instance_variable_get('@env')
35
+ end
36
+
37
+ end
38
+
39
+
40
+ describe '#with()' do
41
+
42
+ it "[!0bk12] returns new wrapper object, keeping cookies and headers." do
43
+ wrapper = Rack::TestApp::Wrapper.new(app)
44
+ cookies = {'k1'=>{name: 'k1', value: 'v1'}}
45
+ headers = {'X-Requested-With'=>'XMLHttpRequest'}
46
+ new_wrapper = wrapper.with(cookies: cookies, headers: headers)
47
+ assert_kind_of Rack::TestApp::Wrapper, new_wrapper
48
+ expected = {"HTTP_X_REQUESTED_WITH"=>"XMLHttpRequest", "HTTP_COOKIE"=>"k1=v1"}
49
+ assert_equal expected, new_wrapper.instance_variable_get('@env')
50
+ end
51
+
52
+ it "[!mkdbu] yields with new wrapper object if block given." do
53
+ wrapper = Rack::TestApp::Wrapper.new(app)
54
+ cookies = {'k1'=>{name: 'k1', value: 'v1'}}
55
+ headers = {'X-Requested-With'=>'XMLHttpRequest'}
56
+ wrapper.with(cookies: cookies, headers: headers) do |new_wrapper|
57
+ assert_kind_of Rack::TestApp::Wrapper, new_wrapper
58
+ expected = {"HTTP_X_REQUESTED_WITH"=>"XMLHttpRequest", "HTTP_COOKIE"=>"k1=v1"}
59
+ assert_equal expected, new_wrapper.instance_variable_get('@env')
60
+ end
61
+ end
62
+
63
+ end
64
+
65
+
66
+ describe '#request()' do
67
+
68
+ it "[!eb153] returns Rack::TestApp::Result object." do
69
+ wrapper = Rack::TestApp::Wrapper.new(app)
70
+ r = wrapper.request(:POST, '/hello')
71
+ assert_kind_of Rack::TestApp::Result, r
72
+ end
73
+
74
+ it "[!4xpwa] creates env object and calls app with it." do
75
+ wrapper = Rack::TestApp::Wrapper.new(app)
76
+ wrapper.request(:POST, '/hello')
77
+ assert_equal 'POST', wrapper.last_env['REQUEST_METHOD']
78
+ assert_equal '/hello', wrapper.last_env['PATH_INFO']
79
+ end
80
+
81
+ it "[!r6sod] merges @env if passed for initializer." do
82
+ wrapper = Rack::TestApp::Wrapper.new(app, {"HTTPS"=>"on"})
83
+ wrapper.request(:POST, '/hello')
84
+ assert_equal 'on', wrapper.last_env['HTTPS']
85
+ #
86
+ wrapper = Rack::TestApp::Wrapper.new(app)
87
+ wrapper.request(:POST, '/hello')
88
+ assert_equal 'off', wrapper.last_env['HTTPS']
89
+ end
90
+
91
+ end
92
+
93
+
94
+ describe '#GET()' do
95
+
96
+ it "requests with GET method." do
97
+ wrapper = Rack::TestApp::Wrapper.new(app)
98
+ r = wrapper.GET('/api/foo', query: {"id"=>123})
99
+ assert_equal "GET /api/foo?id=123", r.body_text
100
+ end
101
+
102
+ end
103
+
104
+
105
+ describe '#POST()' do
106
+
107
+ it "requests with POST method." do
108
+ wrapper = Rack::TestApp::Wrapper.new(app)
109
+ r = wrapper.POST('/api/foo', form: {"id"=>123})
110
+ assert_equal "POST /api/foo", r.body_text
111
+ end
112
+
113
+ end
114
+
115
+
116
+ describe '#PUT()' do
117
+
118
+ it "requests with PUT method." do
119
+ wrapper = Rack::TestApp::Wrapper.new(app)
120
+ r = wrapper.PUT('/api/foo', form: {"id"=>123})
121
+ assert_equal "PUT /api/foo", r.body_text
122
+ end
123
+
124
+ end
125
+
126
+
127
+ describe '#DELETE()' do
128
+
129
+ it "requests with DELETE method." do
130
+ wrapper = Rack::TestApp::Wrapper.new(app)
131
+ r = wrapper.DELETE('/api/foo', form: {"id"=>123})
132
+ assert_equal "DELETE /api/foo", r.body_text
133
+ end
134
+
135
+ end
136
+
137
+
138
+ describe '#HEAD()' do
139
+
140
+ it "requests with HEAD method." do
141
+ wrapper = Rack::TestApp::Wrapper.new(app)
142
+ r = wrapper.HEAD('/api/foo', query: {"id"=>123})
143
+ assert_equal "", r.body_text
144
+ end
145
+
146
+ end
147
+
148
+
149
+ describe '#PATCH()' do
150
+
151
+ it "requests with PATCH method." do
152
+ wrapper = Rack::TestApp::Wrapper.new(app)
153
+ r = wrapper.PATCH('/api/foo', form: {"id"=>123})
154
+ assert_equal "PATCH /api/foo", r.body_text
155
+ end
156
+
157
+ end
158
+
159
+
160
+ describe '#OPTIONS()' do
161
+
162
+ it "requests with OPTIONS method." do
163
+ wrapper = Rack::TestApp::Wrapper.new(app)
164
+ r = wrapper.OPTIONS('/api/foo', form: {"id"=>123})
165
+ assert_equal "OPTIONS /api/foo", r.body_text
166
+ end
167
+
168
+ end
169
+
170
+
171
+ describe '#TRACE()' do
172
+
173
+ it "requests with TRACE method." do
174
+ wrapper = Rack::TestApp::Wrapper.new(app)
175
+ r = wrapper.TRACE('/api/foo', form: {"id"=>123})
176
+ assert_equal "TRACE /api/foo", r.body_text
177
+ end
178
+
179
+ end
180
+
181
+
182
+ end
@@ -0,0 +1,4 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'rack/test_app'
3
+
4
+ require 'minitest/autorun'
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-test_app
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - makoto kuwata
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-11-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rack
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.10'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.10'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
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: |
70
+ Rack::TestApp is another testing helper library for Rack application.
71
+ IMO it is more intuitive than Rack::Test.
72
+ email:
73
+ - kwa@kuwata-lab.com
74
+ executables: []
75
+ extensions: []
76
+ extra_rdoc_files: []
77
+ files:
78
+ - MIT-LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - lib/rack/test_app.rb
82
+ - rack-test_app.gemspec
83
+ - test/rack/test_app/MultipartBuilder_test.rb
84
+ - test/rack/test_app/Result_test.rb
85
+ - test/rack/test_app/TestApp_test.rb
86
+ - test/rack/test_app/Util_test.rb
87
+ - test/rack/test_app/Wrapper_test.rb
88
+ - test/rack/test_app/data/example1.jpg
89
+ - test/rack/test_app/data/example1.png
90
+ - test/rack/test_app/data/multipart.form
91
+ - test/test_helper.rb
92
+ homepage: https://github.com/kwatch/rack-test_app
93
+ licenses:
94
+ - MIT-LICENCE
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: '2.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.4.5.1
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: more intuitive testing helper library for Rack app
116
+ test_files: []