-
-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathmock.rb
59 lines (44 loc) · 1.43 KB
/
mock.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# frozen_string_literal: true
# Released under the MIT License.
# Copyright, 2024, by Samuel Williams.
require "async/http/mock"
require "async/http/endpoint"
require "async/http/client"
require "sus/fixtures/async/reactor_context"
describe Async::HTTP::Mock do
include Sus::Fixtures::Async::ReactorContext
let(:endpoint) {Async::HTTP::Mock::Endpoint.new}
it "can respond to requests" do
server = Async do
endpoint.run do |request|
::Protocol::HTTP::Response[200, [], ["Hello World"]]
end
end
client = Async::HTTP::Client.new(endpoint)
response = client.get("/index")
expect(response).to be(:success?)
expect(response.read).to be == "Hello World"
end
with "mocked client" do
it "can mock a client" do
server = Async do
endpoint.run do |request|
::Protocol::HTTP::Response[200, [], ["Authority: #{request.authority}"]]
end
end
mock(Async::HTTP::Client) do |mock|
replacement_endpoint = self.endpoint
mock.wrap(:new) do |original, original_endpoint, **options|
original.call(replacement_endpoint.wrap(original_endpoint), **options)
end
end
google_endpoint = Async::HTTP::Endpoint.parse("https://www.google.com")
client = Async::HTTP::Client.new(google_endpoint)
response = client.get("/search?q=hello")
expect(response).to be(:success?)
expect(response.read).to be == "Authority: www.google.com"
ensure
response&.close
end
end
end