File: rewindable.rb

package info (click to toggle)
ruby-protocol-http 0.59.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 864 kB
  • sloc: ruby: 7,612; makefile: 4
file content (117 lines) | stat: -rw-r--r-- 2,693 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
# frozen_string_literal: true

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

require "protocol/http/body/rewindable"
require "protocol/http/request"

describe Protocol::HTTP::Body::Rewindable do
	let(:source) {Protocol::HTTP::Body::Buffered.new}
	let(:body) {subject.new(source)}
	
	it "can write and read data" do
		3.times do |i|
			source.write("Hello World #{i}")
			expect(body.read).to be == "Hello World #{i}"
		end
	end
	
	it "can write and read data multiple times" do
		3.times do |i|
			source.write("Hello World #{i}")
		end
		
		3.times do
			body.rewind
			
			expect(body).to be(:ready?)
			expect(body.read).to be == "Hello World 0"
		end
	end
	
	it "can buffer data in order" do
		3.times do |i|
			source.write("Hello World #{i}")
		end
		
		2.times do
			body.rewind
			
			3.times do |i|
				expect(body.read).to be == "Hello World #{i}"
			end
		end
	end
	
	with ".wrap" do
		with "a buffered body" do
			let(:body) {Protocol::HTTP::Body::Buffered.new}
			let(:message) {Protocol::HTTP::Request.new(nil, nil, "GET", "/", nil, Protocol::HTTP::Headers.new, body)}
			
			it "returns the body" do
				expect(subject.wrap(message)).to be == body
			end
		end
		
		with "a non-rewindable body" do
			let(:body) {Protocol::HTTP::Body::Readable.new}
			let(:message) {Protocol::HTTP::Request.new(nil, nil, "GET", "/", nil, Protocol::HTTP::Headers.new, body)}
			
			it "returns a new rewindable body" do
				expect(subject.wrap(message)).to be_a(Protocol::HTTP::Body::Rewindable)
			end
		end
	end
	
	with "#buffered" do
		it "can generate buffered representation" do
			3.times do |i|
				source.write("Hello World #{i}")
			end
			
			expect(body.buffered).to be(:empty?)
			
			# Read one chunk into the internal buffer:
			body.read
			
			expect(body.buffered.chunks).to be == ["Hello World 0"]
		end
	end
	
	with "#empty?" do
		it "can read and re-read the body" do
			source.write("Hello World")
			expect(body).not.to be(:empty?)
			
			expect(body.read).to be == "Hello World"
			expect(body).to be(:empty?)
			
			body.rewind
			expect(body.read).to be == "Hello World"
			expect(body).to be(:empty?)
		end
	end
	
	with "#rewindable?" do
		it "is rewindable" do
			expect(body).to be(:rewindable?)
		end
	end
	
	with "#inspect" do
		it "can generate string representation" do
			expect(body.inspect).to be == "#<Protocol::HTTP::Body::Buffered empty> | #<Protocol::HTTP::Body::Rewindable 0/0 chunks read>"
		end
	end
	
	with "#as_json" do
		it "includes rewind tracking information" do
			expect(body.as_json).to have_keys(
				class: be == "Protocol::HTTP::Body::Rewindable",
				index: be == 0,
				chunks: be == 0
			)
		end
	end
end