sus-fixtures-protocol-http 0.1.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d949cf6744d4f2f0f4f40942ac2abef8eb0f9f214811fec0040501d96935a996
4
+ data.tar.gz: 1d51fd8ec7962c97e9dded72c281638d2fab75855b00b76b11cd32a01e4c0298
5
+ SHA512:
6
+ metadata.gz: a2d2c0a74bb8e3dcfedd8a86e67e04f25d2ebe96fd463cc12184f6afa825a86d63c96683adbd191e572e7f6ee8290b9e6b9818e52fedf8e37addf9cdaffcfb2e
7
+ data.tar.gz: f8045f506c51aaa62500c097ffca02f86ac0ac1b96eef81ed6397e4d563dca06c2383ee509e479b628c972b714e406bd34ac56e722f88cb149fa4a0c6f30da45
checksums.yaml.gz.sig ADDED
Binary file
@@ -0,0 +1,226 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Released under the MIT License.
4
+ # Copyright, 2026, by Samuel Williams.
5
+
6
+ require "protocol/http/cookie"
7
+ require "protocol/http/middleware"
8
+ require "protocol/http/request"
9
+ require "uri"
10
+
11
+ module Sus
12
+ module Fixtures
13
+ module Protocol
14
+ module HTTP
15
+ # An in-process client for exercising protocol HTTP middleware.
16
+ class Client < ::Protocol::HTTP::Middleware
17
+ # Initialize the client.
18
+ #
19
+ # @parameter delegate [::Protocol::HTTP::Middleware] The middleware to exercise.
20
+ # @parameter scheme [String] The default request scheme.
21
+ # @parameter authority [String] The default request authority.
22
+ def initialize(delegate, scheme: "http", authority: "localhost")
23
+ super(delegate)
24
+
25
+ @scheme = scheme
26
+ @authority = authority
27
+
28
+ @headers = {}
29
+ @cookies = {}
30
+ @closed = false
31
+ @exchange_closed = true
32
+ end
33
+
34
+ # @attribute [String] The default request scheme.
35
+ attr :scheme
36
+
37
+ # @attribute [String] The default request authority.
38
+ attr :authority
39
+
40
+ # @attribute [Hash(String, String)] Headers added to every request.
41
+ attr :headers
42
+
43
+ # @attribute [Hash(String, String)] Cookies retained between requests.
44
+ attr :cookies
45
+
46
+ # @attribute [::Protocol::HTTP::Request | Nil] The most recent request.
47
+ attr :last_request
48
+
49
+ # @attribute [::Protocol::HTTP::Response | Nil] The most recent response.
50
+ attr :last_response
51
+
52
+ # Set a default request header.
53
+ #
54
+ # @parameter name [String] The header name.
55
+ # @parameter value [String] The header value.
56
+ def header(name, value)
57
+ @headers[name.downcase] = value
58
+ end
59
+
60
+ # Store a cookie for subsequent requests.
61
+ #
62
+ # @parameter value [String | ::Protocol::HTTP::Cookie] The cookie to store.
63
+ def set_cookie(value)
64
+ case value
65
+ when String
66
+ cookie = ::Protocol::HTTP::Cookie.parse(value)
67
+ when ::Protocol::HTTP::Cookie
68
+ cookie = value
69
+ else
70
+ raise ArgumentError, "Unsupported cookie: #{value.inspect}"
71
+ end
72
+
73
+ if cookie.value
74
+ @cookies[cookie.name] = cookie.value
75
+ else
76
+ @cookies.delete(cookie.name)
77
+ end
78
+
79
+ return cookie
80
+ end
81
+
82
+ # Construct and perform a request.
83
+ #
84
+ # @parameter method [String] The request method.
85
+ # @parameter path [String] The request path.
86
+ # @parameter headers [Hash | ::Protocol::HTTP::Headers | Nil] The request headers.
87
+ # @parameter body [String | Array(String) | ::Protocol::HTTP::Body::Readable | Nil] The request body.
88
+ # @parameter options [Hash] Additional options for {::Protocol::HTTP::Request.[]}.
89
+ # @returns [::Protocol::HTTP::Response] The application response.
90
+ def request(method, path, headers = nil, body = nil, **options)
91
+ return self.call(
92
+ ::Protocol::HTTP::Request[method, path, headers, body, **options]
93
+ )
94
+ end
95
+
96
+ # Perform a prepared request against the application.
97
+ #
98
+ # @parameter request [::Protocol::HTTP::Request] The prepared request.
99
+ # @returns [::Protocol::HTTP::Response] The application response.
100
+ def call(request)
101
+ if @closed
102
+ raise IOError, "Client is closed!"
103
+ end
104
+
105
+ self.close_exchange
106
+ self.prepare_request(request)
107
+
108
+ @last_request = request
109
+ @last_response = nil
110
+ @exchange_closed = false
111
+
112
+ begin
113
+ @last_response = super(request)
114
+ self.store_cookies(@last_response.headers["set-cookie"])
115
+
116
+ return @last_response
117
+ rescue => error
118
+ self.close_exchange(error)
119
+ raise
120
+ end
121
+ end
122
+
123
+ # Follow the location in the most recent redirect response.
124
+ #
125
+ # Statuses 307 and 308 preserve the original method and body. Other redirects use `GET`, except that `HEAD` remains `HEAD`.
126
+ #
127
+ # @returns [::Protocol::HTTP::Response] The redirected response.
128
+ def follow_redirect!
129
+ response = @last_response
130
+ request = @last_request
131
+
132
+ unless response&.redirection?
133
+ raise RuntimeError, "The last response is not a redirect!"
134
+ end
135
+
136
+ location = response.headers["location"]
137
+ unless location
138
+ raise RuntimeError, "The redirect response has no location!"
139
+ end
140
+
141
+ base = "#{request.scheme}://#{request.authority}#{request.path}"
142
+ target = ::URI.join(base, location.to_s)
143
+
144
+ if response.preserve_method?
145
+ if @request_had_body && !@replay_body
146
+ raise IOError, "The request body cannot be replayed!"
147
+ end
148
+
149
+ method = request.method
150
+ body = @replay_body
151
+ elsif request.head?
152
+ method = ::Protocol::HTTP::Methods::HEAD
153
+ body = nil
154
+ else
155
+ method = ::Protocol::HTTP::Methods::GET
156
+ body = nil
157
+ end
158
+
159
+ return self.request(
160
+ method,
161
+ target.request_uri,
162
+ nil,
163
+ body,
164
+ scheme: target.scheme,
165
+ authority: target.authority,
166
+ )
167
+ end
168
+
169
+ # Close the current exchange and application.
170
+ #
171
+ # @parameter error [Exception | Nil] The error which caused the client to close.
172
+ def close(error = nil)
173
+ return if @closed
174
+
175
+ @closed = true
176
+
177
+ begin
178
+ self.close_exchange(error)
179
+ ensure
180
+ super()
181
+ end
182
+ end
183
+
184
+ private
185
+
186
+ def prepare_request(request)
187
+ request.scheme ||= @scheme
188
+ request.authority ||= @authority
189
+
190
+ @headers.each do |name, value|
191
+ unless request.headers.include?(name)
192
+ request.headers[name] = value
193
+ end
194
+ end
195
+
196
+ if !@cookies.empty? && !request.headers.include?("cookie")
197
+ request.headers["cookie"] = @cookies.map{|name, value| "#{name}=#{value}"}
198
+ end
199
+
200
+ @request_had_body = request.body?
201
+ @replay_body = request.body&.buffered
202
+ end
203
+
204
+ def store_cookies(values)
205
+ return unless values
206
+
207
+ values.to_h.each_value do |cookie|
208
+ self.set_cookie(cookie)
209
+ end
210
+ end
211
+
212
+ def close_exchange(error = nil)
213
+ return if @exchange_closed
214
+
215
+ begin
216
+ @last_response&.close(error)
217
+ ensure
218
+ @last_request&.close(error)
219
+ @exchange_closed = true
220
+ end
221
+ end
222
+ end
223
+ end
224
+ end
225
+ end
226
+ end
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Released under the MIT License.
4
+ # Copyright, 2026, by Samuel Williams.
5
+
6
+ require "protocol/http/middleware"
7
+ require_relative "client"
8
+
9
+ module Sus
10
+ module Fixtures
11
+ module Protocol
12
+ module HTTP
13
+ # A test context for exercising protocol HTTP middleware in-process.
14
+ module MiddlewareContext
15
+ # The middleware under test.
16
+ #
17
+ # @returns [::Protocol::HTTP::Middleware] The middleware.
18
+ def middleware
19
+ ::Protocol::HTTP::Middleware::HelloWorld
20
+ end
21
+
22
+ # The in-process client for the application.
23
+ #
24
+ # @returns [Client] The client.
25
+ def client
26
+ @client ||= Client.new(middleware)
27
+ end
28
+
29
+ # The most recent request.
30
+ #
31
+ # @returns [::Protocol::HTTP::Request | Nil] The request.
32
+ def last_request
33
+ client.last_request
34
+ end
35
+
36
+ # The most recent response.
37
+ #
38
+ # @returns [::Protocol::HTTP::Response | Nil] The response.
39
+ def last_response
40
+ client.last_response
41
+ end
42
+
43
+ # Close the client after each test.
44
+ #
45
+ # @parameter error [Exception | Nil] The error raised by the test, if any.
46
+ def after(error = nil)
47
+ begin
48
+ @client&.close(error)
49
+ ensure
50
+ super
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Released under the MIT License.
4
+ # Copyright, 2026, by Samuel Williams.
5
+
6
+ # @namespace
7
+ module Sus
8
+ # @namespace
9
+ module Fixtures
10
+ # @namespace
11
+ module Protocol
12
+ # @namespace
13
+ module HTTP
14
+ VERSION = "0.1.0"
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Released under the MIT License.
4
+ # Copyright, 2026, by Samuel Williams.
5
+
6
+ require_relative "http/version"
7
+ require_relative "http/client"
8
+ require_relative "http/middleware_context"
data/license.md ADDED
@@ -0,0 +1,21 @@
1
+ # MIT License
2
+
3
+ Copyright, 2026, by Samuel Williams.
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/readme.md ADDED
@@ -0,0 +1,53 @@
1
+ # Sus::Fixtures::Protocol::HTTP
2
+
3
+ Provides transport-free test fixtures for <code class="language-ruby">Protocol::HTTP::Middleware</code> applications.
4
+
5
+ [![Development Status](https://github.com/socketry/sus-fixtures-protocol-http/workflows/Test/badge.svg)](https://github.com/socketry/sus-fixtures-protocol-http/actions?workflow=Test)
6
+
7
+ ## Usage
8
+
9
+ Please see the [project documentation](https://socketry.github.io/sus-fixtures-protocol-http/) for more details.
10
+
11
+ - [Getting Started](https://socketry.github.io/sus-fixtures-protocol-http/guides/getting-started/index) - This guide explains how to exercise <code class="language-ruby">Protocol::HTTP::Middleware</code> directly, without starting a server.
12
+
13
+ ## Releases
14
+
15
+ Please see the [project releases](https://socketry.github.io/sus-fixtures-protocol-http/releases/index) for all releases.
16
+
17
+ ### v0.1.0
18
+
19
+ - Introduce an in-process client and middleware context for protocol HTTP middleware.
20
+
21
+ ## Contributing
22
+
23
+ We welcome contributions to this project.
24
+
25
+ 1. Fork the repository.
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`).
27
+ 3. Commit your changes (`git commit -am 'Add some feature.'`).
28
+ 4. Push to the branch (`git push origin my-new-feature`).
29
+ 5. Create a new pull request.
30
+
31
+ ### Running Tests
32
+
33
+ To run the test suite:
34
+
35
+ ``` shell
36
+ bundle exec sus
37
+ ```
38
+
39
+ ### Making Releases
40
+
41
+ To make a new release:
42
+
43
+ ``` shell
44
+ bundle exec bake gem:release:patch # or minor or major
45
+ ```
46
+
47
+ ### Developer Certificate of Origin
48
+
49
+ In order to protect users of this project, we require all contributors to comply with the [Developer Certificate of Origin](https://developercertificate.org/). This ensures that all contributions are properly licensed and attributed.
50
+
51
+ ### Community Guidelines
52
+
53
+ This project is best served by a collaborative and respectful environment. Treat each other professionally, respect differing viewpoints, and engage constructively. Harassment, discrimination, or harmful behavior is not tolerated. Communicate clearly, listen actively, and support one another. If any issues arise, please inform the project maintainers.
data/releases.md ADDED
@@ -0,0 +1,5 @@
1
+ # Releases
2
+
3
+ ## v0.1.0
4
+
5
+ - Introduce an in-process client and middleware context for protocol HTTP middleware.
data.tar.gz.sig ADDED
Binary file
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sus-fixtures-protocol-http
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Samuel Williams
8
+ bindir: bin
9
+ cert_chain:
10
+ - |
11
+ -----BEGIN CERTIFICATE-----
12
+ MIIE2DCCA0CgAwIBAgIBATANBgkqhkiG9w0BAQsFADBhMRgwFgYDVQQDDA9zYW11
13
+ ZWwud2lsbGlhbXMxHTAbBgoJkiaJk/IsZAEZFg1vcmlvbnRyYW5zZmVyMRIwEAYK
14
+ CZImiZPyLGQBGRYCY28xEjAQBgoJkiaJk/IsZAEZFgJuejAeFw0yMjA4MDYwNDUz
15
+ MjRaFw0zMjA4MDMwNDUzMjRaMGExGDAWBgNVBAMMD3NhbXVlbC53aWxsaWFtczEd
16
+ MBsGCgmSJomT8ixkARkWDW9yaW9udHJhbnNmZXIxEjAQBgoJkiaJk/IsZAEZFgJj
17
+ bzESMBAGCgmSJomT8ixkARkWAm56MIIBojANBgkqhkiG9w0BAQEFAAOCAY8AMIIB
18
+ igKCAYEAomvSopQXQ24+9DBB6I6jxRI2auu3VVb4nOjmmHq7XWM4u3HL+pni63X2
19
+ 9qZdoq9xt7H+RPbwL28LDpDNflYQXoOhoVhQ37Pjn9YDjl8/4/9xa9+NUpl9XDIW
20
+ sGkaOY0eqsQm1pEWkHJr3zn/fxoKPZPfaJOglovdxf7dgsHz67Xgd/ka+Wo1YqoE
21
+ e5AUKRwUuvaUaumAKgPH+4E4oiLXI4T1Ff5Q7xxv6yXvHuYtlMHhYfgNn8iiW8WN
22
+ XibYXPNP7NtieSQqwR/xM6IRSoyXKuS+ZNGDPUUGk8RoiV/xvVN4LrVm9upSc0ss
23
+ RZ6qwOQmXCo/lLcDUxJAgG95cPw//sI00tZan75VgsGzSWAOdjQpFM0l4dxvKwHn
24
+ tUeT3ZsAgt0JnGqNm2Bkz81kG4A2hSyFZTFA8vZGhp+hz+8Q573tAR89y9YJBdYM
25
+ zp0FM4zwMNEUwgfRzv1tEVVUEXmoFCyhzonUUw4nE4CFu/sE3ffhjKcXcY//qiSW
26
+ xm4erY3XAgMBAAGjgZowgZcwCQYDVR0TBAIwADALBgNVHQ8EBAMCBLAwHQYDVR0O
27
+ BBYEFO9t7XWuFf2SKLmuijgqR4sGDlRsMC4GA1UdEQQnMCWBI3NhbXVlbC53aWxs
28
+ aWFtc0BvcmlvbnRyYW5zZmVyLmNvLm56MC4GA1UdEgQnMCWBI3NhbXVlbC53aWxs
29
+ aWFtc0BvcmlvbnRyYW5zZmVyLmNvLm56MA0GCSqGSIb3DQEBCwUAA4IBgQB5sxkE
30
+ cBsSYwK6fYpM+hA5B5yZY2+L0Z+27jF1pWGgbhPH8/FjjBLVn+VFok3CDpRqwXCl
31
+ xCO40JEkKdznNy2avOMra6PFiQyOE74kCtv7P+Fdc+FhgqI5lMon6tt9rNeXmnW/
32
+ c1NaMRdxy999hmRGzUSFjozcCwxpy/LwabxtdXwXgSay4mQ32EDjqR1TixS1+smp
33
+ 8C/NCWgpIfzpHGJsjvmH2wAfKtTTqB9CVKLCWEnCHyCaRVuKkrKjqhYCdmMBqCws
34
+ JkxfQWC+jBVeG9ZtPhQgZpfhvh+6hMhraUYRQ6XGyvBqEUe+yo6DKIT3MtGE2+CP
35
+ eX9i9ZWBydWb8/rvmwmX2kkcBbX0hZS1rcR593hGc61JR6lvkGYQ2MYskBveyaxt
36
+ Q2K9NVun/S785AP05vKkXZEFYxqG6EW012U4oLcFl5MySFajYXRYbuUpH6AY+HP8
37
+ voD0MPg1DssDLKwXyt1eKD/+Fq0bFWhwVM/1XiAXL7lyYUyOq24KHgQ2Csg=
38
+ -----END CERTIFICATE-----
39
+ date: 1980-01-02 00:00:00.000000000 Z
40
+ dependencies:
41
+ - !ruby/object:Gem::Dependency
42
+ name: protocol-http
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.68'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.68'
55
+ - !ruby/object:Gem::Dependency
56
+ name: sus
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.37'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.37'
69
+ executables: []
70
+ extensions: []
71
+ extra_rdoc_files: []
72
+ files:
73
+ - lib/sus/fixtures/protocol/http.rb
74
+ - lib/sus/fixtures/protocol/http/client.rb
75
+ - lib/sus/fixtures/protocol/http/middleware_context.rb
76
+ - lib/sus/fixtures/protocol/http/version.rb
77
+ - license.md
78
+ - readme.md
79
+ - releases.md
80
+ homepage: https://github.com/socketry/sus-fixtures-protocol-http
81
+ licenses:
82
+ - MIT
83
+ metadata:
84
+ bug_tracker_uri: https://github.com/socketry/sus-fixtures-protocol-http/issues
85
+ changelog_uri: https://github.com/socketry/sus-fixtures-protocol-http/blob/main/releases.md
86
+ documentation_uri: https://socketry.github.io/sus-fixtures-protocol-http/
87
+ funding_uri: https://github.com/sponsors/ioquatix/
88
+ source_code_uri: https://github.com/socketry/sus-fixtures-protocol-http.git
89
+ rdoc_options: []
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '3.3'
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ requirements: []
103
+ rubygems_version: 4.0.10
104
+ specification_version: 4
105
+ summary: Test fixtures for Protocol::HTTP middleware.
106
+ test_files: []
metadata.gz.sig ADDED
Binary file