File: endpoint_spec.rb

package info (click to toggle)
ruby-async-http 0.59.5-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid
  • size: 572 kB
  • sloc: ruby: 4,164; javascript: 40; makefile: 4
file content (154 lines) | stat: -rw-r--r-- 4,815 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# Copyright, 2018, by Samuel G. D. Williams. <http://www.codeotaku.com>
# 
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

require 'async/http/endpoint'

RSpec.describe Async::HTTP::Endpoint do
	it "should fail to parse relative url" do
		expect{
			described_class.parse("/foo/bar")
		}.to raise_error(ArgumentError, /absolute/)
	end
	
	describe '#port' do
		let(:url_string) {"https://localhost:9292"}
		
		it "extracts port from URL" do
			endpoint = Async::HTTP::Endpoint.parse(url_string)
			
			expect(endpoint.port).to eq 9292
		end
		
		it "extracts port from options" do
			endpoint = Async::HTTP::Endpoint.parse(url_string, port: 9000)
			
			expect(endpoint.port).to eq 9000
		end
	end
	
	describe '#hostname' do
		describe Async::HTTP::Endpoint.parse("https://127.0.0.1:9292") do
			it {is_expected.to have_attributes(hostname: '127.0.0.1')}
			
			it "should be connecting to 127.0.0.1" do
				expect(subject.endpoint).to be_a Async::IO::SSLEndpoint
				expect(subject.endpoint).to have_attributes(hostname: '127.0.0.1')
				expect(subject.endpoint.endpoint).to have_attributes(hostname: '127.0.0.1')
			end
		end
		
		describe Async::HTTP::Endpoint.parse("https://127.0.0.1:9292", hostname: 'localhost') do
			it {is_expected.to have_attributes(hostname: 'localhost')}
			it {is_expected.to_not be_localhost}
			
			it "should be connecting to localhost" do
				expect(subject.endpoint).to be_a Async::IO::SSLEndpoint
				expect(subject.endpoint).to have_attributes(hostname: '127.0.0.1')
				expect(subject.endpoint.endpoint).to have_attributes(hostname: 'localhost')
			end
		end
	end
	
	describe '.for' do
		context Async::HTTP::Endpoint.for("http", "localhost") do
			it {is_expected.to have_attributes(scheme: "http", hostname: "localhost", path: "/")}
			it {is_expected.to_not be_secure}
		end

		context Async::HTTP::Endpoint.for("http", "localhost", "/foo") do
			it {is_expected.to have_attributes(scheme: "http", hostname: "localhost", path: "/foo")}
		end
	end
	
	describe '#secure?' do
		subject {Async::HTTP::Endpoint.parse(description)}
		
		context 'http://localhost' do
			it { is_expected.to_not be_secure }
		end
		
		context 'https://localhost' do
			it { is_expected.to be_secure }
		end
		
		context 'with scheme: https' do
			subject {Async::HTTP::Endpoint.parse("http://localhost", scheme: 'https')}
			
			it { is_expected.to be_secure }
		end
	end
	
	describe '#localhost?' do
		subject {Async::HTTP::Endpoint.parse(description)}
		
		context 'http://localhost' do
			it { is_expected.to be_localhost }
		end
		
		context 'http://hello.localhost' do
			it { is_expected.to be_localhost }
		end
		
		context 'http://localhost.' do
			it { is_expected.to be_localhost }
		end
		
		context 'http://hello.localhost.' do
			it { is_expected.to be_localhost }
		end
		
		context 'http://localhost.com' do
			it { is_expected.to_not be_localhost }
		end
	end
	
	describe '#path' do
		it "can normal urls" do
			endpoint = Async::HTTP::Endpoint.parse("http://foo.com/bar?baz")
			expect(endpoint.path).to be == "/bar?baz"
		end
		
		it "can handle websocket urls" do
			endpoint = Async::HTTP::Endpoint.parse("wss://foo.com/bar?baz")
			expect(endpoint.path).to be == "/bar?baz"
		end
	end
end

RSpec.describe "http://www.google.com/search" do
	let(:endpoint) {Async::HTTP::Endpoint.parse(subject)}
	
	it "should be valid endpoint" do
		expect{endpoint}.to_not raise_error
	end
	
	it "should select the correct protocol" do
		expect(endpoint.protocol).to be Async::HTTP::Protocol::HTTP1
	end
	
	it "should parse the correct hostname" do
		expect(endpoint.hostname).to be == "www.google.com"
	end
	
	it "should not be equal if path is different" do
		other = Async::HTTP::Endpoint.parse('http://www.google.com/search?q=ruby')
		expect(endpoint).to_not be_eql other
	end
end