rspec-httpbin 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 5aa30b2e6b2d66d162419c7e4a3dc2da38d078511887b9ff526affc90f9d1900
4
+ data.tar.gz: c64e63606ab7fbb086c5a18d850bfa743c1f2c84043d03a31b9a15b81e506a4e
5
+ SHA512:
6
+ metadata.gz: ca2e3cd81a9ac2eb75d88d4d55c1afed0be78a99b014ba8748461a067c64d3a92eac48d35a0e324f2d785c8acb0c655fcd82ef597e8786bc3a0516adfbfcddfb
7
+ data.tar.gz: fe5cb8b921baeae60b1c9029a0e2929344952dc5f4ea699f4b17c4b84e8f94d6fced80b0919d87b44c81d7277ae5b9d9bdfcd40c8a3260bfde978f98f25d5e1d
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format Fuubar
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,18 @@
1
+ AllCops:
2
+ TargetRubyVersion: 3.2
3
+ NewCops: enable
4
+ RSpec/FilePath:
5
+ SpecSuffixOnly: true
6
+ require:
7
+ - rubocop-capybara
8
+ - rubocop-performance
9
+ - rubocop-rake
10
+ - rubocop-rspec
11
+ - rubocop-yard
12
+ - standard
13
+ - standard-custom
14
+ - standard-performance
15
+ inherit_gem:
16
+ standard: config/base.yml
17
+ standard-custom: config/base.yml
18
+ standard-performance: config/base.yml
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Manabu Niseki
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Manabu Niseki
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,56 @@
1
+ # rspec-httpbin
2
+
3
+ An [httpbin](https://github.com/postmanlabs/httpbin) like Rack app for RSpec.
4
+
5
+ > [!NOTE]
6
+ > This implementation is forked from [weapp/foxyrb](https://github.com/weapp/foxyrb/).
7
+
8
+ ## Install
9
+
10
+ ```bash
11
+ gem install rspec-httpbin
12
+ ```
13
+
14
+ ## Usage
15
+
16
+ `RSpec::HTTPBin` is a Rack app. You can mount it like:
17
+
18
+ ```rb
19
+ require 'capybara'
20
+
21
+ let!(:server) do
22
+ server = Capybara::Server.new(RSpec::HTTPBin)
23
+ server.boot
24
+ server
25
+ end
26
+ ```
27
+
28
+ And do a test:
29
+
30
+ ```rb
31
+ require 'http'
32
+
33
+ res = HTTP.get("#{server.base_url}/get")
34
+ json = JSON.parse(res.body.to_s)
35
+ expect(json['headers']['User-Agent']).to start_with('http.rb/')
36
+ ```
37
+
38
+ The following API endpoints are supported:
39
+
40
+ - `/get` (GET)
41
+ - `/post` (POST)
42
+ - `/put` (PUT)
43
+ - `/delete` (DELETE)
44
+ - `/patch` (PATCH)
45
+ - `/status/{status}` (any HTTP method)
46
+
47
+ `/get`, `/post`, `put`, `/delete` and `patch` return an HTTP response with the following fields.
48
+
49
+ - `data`: Request body.
50
+ - `files`: Not implemented. TBD.
51
+ - `form`: Form data.
52
+ - `json`: JSON data.
53
+ - `args`: Parsed query strings.
54
+ - `headers`: Headers.
55
+ - `origin`: Origin.
56
+ - `url`: URL.
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec]
data/lefthook.yml ADDED
@@ -0,0 +1,9 @@
1
+ pre-commit:
2
+ commands:
3
+ rubocop:
4
+ glob: "*.rb"
5
+ run: bundle exec rubocop --fix {staged_files}
6
+ stage_fixed: true
7
+ actionlint:
8
+ glob: ".github/workflows/*.yaml"
9
+ run: actionlint
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RSpec
4
+ class HTTPBin
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
@@ -0,0 +1,120 @@
1
+ require "forwardable"
2
+
3
+ require_relative "httpbin/version"
4
+
5
+ module RSpec
6
+ class HTTPBin
7
+ extend Forwardable
8
+
9
+ class << self
10
+ def call(env)
11
+ new(env).call
12
+ end
13
+ end
14
+
15
+ # @return [Rack::Request] Rack request
16
+ attr_reader :req
17
+
18
+ def initialize(env)
19
+ @req = Rack::Request.new(env)
20
+ end
21
+
22
+ def_delegators :req, :content_type, :form_data?, :get?, :post?, :delete?, :url, :query_string, :path, :path_info,
23
+ :patch?, :put?, :env
24
+
25
+ def call
26
+ case path_info
27
+ when "/get"
28
+ get? ? ok_response : error_405
29
+ when "/post"
30
+ post? ? ok_response : error_405
31
+ when "/delete"
32
+ delete? ? ok_response : error_405
33
+ when "/put"
34
+ put? ? ok_response : error_405
35
+ when "/patch"
36
+ patch? ? ok_response : error_405
37
+ when %r{^/status/[0-9]+$}
38
+ status = path_info.split("/").last
39
+ status_response status
40
+ else
41
+ error_404
42
+ end
43
+ end
44
+
45
+ def headers
46
+ http_headers = env.select { |k, _v| k.start_with?("HTTP_") }
47
+ http_headers.transform_keys do |k|
48
+ k.sub(/^HTTP_/, "").downcase.gsub(/(^|_)\w/, &:upcase).tr("_", "-")
49
+ end
50
+ end
51
+
52
+ def json?
53
+ content_type == "application/json"
54
+ end
55
+
56
+ def origin
57
+ env["REMOTE_ADDR"]
58
+ end
59
+
60
+ def body
61
+ @body ||= [].tap do |out|
62
+ req.body.rewind
63
+ out << req.body.read
64
+ req.body.rewind
65
+ end.first || ""
66
+ end
67
+
68
+ def files
69
+ return nil unless post? && Rack::Multipart::MULTIPART.match?(content_type)
70
+
71
+ @files ||= [].tap do |out|
72
+ multipart = Rack::Multipart.parse_multipart(env)
73
+ out << multipart.to_h.map do |k, v|
74
+ [k, v[:tempfile].read]
75
+ end.to_h
76
+ rescue
77
+ # do nothing
78
+ end.first
79
+ end
80
+
81
+ def form
82
+ Rack::Utils.parse_nested_query(body) if form_data?
83
+ end
84
+
85
+ def json
86
+ JSON.parse(body) if json?
87
+ end
88
+
89
+ def data
90
+ return "" if form_data? || !files.nil?
91
+
92
+ body
93
+ end
94
+
95
+ def body_payload
96
+ return {} if body.empty?
97
+
98
+ {data:, files:, form:, json:}
99
+ end
100
+
101
+ def ok_response
102
+ payload = body_payload.merge(args: query_string, headers:, origin:, url:)
103
+
104
+ ["200", {"Content-Type" => "application/json"}, [JSON.generate(payload)]]
105
+ ["200", {"Content-Type" => "text/plain"}, [JSON.generate(payload)]]
106
+ end
107
+
108
+ def status_response(status)
109
+ [status, {"Content-Type" => "text/plain"}, [JSON.generate({})]]
110
+ end
111
+
112
+ def error_404
113
+ ["404", {"Content-Type" => "application/json"}, [JSON.generate({})]]
114
+ end
115
+
116
+ def error_405
117
+ ["405", {"Content-Type" => "application/json"}, [JSON.generate({})]]
118
+ end
119
+ end
120
+ end
metadata ADDED
@@ -0,0 +1,278 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rspec-httpbin
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Manabu Niseki
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-06-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: capybara
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.40'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.40'
41
+ - !ruby/object:Gem::Dependency
42
+ name: fuubar
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 2.5.1
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 2.5.1
55
+ - !ruby/object:Gem::Dependency
56
+ name: http
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 5.2.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 5.2.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: puma
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 6.4.2
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 6.4.2
83
+ - !ruby/object:Gem::Dependency
84
+ name: rack
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 3.0.11
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 3.0.11
97
+ - !ruby/object:Gem::Dependency
98
+ name: rack-test
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 2.1.0
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 2.1.0
111
+ - !ruby/object:Gem::Dependency
112
+ name: rackup
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: 2.1.0
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: 2.1.0
125
+ - !ruby/object:Gem::Dependency
126
+ name: rake
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: 13.2.1
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: 13.2.1
139
+ - !ruby/object:Gem::Dependency
140
+ name: rspec
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: '3.13'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: '3.13'
153
+ - !ruby/object:Gem::Dependency
154
+ name: rspec-parameterized
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - "~>"
158
+ - !ruby/object:Gem::Version
159
+ version: 1.0.2
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - "~>"
165
+ - !ruby/object:Gem::Version
166
+ version: 1.0.2
167
+ - !ruby/object:Gem::Dependency
168
+ name: rubocop-rake
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - "~>"
172
+ - !ruby/object:Gem::Version
173
+ version: '0.6'
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - "~>"
179
+ - !ruby/object:Gem::Version
180
+ version: '0.6'
181
+ - !ruby/object:Gem::Dependency
182
+ name: rubocop-rspec
183
+ requirement: !ruby/object:Gem::Requirement
184
+ requirements:
185
+ - - "~>"
186
+ - !ruby/object:Gem::Version
187
+ version: 2.31.0
188
+ type: :development
189
+ prerelease: false
190
+ version_requirements: !ruby/object:Gem::Requirement
191
+ requirements:
192
+ - - "~>"
193
+ - !ruby/object:Gem::Version
194
+ version: 2.31.0
195
+ - !ruby/object:Gem::Dependency
196
+ name: rubocop-yard
197
+ requirement: !ruby/object:Gem::Requirement
198
+ requirements:
199
+ - - "~>"
200
+ - !ruby/object:Gem::Version
201
+ version: 0.9.3
202
+ type: :development
203
+ prerelease: false
204
+ version_requirements: !ruby/object:Gem::Requirement
205
+ requirements:
206
+ - - "~>"
207
+ - !ruby/object:Gem::Version
208
+ version: 0.9.3
209
+ - !ruby/object:Gem::Dependency
210
+ name: standard
211
+ requirement: !ruby/object:Gem::Requirement
212
+ requirements:
213
+ - - "~>"
214
+ - !ruby/object:Gem::Version
215
+ version: 1.36.0
216
+ type: :development
217
+ prerelease: false
218
+ version_requirements: !ruby/object:Gem::Requirement
219
+ requirements:
220
+ - - "~>"
221
+ - !ruby/object:Gem::Version
222
+ version: 1.36.0
223
+ - !ruby/object:Gem::Dependency
224
+ name: lefthook
225
+ requirement: !ruby/object:Gem::Requirement
226
+ requirements:
227
+ - - "~>"
228
+ - !ruby/object:Gem::Version
229
+ version: '1.6'
230
+ type: :development
231
+ prerelease: false
232
+ version_requirements: !ruby/object:Gem::Requirement
233
+ requirements:
234
+ - - "~>"
235
+ - !ruby/object:Gem::Version
236
+ version: '1.6'
237
+ description: An httpbin like Rack module for Rspec
238
+ email:
239
+ - manabu.niseki@gmail.com
240
+ executables: []
241
+ extensions: []
242
+ extra_rdoc_files: []
243
+ files:
244
+ - ".rspec"
245
+ - ".rubocop.yml"
246
+ - LICENSE
247
+ - LICENSE.txt
248
+ - README.md
249
+ - Rakefile
250
+ - lefthook.yml
251
+ - lib/rspec/httpbin.rb
252
+ - lib/rspec/httpbin/version.rb
253
+ homepage: https://github.com/ninoseki/rspec-httpbin
254
+ licenses:
255
+ - MIT
256
+ metadata:
257
+ homepage_uri: https://github.com/ninoseki/rspec-httpbin
258
+ source_code_uri: https://github.com/ninoseki/rspec-httpbin
259
+ post_install_message:
260
+ rdoc_options: []
261
+ require_paths:
262
+ - lib
263
+ required_ruby_version: !ruby/object:Gem::Requirement
264
+ requirements:
265
+ - - ">="
266
+ - !ruby/object:Gem::Version
267
+ version: 3.0.0
268
+ required_rubygems_version: !ruby/object:Gem::Requirement
269
+ requirements:
270
+ - - ">="
271
+ - !ruby/object:Gem::Version
272
+ version: '0'
273
+ requirements: []
274
+ rubygems_version: 3.5.9
275
+ signing_key:
276
+ specification_version: 4
277
+ summary: An httpbin like Rack module for RSpec
278
+ test_files: []