File: client.rb

package info (click to toggle)
ruby-async-http 0.94.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 916 kB
  • sloc: ruby: 5,224; javascript: 40; makefile: 4
file content (73 lines) | stat: -rw-r--r-- 1,705 bytes parent folder | download
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
62
63
64
65
66
67
68
69
70
71
72
73
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2017-2024, by Samuel Williams.

require "async/http/server"
require "async/http/client"
require "async/reactor"

require "async/http/endpoint"
require "protocol/http/accept_encoding"

require "sus/fixtures/async"
require "sus/fixtures/async/http"

describe Async::HTTP::Client do
	with "basic server" do
		include Sus::Fixtures::Async::HTTP::ServerContext
		
		it "client can get resource" do
			response = client.get("/")
			response.read
			expect(response).to be(:success?)
		end
		
		with "client" do
			with "#as_json" do
				it "generates a JSON representation" do
					expect(client.as_json).to be == {
						endpoint: client.endpoint.to_s,
						protocol: client.protocol,
						retries: client.retries,
						scheme: endpoint.scheme,
						authority: endpoint.authority,
					}
				end
				
				it "generates a JSON string" do
					expect(JSON.dump(client)).to be == client.to_json
				end
			end
		end
		
		with "server" do
			with "#as_json" do
				it "generates a JSON representation" do
					expect(server.as_json).to be == {
						endpoint: server.endpoint.to_s,
						protocol: server.protocol,
						scheme: server.scheme,
					}
				end
				
				it "generates a JSON string" do
					expect(JSON.dump(server)).to be == server.to_json
				end
			end
		end
	end
	
	with "non-existant host" do
		include Sus::Fixtures::Async::ReactorContext
		
		let(:endpoint) {Async::HTTP::Endpoint.parse("http://the.future")}
		let(:client) {Async::HTTP::Client.new(endpoint)}
		
		it "should fail to connect" do
			expect do
				client.get("/")
			end.to raise_exception(SocketError, message: be =~ /not known/)
		end
	end
end