File: head.rb

package info (click to toggle)
ruby-protocol-http 0.55.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 840 kB
  • sloc: ruby: 6,904; makefile: 4
file content (78 lines) | stat: -rw-r--r-- 1,529 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
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2023-2025, by Samuel Williams.

require "protocol/http/body/head"
require "protocol/http/body/buffered"

describe Protocol::HTTP::Body::Head do
	with "zero length" do
		let(:body) {subject.new(0)}
		
		it "should be ready" do
			expect(body).to be(:ready?)
		end
		
		it "should be empty" do
			expect(body).to be(:empty?)
		end
		
		with "#join" do
			it "should be nil" do
				expect(body.join).to be_nil
			end
		end
	end
	
	with "non-zero length" do
		let(:body) {subject.new(1)}
		
		it "should be empty" do
			expect(body).to be(:empty?)
		end
		
		with "#read" do
			it "should be nil" do
				expect(body.join).to be_nil
			end
		end
		
		with "#join" do
			it "should be nil" do
				expect(body.join).to be_nil
			end
		end
	end
	
	with ".for" do
		with "body" do
			let(:source) {Protocol::HTTP::Body::Buffered.wrap("!")}
			let(:body) {subject.for(source)}
			
			it "captures length and closes existing body" do
				expect(source).to receive(:close)
				
				expect(body).to have_attributes(length: be == 1)
				body.close
			end
		end
		
		with "content length" do
			let(:body) {subject.for(nil, 42)}
			
			it "uses the content length if no body is provided" do
				expect(body).to have_attributes(length: be == 42)
				expect(body).to be(:empty?)
				expect(body).to be(:ready?)
			end
		end
	end
	
	with ".for with nil body" do
		it "returns nil when body is nil" do
			body = subject.for(nil)
			expect(body).to be_nil
		end
	end
end