File: wrapper.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 (121 lines) | stat: -rw-r--r-- 2,697 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
# frozen_string_literal: true

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

require "protocol/http/body/wrapper"
require "protocol/http/body/buffered"
require "protocol/http/request"

require "json"
require "stringio"

describe Protocol::HTTP::Body::Wrapper do
	let(:source) {Protocol::HTTP::Body::Buffered.new}
	let(:body) {subject.new(source)}
	
	with "#stream?" do
		it "should not be streamable" do
			expect(body).not.to be(:stream?)
		end
	end
	
	it "should proxy close" do
		expect(source).to receive(:close).and_return(nil)
		body.close
	end
	
	it "should proxy empty?" do
		expect(source).to receive(:empty?).and_return(true)
		expect(body.empty?).to be == true
	end
	
	it "should proxy ready?" do
		expect(source).to receive(:ready?).and_return(true)
		expect(body.ready?).to be == true
	end
	
	it "should proxy length" do
		expect(source).to receive(:length).and_return(1)
		expect(body.length).to be == 1
	end
	
	it "should proxy read" do
		expect(source).to receive(:read).and_return("!")
		expect(body.read).to be == "!"
	end
	
	it "should proxy inspect" do
		expect(source).to receive(:inspect).and_return("!")
		expect(body.inspect).to be(:include?, "!")
	end
	
	with ".wrap" do
		let(:message) {Protocol::HTTP::Request.new(nil, nil, "GET", "/", nil, Protocol::HTTP::Headers.new, body)}
		
		it "should wrap body" do
			subject.wrap(message)
			
			expect(message.body).to be_a(Protocol::HTTP::Body::Wrapper)
		end
	end
	
	with "#buffered" do
		it "should proxy buffered" do
			expect(source).to receive(:buffered).and_return(true)
			expect(body.buffered).to be == true
		end
	end
	
	with "#rewindable?" do
		it "should proxy rewindable?" do
			expect(source).to receive(:rewindable?).and_return(true)
			expect(body.rewindable?).to be == true
		end
	end
	
	with "#rewind" do
		it "should proxy rewind" do
			expect(source).to receive(:rewind).and_return(true)
			expect(body.rewind).to be == true
		end
	end
	
	with "#as_json" do
		it "generates a JSON representation" do
			expect(body.as_json).to have_keys(
				class: be == "Protocol::HTTP::Body::Wrapper",
				body: be == source.as_json
			)
		end
		
		it "generates a JSON string" do
			expect(JSON.dump(body)).to be == body.to_json
		end
	end
	
	with "#each" do
		it "should invoke close correctly" do
			expect(body).to receive(:close)
			
			body.each{}
		end
	end
	
	with "#stream" do
		let(:stream) {StringIO.new}
		
		it "should invoke close correctly" do
			expect(body).to receive(:close)
			
			body.call(stream)
		end
	end
	
	with "#discard" do
		it "should proxy discard" do
			expect(source).to receive(:discard).and_return(nil)
			expect(body.discard).to be_nil
		end
	end
end