Files
James Coglan 9ce857b3d4 Revise uses of encoding APIs.
When originally implemented, we still supported Ruby 1.8, which
necessitated checking for encoding methods and using a regex to validate
UTF-8. These checks are now gone.

We tagged many strings as binary when not strictly necessary, either
because we were just going to iterate their bytes or because we were
going to hand them off to the caller which should just write them
directly to a socket. Strings used as buffers to accumulate streaming
input are still tagged as binary to avoid encoding
collision/conversion.

The places where we do need to tag as UTF-8 (i.e. just before emitting
to the application) remain, but copy the string if necessary. This
allows us to work with frozen strings.

Finally, strings passed in via the Driver#text method should be
*transcoded* to UTF-8 if necessary, not merely tagged. The Ruby
String#encode method produces a new string so this should also be safe
with frozen strings.
2016-05-19 21:08:22 +01:00

105 lines
2.8 KiB
Ruby

# encoding=utf-8
require "spec_helper"
describe WebSocket::Driver::Draft75 do
include EncodingHelper
let :env do
{
"REQUEST_METHOD" => "GET",
"HTTP_CONNECTION" => "Upgrade",
"HTTP_UPGRADE" => "WebSocket",
"HTTP_ORIGIN" => "http://www.example.com"
}
end
let :socket do
socket = double(WebSocket)
allow(socket).to receive(:env).and_return(env)
allow(socket).to receive(:url).and_return("ws://www.example.com/socket")
allow(socket).to receive(:write) { |message| @bytes = bytes(message) }
socket
end
let :driver do
driver = WebSocket::Driver::Draft75.new(socket)
driver.on(:open) { |e| @open = true }
driver.on(:message) { |e| @message += e.data }
driver.on(:close) { |e| @close = true }
driver
end
before do
@open = @close = false
@message = ""
end
describe "in the :connecting state" do
it "starts in the :connecting state" do
expect(driver.state).to eq :connecting
end
describe :start do
it "writes the handshake response to the socket" do
expect(socket).to receive(:write).with(
"HTTP/1.1 101 Web Socket Protocol Handshake\r\n" +
"Upgrade: WebSocket\r\n" +
"Connection: Upgrade\r\n" +
"WebSocket-Origin: http://www.example.com\r\n" +
"WebSocket-Location: ws://www.example.com/socket\r\n" +
"\r\n")
driver.start
end
it "returns true" do
expect(driver.start).to eq true
end
it "triggers the onopen event" do
driver.start
expect(@open).to eq true
end
it "changes the state to :open" do
driver.start
expect(driver.state).to eq :open
end
it "sets the protocol version" do
driver.start
expect(driver.version).to eq "hixie-75"
end
end
describe :frame do
it "does not write to the socket" do
expect(socket).not_to receive(:write)
driver.frame("Hello, world")
end
it "returns true" do
expect(driver.frame("whatever")).to eq true
end
it "queues the frames until the handshake has been sent" do
expect(socket).to receive(:write).with(
"HTTP/1.1 101 Web Socket Protocol Handshake\r\n" +
"Upgrade: WebSocket\r\n" +
"Connection: Upgrade\r\n" +
"WebSocket-Origin: http://www.example.com\r\n" +
"WebSocket-Location: ws://www.example.com/socket\r\n" +
"\r\n")
expect(socket).to receive(:write).with(WebSocket::Driver.encode "\x00Hi\xFF", WebSocket::Driver::BINARY)
driver.frame("Hi")
driver.start
expect(@bytes).to eq [0x00, 72, 105, 0xFF]
end
end
end
it_should_behave_like "draft-75 protocol"
end