File: http.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 (70 lines) | stat: -rwxr-xr-x 1,751 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
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2024, by Thomas Morgan.
# Copyright, 2024-2025, by Samuel Williams.

require "async/http/protocol/http"
require "async/http/a_protocol"

describe Async::HTTP::Protocol::HTTP do
	let(:protocol) {subject.default}
	
	with ".default" do
		it "has a default instance" do
			expect(protocol).to be_a Async::HTTP::Protocol::HTTP
		end
	end
	
	with "#protocol_for" do
		let(:buffer) {StringIO.new}
		
		it "it can detect http/1.1" do
			buffer.write("GET / HTTP/1.1\r\nHost: localhost\r\n\r\n")
			buffer.rewind
			
			stream = IO::Stream(buffer)
			
			expect(protocol.protocol_for(stream)).to be == Async::HTTP::Protocol::HTTP1
		end
		
		it "it can detect http/2" do
			# This special preface is used to indicate that the client would like to use HTTP/2.
			# https://www.rfc-editor.org/rfc/rfc7540.html#section-3.5
			buffer.write("PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n")
			buffer.rewind
			
			stream = IO::Stream(buffer)
			
			expect(protocol.protocol_for(stream)).to be == Async::HTTP::Protocol::HTTP2
		end
	end
	
	with "server" do
		include Sus::Fixtures::Async::HTTP::ServerContext
		let(:protocol) {subject}
		
		with "http11 client" do
			it "should make a successful request" do
				response = client.get("/")
				expect(response).to be(:success?)
				expect(response.version).to be == "HTTP/1.1"
				response.read
			end
		end
		
		with "http2 client" do
			def make_client(endpoint, **options)
				options[:protocol] = Async::HTTP::Protocol::HTTP2
				super
			end
			
			it "should make a successful request" do
				response = client.get("/")
				expect(response).to be(:success?)
				expect(response.version).to be == "HTTP/2"
				response.read
			end
		end
	end
end