File: file.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 (129 lines) | stat: -rw-r--r-- 2,608 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
# frozen_string_literal: true

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

require "protocol/http/body/file"

describe Protocol::HTTP::Body::File do
	let(:path) {File.expand_path("file_spec.txt", __dir__)}
	let(:body) {subject.open(path)}
	
	after do
		@body&.close
	end
	
	# with '#stream?' do
	# 	it "should be streamable" do
	# 		expect(body).to be(:stream?)
	# 	end
	# end
	
	with "#join" do
		it "should read entire file" do
			expect(body.join).to be == "Hello World"
		end
	end
	
	with "#close" do
		it "should close file" do
			body.close
			
			expect(body).to be(:empty?)
			expect(body.file).to be(:closed?)
		end
	end
	
	with "#rewindable?" do
		it "should be rewindable" do
			expect(body).to be(:rewindable?)
		end
	end
	
	with "#rewind" do
		it "should rewind file" do
			expect(body.read).to be == "Hello World"
			expect(body).to be(:empty?)
			
			body.rewind
			
			expect(body).not.to be(:empty?)
			expect(body.read).to be == "Hello World"
		end
	end
	
	with "#buffered" do
		it "should return a new instance" do
			buffered = body.buffered
			
			expect(buffered).to be_a(Protocol::HTTP::Body::File)
			expect(buffered).not.to be_equal(body)
		ensure
			buffered&.close
		end
	end
	
	with "#inspect" do
		it "generates a string representation" do
			expect(body.inspect).to be =~ /Protocol::HTTP::Body::File (.*?), \d+ bytes remaining/
		end
		
		with "range" do
			let(:body) {subject.new(File.open(path), 5..10)}
			
			it "shows offset when present" do
				expect(body.inspect).to be =~ /Protocol::HTTP::Body::File (.*?) \+5, \d+ bytes remaining/
			end
		end
	end
	
	with "entire file" do
		it "should read entire file" do
			expect(body.read).to be == "Hello World"
		end
		
		it "should use binary encoding" do
			expect(::File).to receive(:open).with(path, ::File::RDONLY | ::File::BINARY)
			
			chunk = body.read
			
			expect(chunk.encoding).to be == Encoding::BINARY
		end
		
		with "#ready?" do
			it "should be ready" do
				expect(body).to be(:ready?)
			end
		end
	end
	
	with "partial file" do
		let(:body) {subject.open(path, 2...4)}
		
		it "should read specified range" do
			expect(body.read).to be == "ll"
		end
	end
	
	with "#call" do
		let(:output) {StringIO.new}
		
		it "can stream output" do
			body.call(output)
			
			expect(output.string).to be == "Hello World"
		end
		
		with "/dev/zero" do
			it "can stream partial output" do
				skip unless File.exist?("/dev/zero")
				
				body = subject.open("/dev/zero", 0...10)
				
				body.call(output)
				
				expect(output.string).to be == "\x00" * 10
			end
		end
	end
end