http-fake 0.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.
- checksums.yaml +7 -0
- checksums.yaml.gz.sig +0 -0
- data/LICENSE.adoc +134 -0
- data/README.adoc +214 -0
- data/http-fake.gemspec +33 -0
- data/lib/http/fake/builder.rb +52 -0
- data/lib/http/fake/client.rb +27 -0
- data/lib/http/fake/connector.rb +29 -0
- data/lib/http/fake/requester.rb +23 -0
- data/lib/http/fake/responder.rb +46 -0
- data/lib/http/fake.rb +16 -0
- data.tar.gz.sig +1 -0
- metadata +137 -0
- metadata.gz.sig +0 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: f068555328be1d8bab977bd4ada2cbfbc67691714681cbf0065a6cdb5ae797bf
|
|
4
|
+
data.tar.gz: c28eecb527f8d7588036974c6af8bc87015b4aea516c36c59f5a6a4e5e46789e
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: c175d8a7f3c1d910bf4fdb05189bdf509db4f1a6542a678edf521f7ff889378938214408eee380fc8b00e749e8e030a93d2717b69de50c26e54cdf35912402cd
|
|
7
|
+
data.tar.gz: 7a9e1828ee3f847caedae2d2f7d73b8ccd7adfba6fa3ec837057ed5fec5c7d64b7c68bcfac854d8822ed33e92e614eebd92f799989e290d4a59be2be3a12f661
|
checksums.yaml.gz.sig
ADDED
|
Binary file
|
data/LICENSE.adoc
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
= Hippocratic License
|
|
2
|
+
|
|
3
|
+
Version: 2.1.0.
|
|
4
|
+
|
|
5
|
+
Purpose. The purpose of this License is for the Licensor named above to
|
|
6
|
+
permit the Licensee (as defined below) broad permission, if consistent
|
|
7
|
+
with Human Rights Laws and Human Rights Principles (as each is defined
|
|
8
|
+
below), to use and work with the Software (as defined below) within the
|
|
9
|
+
full scope of Licensor’s copyright and patent rights, if any, in the
|
|
10
|
+
Software, while ensuring attribution and protecting the Licensor from
|
|
11
|
+
liability.
|
|
12
|
+
|
|
13
|
+
Permission and Conditions. The Licensor grants permission by this
|
|
14
|
+
license ("License"), free of charge, to the extent of Licensor’s
|
|
15
|
+
rights under applicable copyright and patent law, to any person or
|
|
16
|
+
entity (the "Licensee") obtaining a copy of this software and
|
|
17
|
+
associated documentation files (the "Software"), to do everything with
|
|
18
|
+
the Software that would otherwise infringe (i) the Licensor’s copyright
|
|
19
|
+
in the Software or (ii) any patent claims to the Software that the
|
|
20
|
+
Licensor can license or becomes able to license, subject to all of the
|
|
21
|
+
following terms and conditions:
|
|
22
|
+
|
|
23
|
+
* Acceptance. This License is automatically offered to every person and
|
|
24
|
+
entity subject to its terms and conditions. Licensee accepts this
|
|
25
|
+
License and agrees to its terms and conditions by taking any action with
|
|
26
|
+
the Software that, absent this License, would infringe any intellectual
|
|
27
|
+
property right held by Licensor.
|
|
28
|
+
* Notice. Licensee must ensure that everyone who gets a copy of any part
|
|
29
|
+
of this Software from Licensee, with or without changes, also receives
|
|
30
|
+
the License and the above copyright notice (and if included by the
|
|
31
|
+
Licensor, patent, trademark and attribution notice). Licensee must cause
|
|
32
|
+
any modified versions of the Software to carry prominent notices stating
|
|
33
|
+
that Licensee changed the Software. For clarity, although Licensee is
|
|
34
|
+
free to create modifications of the Software and distribute only the
|
|
35
|
+
modified portion created by Licensee with additional or different terms,
|
|
36
|
+
the portion of the Software not modified must be distributed pursuant to
|
|
37
|
+
this License. If anyone notifies Licensee in writing that Licensee has
|
|
38
|
+
not complied with this Notice section, Licensee can keep this License by
|
|
39
|
+
taking all practical steps to comply within 30 days after the notice. If
|
|
40
|
+
Licensee does not do so, Licensee’s License (and all rights licensed
|
|
41
|
+
hereunder) shall end immediately.
|
|
42
|
+
* Compliance with Human Rights Principles and Human Rights Laws.
|
|
43
|
+
[arabic]
|
|
44
|
+
. Human Rights Principles.
|
|
45
|
+
[loweralpha]
|
|
46
|
+
.. Licensee is advised to consult the articles of the United Nations
|
|
47
|
+
Universal Declaration of Human Rights and the United Nations Global
|
|
48
|
+
Compact that define recognized principles of international human rights
|
|
49
|
+
(the "Human Rights Principles"). Licensee shall use the Software in a
|
|
50
|
+
manner consistent with Human Rights Principles.
|
|
51
|
+
.. Unless the Licensor and Licensee agree otherwise, any dispute,
|
|
52
|
+
controversy, or claim arising out of or relating to (i) Section 1(a)
|
|
53
|
+
regarding Human Rights Principles, including the breach of Section 1(a),
|
|
54
|
+
termination of this License for breach of the Human Rights Principles,
|
|
55
|
+
or invalidity of Section 1(a) or (ii) a determination of whether any Law
|
|
56
|
+
is consistent or in conflict with Human Rights Principles pursuant to
|
|
57
|
+
Section 2, below, shall be settled by arbitration in accordance with the
|
|
58
|
+
Hague Rules on Business and Human Rights Arbitration (the "Rules");
|
|
59
|
+
provided, however, that Licensee may elect not to participate in such
|
|
60
|
+
arbitration, in which event this License (and all rights licensed
|
|
61
|
+
hereunder) shall end immediately. The number of arbitrators shall be one
|
|
62
|
+
unless the Rules require otherwise.
|
|
63
|
+
+
|
|
64
|
+
Unless both the Licensor and Licensee agree to the contrary: (1) All
|
|
65
|
+
documents and information concerning the arbitration shall be public and
|
|
66
|
+
may be disclosed by any party; (2) The repository referred to under
|
|
67
|
+
Article 43 of the Rules shall make available to the public in a timely
|
|
68
|
+
manner all documents concerning the arbitration which are communicated
|
|
69
|
+
to it, including all submissions of the parties, all evidence admitted
|
|
70
|
+
into the record of the proceedings, all transcripts or other recordings
|
|
71
|
+
of hearings and all orders, decisions and awards of the arbitral
|
|
72
|
+
tribunal, subject only to the arbitral tribunal’s powers to take such
|
|
73
|
+
measures as may be necessary to safeguard the integrity of the arbitral
|
|
74
|
+
process pursuant to Articles 18, 33, 41 and 42 of the Rules; and (3)
|
|
75
|
+
Article 26(6) of the Rules shall not apply.
|
|
76
|
+
. Human Rights Laws. The Software shall not be used by any person or
|
|
77
|
+
entity for any systems, activities, or other uses that violate any Human
|
|
78
|
+
Rights Laws. "Human Rights Laws" means any applicable laws,
|
|
79
|
+
regulations, or rules (collectively, "Laws") that protect human,
|
|
80
|
+
civil, labor, privacy, political, environmental, security, economic, due
|
|
81
|
+
process, or similar rights; provided, however, that such Laws are
|
|
82
|
+
consistent and not in conflict with Human Rights Principles (a dispute
|
|
83
|
+
over the consistency or a conflict between Laws and Human Rights
|
|
84
|
+
Principles shall be determined by arbitration as stated above). Where
|
|
85
|
+
the Human Rights Laws of more than one jurisdiction are applicable or in
|
|
86
|
+
conflict with respect to the use of the Software, the Human Rights Laws
|
|
87
|
+
that are most protective of the individuals or groups harmed shall
|
|
88
|
+
apply.
|
|
89
|
+
. Indemnity. Licensee shall hold harmless and indemnify Licensor (and
|
|
90
|
+
any other contributor) against all losses, damages, liabilities,
|
|
91
|
+
deficiencies, claims, actions, judgments, settlements, interest, awards,
|
|
92
|
+
penalties, fines, costs, or expenses of whatever kind, including
|
|
93
|
+
Licensor’s reasonable attorneys’ fees, arising out of or relating to
|
|
94
|
+
Licensee’s use of the Software in violation of Human Rights Laws or
|
|
95
|
+
Human Rights Principles.
|
|
96
|
+
* Failure to Comply. Any failure of Licensee to act according to the
|
|
97
|
+
terms and conditions of this License is both a breach of the License and
|
|
98
|
+
an infringement of the intellectual property rights of the Licensor
|
|
99
|
+
(subject to exceptions under Laws, e.g., fair use). In the event of a
|
|
100
|
+
breach or infringement, the terms and conditions of this License may be
|
|
101
|
+
enforced by Licensor under the Laws of any jurisdiction to which
|
|
102
|
+
Licensee is subject. Licensee also agrees that the Licensor may enforce
|
|
103
|
+
the terms and conditions of this License against Licensee through
|
|
104
|
+
specific performance (or similar remedy under Laws) to the extent
|
|
105
|
+
permitted by Laws. For clarity, except in the event of a breach of this
|
|
106
|
+
License, infringement, or as otherwise stated in this License, Licensor
|
|
107
|
+
may not terminate this License with Licensee.
|
|
108
|
+
* Enforceability and Interpretation. If any term or provision of this
|
|
109
|
+
License is determined to be invalid, illegal, or unenforceable by a
|
|
110
|
+
court of competent jurisdiction, then such invalidity, illegality, or
|
|
111
|
+
unenforceability shall not affect any other term or provision of this
|
|
112
|
+
License or invalidate or render unenforceable such term or provision in
|
|
113
|
+
any other jurisdiction; provided, however, subject to a court
|
|
114
|
+
modification pursuant to the immediately following sentence, if any term
|
|
115
|
+
or provision of this License pertaining to Human Rights Laws or Human
|
|
116
|
+
Rights Principles is deemed invalid, illegal, or unenforceable against
|
|
117
|
+
Licensee by a court of competent jurisdiction, all rights in the
|
|
118
|
+
Software granted to Licensee shall be deemed null and void as between
|
|
119
|
+
Licensor and Licensee. Upon a determination that any term or provision
|
|
120
|
+
is invalid, illegal, or unenforceable, to the extent permitted by Laws,
|
|
121
|
+
the court may modify this License to affect the original purpose that
|
|
122
|
+
the Software be used in compliance with Human Rights Principles and
|
|
123
|
+
Human Rights Laws as closely as possible. The language in this License
|
|
124
|
+
shall be interpreted as to its fair meaning and not strictly for or
|
|
125
|
+
against any party.
|
|
126
|
+
* Disclaimer. TO THE FULL EXTENT ALLOWED BY LAW, THIS SOFTWARE COMES
|
|
127
|
+
"AS IS," WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED, AND LICENSOR AND
|
|
128
|
+
ANY OTHER CONTRIBUTOR SHALL NOT BE LIABLE TO ANYONE FOR ANY DAMAGES OR
|
|
129
|
+
OTHER LIABILITY ARISING FROM, OUT OF, OR IN CONNECTION WITH THE SOFTWARE
|
|
130
|
+
OR THIS LICENSE, UNDER ANY KIND OF LEGAL CLAIM.
|
|
131
|
+
|
|
132
|
+
This Hippocratic License is an link:https://ethicalsource.dev[Ethical Source license] and is offered
|
|
133
|
+
for use by licensors and licensees at their own risk, on an "AS IS" basis, and with no warranties
|
|
134
|
+
express or implied, to the maximum extent permitted by Laws.
|
data/README.adoc
ADDED
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
:http_link: link:https://github.com/httprb/http[HTTP]
|
|
2
|
+
|
|
3
|
+
:toc: macro
|
|
4
|
+
:toclevels: 5
|
|
5
|
+
:figure-caption!:
|
|
6
|
+
|
|
7
|
+
= HTTP Fake
|
|
8
|
+
|
|
9
|
+
HTTP Fake is a companion to the {http_link} gem when you want a convenient way to test HTTP requests
|
|
10
|
+
by swapping out your _real_ HTTP client with this _fake_ HTTP client. Using a fake allows you to
|
|
11
|
+
speed up and simplify your test suite by answering fake responses without hitting a live API. You'll
|
|
12
|
+
still want to test against a live API, eventually, within your integration tests but at a lower
|
|
13
|
+
level, like your unit tests, you can use this gem instead. This gem is particularly useful when
|
|
14
|
+
using _Dependency Injection_, especially when coupled with the
|
|
15
|
+
link:https://www.alchemists.io/projects/auto_injector[Auto Injector] gem.
|
|
16
|
+
|
|
17
|
+
toc::[]
|
|
18
|
+
|
|
19
|
+
== Features
|
|
20
|
+
|
|
21
|
+
* Provides a fake HTTP client as a testing companion to the {http_link} gem.
|
|
22
|
+
* Supports the following HTTP verbs: CONNECT, DELETE, GET, HEAD, OPTIONS, PATCH, POST, PUT, and
|
|
23
|
+
TRACE.
|
|
24
|
+
* Uses a simple DSL for defining HTTP endpoints, headers, bodies, and statuses.
|
|
25
|
+
* Works well with objects that use _Dependency Injection_.
|
|
26
|
+
* Speeds up your test suite when you don't need a live API.
|
|
27
|
+
|
|
28
|
+
== Requirements
|
|
29
|
+
|
|
30
|
+
. link:https://www.ruby-lang.org[Ruby].
|
|
31
|
+
. {http_link}.
|
|
32
|
+
|
|
33
|
+
== Setup
|
|
34
|
+
|
|
35
|
+
To install within an existing project, run:
|
|
36
|
+
|
|
37
|
+
[source,bash]
|
|
38
|
+
----
|
|
39
|
+
bundle add http-fake
|
|
40
|
+
----
|
|
41
|
+
|
|
42
|
+
You'll want to ensure this gem is part of your _test_ group since it's
|
|
43
|
+
only meant to aid in writing specs.
|
|
44
|
+
|
|
45
|
+
== Usage
|
|
46
|
+
|
|
47
|
+
This gem works with any test framework and, for demonstration purposes, we'll assume you're using
|
|
48
|
+
link:https://rspec.info[RSpec]. You can adapt these examples to your test framework of choice. A
|
|
49
|
+
simple spec might look the this:
|
|
50
|
+
|
|
51
|
+
[source,ruby]
|
|
52
|
+
----
|
|
53
|
+
RSpec.describe Endpoint do
|
|
54
|
+
subject(:endpoint) { described_class.new http: }
|
|
55
|
+
|
|
56
|
+
let :http do
|
|
57
|
+
HTTP::Fake::Client.new do
|
|
58
|
+
get "/customers" do
|
|
59
|
+
headers["Content-Type"] = "application/json"
|
|
60
|
+
status 200
|
|
61
|
+
|
|
62
|
+
<<~JSON
|
|
63
|
+
{
|
|
64
|
+
"customers": [
|
|
65
|
+
{"name": "Jill Smith"}
|
|
66
|
+
]
|
|
67
|
+
}
|
|
68
|
+
JSON
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
describe "#customers" do
|
|
74
|
+
it "answers customers array when successful" do
|
|
75
|
+
response = endpoint.customers
|
|
76
|
+
expect(response.parse).to eq(customers: [{name: "Jill Smith"}])
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
----
|
|
81
|
+
|
|
82
|
+
As you can see, our _fake_ `http` client has been defined and injected into our `endpoint` subject.
|
|
83
|
+
When the fake is defined, the path, headers, status, and body are registered as well. This
|
|
84
|
+
allows the fake to match against your real implementation's URL path and swap out making a real HTTP
|
|
85
|
+
call with the canned response defined by the fake. When asking the endpoint for its customers, we
|
|
86
|
+
get back the response as defined by the fake with all of the normal capabilities as if we were using
|
|
87
|
+
the real HTTP client. This works because this gem uses
|
|
88
|
+
link:https://github.com/sinatra/mustermann[Mustermann] for pattern matching against the routes you
|
|
89
|
+
define. This means you can defined routes that are explicit -- as shown above -- or fuzzy based on
|
|
90
|
+
your testing needs. You can also define multiple endpoints for the same fake in case your
|
|
91
|
+
implementation needs to test multiple endpoints at once. Example:
|
|
92
|
+
|
|
93
|
+
[source,ruby]
|
|
94
|
+
----
|
|
95
|
+
let :http do
|
|
96
|
+
HTTP::Fake::Client.new do
|
|
97
|
+
connect("/") { status 200 }
|
|
98
|
+
|
|
99
|
+
head("/") { status 200 }
|
|
100
|
+
options("/") { status 204 }
|
|
101
|
+
|
|
102
|
+
get "/customers" do
|
|
103
|
+
headers["Content-Type"] = "application/json"
|
|
104
|
+
status 200
|
|
105
|
+
|
|
106
|
+
<<~JSON
|
|
107
|
+
{
|
|
108
|
+
"customers": [
|
|
109
|
+
{"name": "Jill Smith"}
|
|
110
|
+
]
|
|
111
|
+
}
|
|
112
|
+
JSON
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
post "/customers" do
|
|
116
|
+
headers["Content-Type"] = "application/json"
|
|
117
|
+
status 201
|
|
118
|
+
{}
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
put "/customers/1" do
|
|
122
|
+
headers["Content-Type"] = "application/json"
|
|
123
|
+
status 200
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
patch "/customers/1" do
|
|
127
|
+
headers["Content-Type"] = "application/json"
|
|
128
|
+
status 200
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
delete("/customers/1") { status 204 }
|
|
132
|
+
trace("/") { status 200 }
|
|
133
|
+
end
|
|
134
|
+
end
|
|
135
|
+
----
|
|
136
|
+
|
|
137
|
+
Lastly, in the examples above, I've been showing how you can work with JSON responses but you might
|
|
138
|
+
want to use other MIME types. For example, XML:
|
|
139
|
+
|
|
140
|
+
[source,ruby]
|
|
141
|
+
----
|
|
142
|
+
HTTP::Fake::Client.new do
|
|
143
|
+
get "/customers" do
|
|
144
|
+
headers["Content-Type"] = "application/xml"
|
|
145
|
+
status 200
|
|
146
|
+
|
|
147
|
+
<<~XML
|
|
148
|
+
<customer>
|
|
149
|
+
<id>1</id>
|
|
150
|
+
<name>Jill Smith</name>
|
|
151
|
+
</customer>
|
|
152
|
+
XML
|
|
153
|
+
end
|
|
154
|
+
end
|
|
155
|
+
----
|
|
156
|
+
|
|
157
|
+
Plain text would work too:
|
|
158
|
+
|
|
159
|
+
[source,ruby]
|
|
160
|
+
----
|
|
161
|
+
HTTP::Fake::Client.new do
|
|
162
|
+
get "/customers" do
|
|
163
|
+
headers["Content-Type"] = "text/plain"
|
|
164
|
+
status 200
|
|
165
|
+
|
|
166
|
+
"1 - Jill Smith"
|
|
167
|
+
end
|
|
168
|
+
end
|
|
169
|
+
----
|
|
170
|
+
|
|
171
|
+
Since you have the ability to define your own headers and status codes, you can also test failure
|
|
172
|
+
response behavior as well. I'll leave that up to you to explore and experiment with further.
|
|
173
|
+
|
|
174
|
+
== Development
|
|
175
|
+
|
|
176
|
+
Clone the repository and then run:
|
|
177
|
+
|
|
178
|
+
[source,bash]
|
|
179
|
+
----
|
|
180
|
+
bin/setup
|
|
181
|
+
----
|
|
182
|
+
|
|
183
|
+
You can also use the IRB console for direct access to all objects:
|
|
184
|
+
|
|
185
|
+
[source,bash]
|
|
186
|
+
----
|
|
187
|
+
bin/console
|
|
188
|
+
----
|
|
189
|
+
|
|
190
|
+
== Tests
|
|
191
|
+
|
|
192
|
+
To test, run:
|
|
193
|
+
|
|
194
|
+
[source,bash]
|
|
195
|
+
----
|
|
196
|
+
bundle exec rake
|
|
197
|
+
----
|
|
198
|
+
|
|
199
|
+
== link:https://www.alchemists.io/policies/license[License]
|
|
200
|
+
|
|
201
|
+
== link:https://www.alchemists.io/policies/security[Security]
|
|
202
|
+
|
|
203
|
+
== link:https://www.alchemists.io/policies/code_of_conduct[Code of Conduct]
|
|
204
|
+
|
|
205
|
+
== link:https://www.alchemists.io/policies/contributions[Contributions]
|
|
206
|
+
|
|
207
|
+
== link:https://www.alchemists.io/projects/http-fake/versions[Versions]
|
|
208
|
+
|
|
209
|
+
== link:https://www.alchemists.io/community[Community]
|
|
210
|
+
|
|
211
|
+
== Credits
|
|
212
|
+
|
|
213
|
+
* Built with link:https://www.alchemists.io/projects/gemsmith[Gemsmith].
|
|
214
|
+
* Engineered by link:https://www.alchemists.io/team/brooke_kuhlmann[Brooke Kuhlmann].
|
data/http-fake.gemspec
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
Gem::Specification.new do |spec|
|
|
4
|
+
spec.name = "http-fake"
|
|
5
|
+
spec.version = "0.0.0"
|
|
6
|
+
spec.authors = ["Brooke Kuhlmann"]
|
|
7
|
+
spec.email = ["brooke@alchemists.io"]
|
|
8
|
+
spec.homepage = "https://www.alchemists.io/projects/http-fake"
|
|
9
|
+
spec.summary = "Provides a fake but equivalent implementation of the HTTP gem for test suites."
|
|
10
|
+
spec.license = "Hippocratic-2.1"
|
|
11
|
+
|
|
12
|
+
spec.metadata = {
|
|
13
|
+
"bug_tracker_uri" => "https://github.com/bkuhlmann/http-fake/issues",
|
|
14
|
+
"changelog_uri" => "https://www.alchemists.io/projects/http-fake/versions",
|
|
15
|
+
"documentation_uri" => "https://www.alchemists.io/projects/http-fake",
|
|
16
|
+
"funding_uri" => "https://github.com/sponsors/bkuhlmann",
|
|
17
|
+
"label" => "HTTP Fake",
|
|
18
|
+
"rubygems_mfa_required" => "true",
|
|
19
|
+
"source_code_uri" => "https://github.com/bkuhlmann/http-fake"
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
spec.signing_key = Gem.default_key_path
|
|
23
|
+
spec.cert_chain = [Gem.default_cert_path]
|
|
24
|
+
|
|
25
|
+
spec.required_ruby_version = "~> 3.1"
|
|
26
|
+
spec.add_dependency "http", "~> 5.0"
|
|
27
|
+
spec.add_dependency "mustermann", "~> 1.1"
|
|
28
|
+
spec.add_dependency "refinements", "~> 9.4"
|
|
29
|
+
spec.add_dependency "zeitwerk", "~> 2.5"
|
|
30
|
+
|
|
31
|
+
spec.extra_rdoc_files = Dir["README*", "LICENSE*"]
|
|
32
|
+
spec.files = Dir["*.gemspec", "lib/**/*"]
|
|
33
|
+
end
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "uri"
|
|
4
|
+
require "refinements/hashes"
|
|
5
|
+
|
|
6
|
+
module HTTP
|
|
7
|
+
module Fake
|
|
8
|
+
# Builds the response for a request.
|
|
9
|
+
class Builder
|
|
10
|
+
using Refinements::Hashes
|
|
11
|
+
|
|
12
|
+
ALLOWED_VERBS = /\A(connect|delete|get|head|options|patch|post|put|trace)\z/
|
|
13
|
+
|
|
14
|
+
attr_reader :requests
|
|
15
|
+
|
|
16
|
+
# :reek:DuplicateMethodCall
|
|
17
|
+
def initialize verbs: ALLOWED_VERBS, responder: Responder
|
|
18
|
+
@verbs = verbs
|
|
19
|
+
@responder = responder
|
|
20
|
+
@requests ||= Hash.with_default Hash.with_default([])
|
|
21
|
+
@responders = Hash.with_default []
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def request verb, uri, arguments = {}
|
|
25
|
+
responder = responders[verb].find { |fake| fake.match? verb, URI(uri).path }
|
|
26
|
+
missing_route_error verb, uri unless responder
|
|
27
|
+
requests[uri][verb] << arguments
|
|
28
|
+
responder.call uri, arguments
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def method_missing(verb, *arguments, &)
|
|
32
|
+
respond_to_missing?(verb) ? add_responder(verb, *arguments, &) : super
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
private
|
|
36
|
+
|
|
37
|
+
attr_reader :verbs, :responder, :responders
|
|
38
|
+
|
|
39
|
+
def respond_to_missing?(verb, include_private = false) = verbs.match?(verb) || super
|
|
40
|
+
|
|
41
|
+
def add_responder verb, pattern, &block
|
|
42
|
+
responder.new(verb, pattern, block).then do |instance|
|
|
43
|
+
responders[[verb, pattern]].append instance
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def missing_route_error verb, uri
|
|
48
|
+
fail StandardError, "No route for #{verb.upcase} #{uri} in responders: #{responders}."
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "forwardable"
|
|
4
|
+
|
|
5
|
+
module HTTP
|
|
6
|
+
module Fake
|
|
7
|
+
# Provides the primary client for dealing with fake requests and responses.
|
|
8
|
+
class Client
|
|
9
|
+
include Chainable
|
|
10
|
+
extend Forwardable
|
|
11
|
+
|
|
12
|
+
delegate %i[requests] => :builder
|
|
13
|
+
|
|
14
|
+
def initialize builder: Builder.new, requester: Requester, &block
|
|
15
|
+
@builder = builder
|
|
16
|
+
@requester = requester
|
|
17
|
+
@builder.instance_eval(&block)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
private
|
|
21
|
+
|
|
22
|
+
attr_reader :builder, :requester
|
|
23
|
+
|
|
24
|
+
def branch(arguments) = requester.new arguments, builder:
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module HTTP
|
|
4
|
+
module Fake
|
|
5
|
+
# Connects an HTTP request and response together.
|
|
6
|
+
class Connector
|
|
7
|
+
def initialize version: Connection::HTTP_1_1, request: Request, response: Response
|
|
8
|
+
@version = version
|
|
9
|
+
@request = request
|
|
10
|
+
@response = response
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def call **arguments
|
|
14
|
+
response.new defaults.merge(
|
|
15
|
+
request: build_request(**arguments),
|
|
16
|
+
**arguments.slice(:headers, :version, :body, :status)
|
|
17
|
+
)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
private
|
|
21
|
+
|
|
22
|
+
attr_reader :version, :request, :response
|
|
23
|
+
|
|
24
|
+
def defaults = {headers: {}, version:, status: 200}
|
|
25
|
+
|
|
26
|
+
def build_request(**arguments) = request.new arguments.slice :verb, :uri
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module HTTP
|
|
4
|
+
module Fake
|
|
5
|
+
# Handles an incoming request.
|
|
6
|
+
class Requester
|
|
7
|
+
include Chainable
|
|
8
|
+
|
|
9
|
+
def initialize defaults = {}, builder: Builder.new
|
|
10
|
+
@defaults = defaults
|
|
11
|
+
@builder = builder
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def request(verb, uri, arguments = {}) = builder.request verb, uri, defaults.merge(arguments)
|
|
15
|
+
|
|
16
|
+
private
|
|
17
|
+
|
|
18
|
+
attr_reader :defaults, :builder
|
|
19
|
+
|
|
20
|
+
def branch(arguments) = self.class.new defaults.merge(arguments), builder:
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "mustermann"
|
|
4
|
+
require "refinements/strings"
|
|
5
|
+
|
|
6
|
+
module HTTP
|
|
7
|
+
module Fake
|
|
8
|
+
# Handles an outgoing response.
|
|
9
|
+
class Responder
|
|
10
|
+
using Refinements::Strings
|
|
11
|
+
|
|
12
|
+
attr_reader :headers
|
|
13
|
+
|
|
14
|
+
def initialize verb, pattern, function
|
|
15
|
+
@verb = verb
|
|
16
|
+
@pattern = Mustermann.new pattern
|
|
17
|
+
@function = function
|
|
18
|
+
@headers = {}
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def match?(verb, path) = !String(pattern.match(path)).blank? && verb == self.verb
|
|
22
|
+
|
|
23
|
+
def call path, arguments = {}
|
|
24
|
+
pattern.params(path)
|
|
25
|
+
.then { |params| instance_exec(params, arguments, &function) }
|
|
26
|
+
.then { |data| connect path, data }
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def status(code = nil) = @status ||= code
|
|
30
|
+
|
|
31
|
+
private
|
|
32
|
+
|
|
33
|
+
attr_reader :verb, :pattern, :function
|
|
34
|
+
|
|
35
|
+
def connect path, data
|
|
36
|
+
Connector.new.call verb:,
|
|
37
|
+
uri: "https://example.com/#{path}",
|
|
38
|
+
headers:,
|
|
39
|
+
body: body(data),
|
|
40
|
+
status:
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def body(data) = data == status ? "" : data
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
data/lib/http/fake.rb
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "zeitwerk"
|
|
4
|
+
require "http"
|
|
5
|
+
|
|
6
|
+
Zeitwerk::Loader.new.then do |loader|
|
|
7
|
+
loader.push_dir "#{__dir__}/.."
|
|
8
|
+
loader.inflector.inflect "http" => "HTTP"
|
|
9
|
+
loader.setup
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
module HTTP
|
|
13
|
+
# Main namespace.
|
|
14
|
+
module Fake
|
|
15
|
+
end
|
|
16
|
+
end
|
data.tar.gz.sig
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
���@Fp.�S锡���!���`�)5�[��a�{�o�a�0Ƒ��uwj;WH�49G~t ��d�n~�#]<�A�ރ-l�v���³k��+�b9*��]�qg>��%�}W�8���U6.X����4�(�{��j�;
|
metadata
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: http-fake
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Brooke Kuhlmann
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain:
|
|
11
|
+
- |
|
|
12
|
+
-----BEGIN CERTIFICATE-----
|
|
13
|
+
MIIC/jCCAeagAwIBAgIBBTANBgkqhkiG9w0BAQsFADAlMSMwIQYDVQQDDBpicm9v
|
|
14
|
+
a2UvREM9YWxjaGVtaXN0cy9EQz1pbzAeFw0yMjAzMTkxNzI0MzJaFw0yMzAzMTkx
|
|
15
|
+
NzI0MzJaMCUxIzAhBgNVBAMMGmJyb29rZS9EQz1hbGNoZW1pc3RzL0RDPWlvMIIB
|
|
16
|
+
IjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA6l1qpXTiomH1RfMRloyw7MiE
|
|
17
|
+
xyVx/x8Yc3EupdH7uhNaTXQGyORN6aOY//1QXXMHIZ9tW74nZLhesWMSUMYy0XhB
|
|
18
|
+
brs+KkurHnc9FnEJAbG7ebGvl/ncqZt72nQvaxpDxvuCBHgJAz+8i5wl6FhLw+oT
|
|
19
|
+
9z0A8KcGhz67SdcoQiD7qiCjL/2NTeWHOzkpPrdGlt088+VerEEGf5I13QCvaftP
|
|
20
|
+
D5vkU0YlAm1r98BymuJlcQ1qdkVEI1d48ph4kcS0S0nv1RiuyVb6TCAR3Nu3VaVq
|
|
21
|
+
3fPzZKJLZBx67UvXdbdicWPiUR75elI4PXpLIic3xytaF52ZJYyKZCNZJhNwfQID
|
|
22
|
+
AQABozkwNzAJBgNVHRMEAjAAMAsGA1UdDwQEAwIEsDAdBgNVHQ4EFgQU0nzow9vc
|
|
23
|
+
2CdikiiE3fJhP/gY4ggwDQYJKoZIhvcNAQELBQADggEBAJbbNyWzFjqUNVPPCUCo
|
|
24
|
+
IMrhDa9xf1xkORXNYYbmXgoxRy/KyNbUr+jgEEoWJAm9GXlcqxxWAUI6pK/i4/Qi
|
|
25
|
+
X6rPFEFmeObDOHNvuqy8Hd6AYsu+kP94U/KJhe9wnWGMmGoNKJNU3EkW3jM/osSl
|
|
26
|
+
+JRxiH5t4WtnDiVyoYl5nYC02rYdjJkG6VMxDymXTqn7u6HhYgZkGujq1UPar8x2
|
|
27
|
+
hNIWJblDKKSu7hA2d6+kUthuYo13o1sg1Da/AEDg0hoZSUvhqDEF5Hy232qb3pDt
|
|
28
|
+
CxDe2+VuChj4I1nvIHdu+E6XoEVlanUPKmSg6nddhkKn2gC45Kyzh6FZqnzH/CRp
|
|
29
|
+
RFE=
|
|
30
|
+
-----END CERTIFICATE-----
|
|
31
|
+
date: 2022-05-15 00:00:00.000000000 Z
|
|
32
|
+
dependencies:
|
|
33
|
+
- !ruby/object:Gem::Dependency
|
|
34
|
+
name: http
|
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
|
36
|
+
requirements:
|
|
37
|
+
- - "~>"
|
|
38
|
+
- !ruby/object:Gem::Version
|
|
39
|
+
version: '5.0'
|
|
40
|
+
type: :runtime
|
|
41
|
+
prerelease: false
|
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
43
|
+
requirements:
|
|
44
|
+
- - "~>"
|
|
45
|
+
- !ruby/object:Gem::Version
|
|
46
|
+
version: '5.0'
|
|
47
|
+
- !ruby/object:Gem::Dependency
|
|
48
|
+
name: mustermann
|
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
|
50
|
+
requirements:
|
|
51
|
+
- - "~>"
|
|
52
|
+
- !ruby/object:Gem::Version
|
|
53
|
+
version: '1.1'
|
|
54
|
+
type: :runtime
|
|
55
|
+
prerelease: false
|
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
57
|
+
requirements:
|
|
58
|
+
- - "~>"
|
|
59
|
+
- !ruby/object:Gem::Version
|
|
60
|
+
version: '1.1'
|
|
61
|
+
- !ruby/object:Gem::Dependency
|
|
62
|
+
name: refinements
|
|
63
|
+
requirement: !ruby/object:Gem::Requirement
|
|
64
|
+
requirements:
|
|
65
|
+
- - "~>"
|
|
66
|
+
- !ruby/object:Gem::Version
|
|
67
|
+
version: '9.4'
|
|
68
|
+
type: :runtime
|
|
69
|
+
prerelease: false
|
|
70
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
71
|
+
requirements:
|
|
72
|
+
- - "~>"
|
|
73
|
+
- !ruby/object:Gem::Version
|
|
74
|
+
version: '9.4'
|
|
75
|
+
- !ruby/object:Gem::Dependency
|
|
76
|
+
name: zeitwerk
|
|
77
|
+
requirement: !ruby/object:Gem::Requirement
|
|
78
|
+
requirements:
|
|
79
|
+
- - "~>"
|
|
80
|
+
- !ruby/object:Gem::Version
|
|
81
|
+
version: '2.5'
|
|
82
|
+
type: :runtime
|
|
83
|
+
prerelease: false
|
|
84
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
85
|
+
requirements:
|
|
86
|
+
- - "~>"
|
|
87
|
+
- !ruby/object:Gem::Version
|
|
88
|
+
version: '2.5'
|
|
89
|
+
description:
|
|
90
|
+
email:
|
|
91
|
+
- brooke@alchemists.io
|
|
92
|
+
executables: []
|
|
93
|
+
extensions: []
|
|
94
|
+
extra_rdoc_files:
|
|
95
|
+
- README.adoc
|
|
96
|
+
- LICENSE.adoc
|
|
97
|
+
files:
|
|
98
|
+
- LICENSE.adoc
|
|
99
|
+
- README.adoc
|
|
100
|
+
- http-fake.gemspec
|
|
101
|
+
- lib/http/fake.rb
|
|
102
|
+
- lib/http/fake/builder.rb
|
|
103
|
+
- lib/http/fake/client.rb
|
|
104
|
+
- lib/http/fake/connector.rb
|
|
105
|
+
- lib/http/fake/requester.rb
|
|
106
|
+
- lib/http/fake/responder.rb
|
|
107
|
+
homepage: https://www.alchemists.io/projects/http-fake
|
|
108
|
+
licenses:
|
|
109
|
+
- Hippocratic-2.1
|
|
110
|
+
metadata:
|
|
111
|
+
bug_tracker_uri: https://github.com/bkuhlmann/http-fake/issues
|
|
112
|
+
changelog_uri: https://www.alchemists.io/projects/http-fake/versions
|
|
113
|
+
documentation_uri: https://www.alchemists.io/projects/http-fake
|
|
114
|
+
funding_uri: https://github.com/sponsors/bkuhlmann
|
|
115
|
+
label: HTTP Fake
|
|
116
|
+
rubygems_mfa_required: 'true'
|
|
117
|
+
source_code_uri: https://github.com/bkuhlmann/http-fake
|
|
118
|
+
post_install_message:
|
|
119
|
+
rdoc_options: []
|
|
120
|
+
require_paths:
|
|
121
|
+
- lib
|
|
122
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
123
|
+
requirements:
|
|
124
|
+
- - "~>"
|
|
125
|
+
- !ruby/object:Gem::Version
|
|
126
|
+
version: '3.1'
|
|
127
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
128
|
+
requirements:
|
|
129
|
+
- - ">="
|
|
130
|
+
- !ruby/object:Gem::Version
|
|
131
|
+
version: '0'
|
|
132
|
+
requirements: []
|
|
133
|
+
rubygems_version: 3.3.13
|
|
134
|
+
signing_key:
|
|
135
|
+
specification_version: 4
|
|
136
|
+
summary: Provides a fake but equivalent implementation of the HTTP gem for test suites.
|
|
137
|
+
test_files: []
|
metadata.gz.sig
ADDED
|
Binary file
|