-
-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathinternet.rb
61 lines (44 loc) · 1.61 KB
/
internet.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
60
61
# frozen_string_literal: true
# Released under the MIT License.
# Copyright, 2018-2024, by Samuel Williams.
# Copyright, 2024, by Igor Sidorov.
# Copyright, 2024, by Hal Brodigan.
require "async/http/internet"
require "async/reactor"
require "json"
require "sus/fixtures/async"
describe Async::HTTP::Internet do
include Sus::Fixtures::Async::ReactorContext
let(:internet) {subject.new}
let(:headers) {[["accept", "*/*"], ["user-agent", "async-http"]]}
it "can fetch remote website" do
response = internet.get("https://www.google.com/", headers)
expect(response).to be(:success?)
response.close
end
it "can accept URI::HTTP objects" do
uri = URI.parse("https://www.google.com/")
response = internet.get(uri, headers)
expect(response).to be(:success?)
ensure
response&.close
end
let(:sample) {{"hello" => "world"}}
let(:body) {[JSON.dump(sample)]}
# This test is increasingly flakey.
it "can fetch remote json" do
response = internet.post("https://httpbin.org/anything", headers, body)
expect(response).to be(:success?)
expect{JSON.parse(response.read)}.not.to raise_exception
end
it "can fetch remote website when given custom endpoint instead of url" do
ssl_context = OpenSSL::SSL::SSLContext.new
ssl_context.set_params(verify_mode: OpenSSL::SSL::VERIFY_NONE)
# example of site with invalid certificate that will fail to be fetched without custom SSL options
endpoint = Async::HTTP::Endpoint.parse("https://expired.badssl.com", ssl_context: ssl_context)
response = internet.get(endpoint, headers)
expect(response).to be(:success?)
ensure
response&.close
end
end