David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 1 | # |
| 2 | # Licensed to the Apache Software Foundation (ASF) under one |
| 3 | # or more contributor license agreements. See the NOTICE file |
| 4 | # distributed with this work for additional information |
| 5 | # regarding copyright ownership. The ASF licenses this file |
| 6 | # to you under the Apache License, Version 2.0 (the |
| 7 | # "License"); you may not use this file except in compliance |
| 8 | # with the License. You may obtain a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, |
| 13 | # software distributed under the License is distributed on an |
| 14 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | # KIND, either express or implied. See the License for the |
| 16 | # specific language governing permissions and limitations |
| 17 | # under the License. |
| 18 | # |
| 19 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 20 | require 'spec_helper' |
Kevin Clark | 138c0e1 | 2008-06-18 01:18:28 +0000 | [diff] [blame] | 21 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 22 | describe 'Serializer' do |
Kevin Clark | 138c0e1 | 2008-06-18 01:18:28 +0000 | [diff] [blame] | 23 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 24 | describe Thrift::Serializer do |
Kevin Clark | 138c0e1 | 2008-06-18 01:18:28 +0000 | [diff] [blame] | 25 | it "should serialize structs to binary by default" do |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 26 | serializer = Thrift::Serializer.new(Thrift::BinaryProtocolAcceleratedFactory.new) |
| 27 | data = serializer.serialize(SpecNamespace::Hello.new(:greeting => "'Ello guv'nor!")) |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 28 | expect(data).to eq("\x0B\x00\x01\x00\x00\x00\x0E'Ello guv'nor!\x00") |
Kevin Clark | 138c0e1 | 2008-06-18 01:18:28 +0000 | [diff] [blame] | 29 | end |
| 30 | |
| 31 | it "should serialize structs to the given protocol" do |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 32 | protocol = Thrift::BaseProtocol.new(double("transport")) |
| 33 | expect(protocol).to receive(:write_struct_begin).with("SpecNamespace::Hello") |
| 34 | expect(protocol).to receive(:write_field_begin).with("greeting", Thrift::Types::STRING, 1) |
| 35 | expect(protocol).to receive(:write_string).with("Good day") |
| 36 | expect(protocol).to receive(:write_field_end) |
| 37 | expect(protocol).to receive(:write_field_stop) |
| 38 | expect(protocol).to receive(:write_struct_end) |
| 39 | protocol_factory = double("ProtocolFactory") |
| 40 | allow(protocol_factory).to receive(:get_protocol).and_return(protocol) |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 41 | serializer = Thrift::Serializer.new(protocol_factory) |
| 42 | serializer.serialize(SpecNamespace::Hello.new(:greeting => "Good day")) |
Kevin Clark | 138c0e1 | 2008-06-18 01:18:28 +0000 | [diff] [blame] | 43 | end |
| 44 | end |
| 45 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 46 | describe Thrift::Deserializer do |
Kevin Clark | 138c0e1 | 2008-06-18 01:18:28 +0000 | [diff] [blame] | 47 | it "should deserialize structs from binary by default" do |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 48 | deserializer = Thrift::Deserializer.new |
Kevin Clark | 138c0e1 | 2008-06-18 01:18:28 +0000 | [diff] [blame] | 49 | data = "\x0B\x00\x01\x00\x00\x00\x0E'Ello guv'nor!\x00" |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 50 | expect(deserializer.deserialize(SpecNamespace::Hello.new, data)).to eq(SpecNamespace::Hello.new(:greeting => "'Ello guv'nor!")) |
Kevin Clark | 138c0e1 | 2008-06-18 01:18:28 +0000 | [diff] [blame] | 51 | end |
| 52 | |
| 53 | it "should deserialize structs from the given protocol" do |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 54 | protocol = Thrift::BaseProtocol.new(double("transport")) |
| 55 | expect(protocol).to receive(:read_struct_begin).and_return("SpecNamespace::Hello") |
| 56 | expect(protocol).to receive(:read_field_begin).and_return(["greeting", Thrift::Types::STRING, 1], |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 57 | [nil, Thrift::Types::STOP, 0]) |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 58 | expect(protocol).to receive(:read_string).and_return("Good day") |
| 59 | expect(protocol).to receive(:read_field_end) |
| 60 | expect(protocol).to receive(:read_struct_end) |
| 61 | protocol_factory = double("ProtocolFactory") |
| 62 | allow(protocol_factory).to receive(:get_protocol).and_return(protocol) |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 63 | deserializer = Thrift::Deserializer.new(protocol_factory) |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 64 | expect(deserializer.deserialize(SpecNamespace::Hello.new, "")).to eq(SpecNamespace::Hello.new(:greeting => "Good day")) |
Kevin Clark | 138c0e1 | 2008-06-18 01:18:28 +0000 | [diff] [blame] | 65 | end |
| 66 | end |
| 67 | end |