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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
|
# encoding: binary
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe 'Incoming frame draft 07' do
subject { frame }
let(:version) { 7 }
let(:frame) { WebSocket::Frame::Incoming.new(version: version, data: encoded_text) }
let(:encoded_text) { nil }
let(:decoded_text) { nil }
let(:frame_type) { nil }
let(:error) { nil }
it_behaves_like 'valid_incoming_frame'
context 'should properly decode close frame' do
let(:encoded_text) { "\x88\x07\x03\xE8" + decoded_text }
let(:frame_type) { :close }
let(:decoded_text) { 'Hello' }
let(:close_code) { 1000 }
it_behaves_like 'valid_incoming_frame'
end
context 'should raise error with invalid close code' do
let(:encoded_text) { "\x88\x07\x03\xEDHello" }
let(:decoded_text) { nil }
let(:error) { WebSocket::Error::Frame::UnknownCloseCode }
it_behaves_like 'valid_incoming_frame'
end
context 'should properly decode close frame with invalid UTF-8 message' do
let(:encoded_text) { "\x88\x03\x03\xE8\xE3" }
let(:decoded_text) { nil }
let(:error) { WebSocket::Error::Frame::InvalidPayloadEncoding }
it_behaves_like 'valid_incoming_frame'
end
context 'should properly decode ping frame' do
let(:encoded_text) { "\x89\x05" + decoded_text }
let(:frame_type) { :ping }
let(:decoded_text) { 'Hello' }
it_behaves_like 'valid_incoming_frame'
end
context 'should properly decode pong frame' do
let(:encoded_text) { "\x8a\x05" + decoded_text }
let(:frame_type) { :pong }
let(:decoded_text) { 'Hello' }
it_behaves_like 'valid_incoming_frame'
end
context 'should properly decode text frame' do
let(:encoded_text) { "\x81\x05\x48\x65\x6c\x6c\x6f" }
let(:decoded_text) { 'Hello' }
let(:frame_type) { :text }
it_behaves_like 'valid_incoming_frame'
end
context 'should properly decode masked text frame' do
let(:encoded_text) { "\x81\x85\x37\xfa\x21\x3d\x7f\x9f\x4d\x51\x58" }
let(:decoded_text) { 'Hello' }
let(:frame_type) { :text }
it_behaves_like 'valid_incoming_frame'
end
context 'should properly decode text frame with continuation' do
let(:encoded_text) { "\x01\x03Hel\x80\x02lo" }
let(:frame_type) { :text }
let(:decoded_text) { 'Hello' }
it_behaves_like 'valid_incoming_frame'
end
context 'should properly decode masked text frame with continuation' do
let(:encoded_text) { "\x01\x83\x37\xfa\x21\x3d\x7f\x9f\x4d\x80\x82\x37\xfa\x21\x3d\x5b\x95" }
let(:frame_type) { :text }
let(:decoded_text) { 'Hello' }
it_behaves_like 'valid_incoming_frame'
end
context 'should properly decode text frame in between of continuation' do
let(:encoded_text) { "\x01\x03Hel\x8a\x03abc\x80\x02lo" }
let(:frame_type) { %i[pong text] }
let(:decoded_text) { %w[abc Hello] }
it_behaves_like 'valid_incoming_frame'
end
context 'should not return unfinished more frame' do
let(:encoded_text) { "\x01\x03Hel\x8a\x03abc" }
let(:frame_type) { :pong }
let(:decoded_text) { 'abc' }
it_behaves_like 'valid_incoming_frame'
end
context 'should properly decode 256 bytes binary frame' do
let(:encoded_text) { "\x82\x7E\x01\x00" + decoded_text }
let(:frame_type) { :binary }
let(:decoded_text) { 'a' * 256 }
it_behaves_like 'valid_incoming_frame'
end
context 'should properly decode 64KiB binary frame' do
let(:encoded_text) { "\x82\x7F\x00\x00\x00\x00\x00\x01\x00\x00" + decoded_text }
let(:frame_type) { :binary }
let(:decoded_text) { 'a' * 65_536 }
it_behaves_like 'valid_incoming_frame'
end
context 'should wait with incomplete frame' do
let(:encoded_text) { "\x81\x06Hello" }
let(:decoded_text) { nil }
it_behaves_like 'valid_incoming_frame'
end
context 'should raise error with invalid opcode' do
let(:encoded_text) { "\x85\x05Hello" }
let(:decoded_text) { nil }
let(:error) { WebSocket::Error::Frame::UnknownOpcode }
it_behaves_like 'valid_incoming_frame'
end
context 'should raise error with too long frame' do
let(:encoded_text) { "\x81\x7F" + 'a' * WebSocket.max_frame_size }
let(:decoded_text) { nil }
let(:error) { WebSocket::Error::Frame::TooLong }
it_behaves_like 'valid_incoming_frame'
end
context 'should raise error with continuation frame without more frame earlier' do
let(:encoded_text) { "\x80\x05Hello" }
let(:decoded_text) { nil }
let(:error) { WebSocket::Error::Frame::UnexpectedContinuationFrame }
it_behaves_like 'valid_incoming_frame'
end
end
|