Jake Farrell | b5a18a1 | 2012-10-09 01:10:43 +0000 | [diff] [blame] | 1 | # encoding: UTF-8 |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 2 | # |
| 3 | # Licensed to the Apache Software Foundation (ASF) under one |
| 4 | # or more contributor license agreements. See the NOTICE file |
| 5 | # distributed with this work for additional information |
| 6 | # regarding copyright ownership. The ASF licenses this file |
| 7 | # to you under the Apache License, Version 2.0 (the |
| 8 | # "License"); you may not use this file except in compliance |
| 9 | # with the License. You may obtain a copy of the License at |
| 10 | # |
| 11 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | # |
| 13 | # Unless required by applicable law or agreed to in writing, |
| 14 | # software distributed under the License is distributed on an |
| 15 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 | # KIND, either express or implied. See the License for the |
| 17 | # specific language governing permissions and limitations |
| 18 | # under the License. |
| 19 | # |
| 20 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 21 | require 'spec_helper' |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 22 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 23 | describe 'JsonProtocol' do |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 24 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 25 | describe Thrift::JsonProtocol do |
| 26 | before(:each) do |
| 27 | @trans = Thrift::MemoryBufferTransport.new |
| 28 | @prot = Thrift::JsonProtocol.new(@trans) |
| 29 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 30 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 31 | it "should write json escaped char" do |
| 32 | @prot.write_json_escape_char("\n") |
| 33 | @trans.read(@trans.available).should == '\u000a' |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 34 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 35 | @prot.write_json_escape_char(" ") |
| 36 | @trans.read(@trans.available).should == '\u0020' |
| 37 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 38 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 39 | it "should write json char" do |
| 40 | @prot.write_json_char("\n") |
| 41 | @trans.read(@trans.available).should == '\\n' |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 42 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 43 | @prot.write_json_char(" ") |
| 44 | @trans.read(@trans.available).should == ' ' |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 45 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 46 | @prot.write_json_char("\\") |
| 47 | @trans.read(@trans.available).should == "\\\\" |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 48 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 49 | @prot.write_json_char("@") |
| 50 | @trans.read(@trans.available).should == '@' |
| 51 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 52 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 53 | it "should write json string" do |
| 54 | @prot.write_json_string("this is a \\ json\nstring") |
| 55 | @trans.read(@trans.available).should == "\"this is a \\\\ json\\nstring\"" |
| 56 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 57 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 58 | it "should write json base64" do |
| 59 | @prot.write_json_base64("this is a base64 string") |
Jens Geyer | 123258b | 2015-10-02 00:38:17 +0200 | [diff] [blame] | 60 | @trans.read(@trans.available).should == "\"dGhpcyBpcyBhIGJhc2U2NCBzdHJpbmc=\"" |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 61 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 62 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 63 | it "should write json integer" do |
| 64 | @prot.write_json_integer(45) |
| 65 | @trans.read(@trans.available).should == "45" |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 66 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 67 | @prot.write_json_integer(33000) |
| 68 | @trans.read(@trans.available).should == "33000" |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 69 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 70 | @prot.write_json_integer(3000000000) |
| 71 | @trans.read(@trans.available).should == "3000000000" |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 72 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 73 | @prot.write_json_integer(6000000000) |
| 74 | @trans.read(@trans.available).should == "6000000000" |
| 75 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 76 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 77 | it "should write json double" do |
| 78 | @prot.write_json_double(12.3) |
| 79 | @trans.read(@trans.available).should == "12.3" |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 80 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 81 | @prot.write_json_double(-3.21) |
| 82 | @trans.read(@trans.available).should == "-3.21" |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 83 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 84 | @prot.write_json_double(((+1.0/0.0)/(+1.0/0.0))) |
| 85 | @trans.read(@trans.available).should == "\"NaN\"" |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 86 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 87 | @prot.write_json_double((+1.0/0.0)) |
| 88 | @trans.read(@trans.available).should == "\"Infinity\"" |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 89 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 90 | @prot.write_json_double((-1.0/0.0)) |
| 91 | @trans.read(@trans.available).should == "\"-Infinity\"" |
| 92 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 93 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 94 | it "should write json object start" do |
| 95 | @prot.write_json_object_start |
| 96 | @trans.read(@trans.available).should == "{" |
| 97 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 98 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 99 | it "should write json object end" do |
| 100 | @prot.write_json_object_end |
| 101 | @trans.read(@trans.available).should == "}" |
| 102 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 103 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 104 | it "should write json array start" do |
| 105 | @prot.write_json_array_start |
| 106 | @trans.read(@trans.available).should == "[" |
| 107 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 108 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 109 | it "should write json array end" do |
| 110 | @prot.write_json_array_end |
| 111 | @trans.read(@trans.available).should == "]" |
| 112 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 113 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 114 | it "should write message begin" do |
| 115 | @prot.write_message_begin("name", 12, 32) |
| 116 | @trans.read(@trans.available).should == "[1,\"name\",12,32" |
| 117 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 118 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 119 | it "should write message end" do |
| 120 | @prot.write_message_end |
| 121 | @trans.read(@trans.available).should == "]" |
| 122 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 123 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 124 | it "should write struct begin" do |
| 125 | @prot.write_struct_begin("name") |
| 126 | @trans.read(@trans.available).should == "{" |
| 127 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 128 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 129 | it "should write struct end" do |
| 130 | @prot.write_struct_end |
| 131 | @trans.read(@trans.available).should == "}" |
| 132 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 133 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 134 | it "should write field begin" do |
| 135 | @prot.write_field_begin("name", Thrift::Types::STRUCT, 32) |
| 136 | @trans.read(@trans.available).should == "32{\"rec\"" |
| 137 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 138 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 139 | it "should write field end" do |
| 140 | @prot.write_field_end |
| 141 | @trans.read(@trans.available).should == "}" |
| 142 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 143 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 144 | it "should write field stop" do |
| 145 | @prot.write_field_stop |
| 146 | @trans.read(@trans.available).should == "" |
| 147 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 148 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 149 | it "should write map begin" do |
| 150 | @prot.write_map_begin(Thrift::Types::STRUCT, Thrift::Types::LIST, 32) |
| 151 | @trans.read(@trans.available).should == "[\"rec\",\"lst\",32,{" |
| 152 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 153 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 154 | it "should write map end" do |
| 155 | @prot.write_map_end |
| 156 | @trans.read(@trans.available).should == "}]" |
| 157 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 158 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 159 | it "should write list begin" do |
| 160 | @prot.write_list_begin(Thrift::Types::STRUCT, 32) |
| 161 | @trans.read(@trans.available).should == "[\"rec\",32" |
| 162 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 163 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 164 | it "should write list end" do |
| 165 | @prot.write_list_end |
| 166 | @trans.read(@trans.available).should == "]" |
| 167 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 168 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 169 | it "should write set begin" do |
| 170 | @prot.write_set_begin(Thrift::Types::STRUCT, 32) |
| 171 | @trans.read(@trans.available).should == "[\"rec\",32" |
| 172 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 173 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 174 | it "should write set end" do |
| 175 | @prot.write_set_end |
| 176 | @trans.read(@trans.available).should == "]" |
| 177 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 178 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 179 | it "should write bool" do |
| 180 | @prot.write_bool(true) |
| 181 | @trans.read(@trans.available).should == "1" |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 182 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 183 | @prot.write_bool(false) |
| 184 | @trans.read(@trans.available).should == "0" |
| 185 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 186 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 187 | it "should write byte" do |
| 188 | @prot.write_byte(100) |
| 189 | @trans.read(@trans.available).should == "100" |
| 190 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 191 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 192 | it "should write i16" do |
| 193 | @prot.write_i16(1000) |
| 194 | @trans.read(@trans.available).should == "1000" |
| 195 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 196 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 197 | it "should write i32" do |
| 198 | @prot.write_i32(3000000000) |
| 199 | @trans.read(@trans.available).should == "3000000000" |
| 200 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 201 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 202 | it "should write i64" do |
| 203 | @prot.write_i64(6000000000) |
| 204 | @trans.read(@trans.available).should == "6000000000" |
| 205 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 206 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 207 | it "should write double" do |
| 208 | @prot.write_double(1.23) |
| 209 | @trans.read(@trans.available).should == "1.23" |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 210 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 211 | @prot.write_double(-32.1) |
| 212 | @trans.read(@trans.available).should == "-32.1" |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 213 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 214 | @prot.write_double(((+1.0/0.0)/(+1.0/0.0))) |
| 215 | @trans.read(@trans.available).should == "\"NaN\"" |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 216 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 217 | @prot.write_double((+1.0/0.0)) |
| 218 | @trans.read(@trans.available).should == "\"Infinity\"" |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 219 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 220 | @prot.write_double((-1.0/0.0)) |
| 221 | @trans.read(@trans.available).should == "\"-Infinity\"" |
| 222 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 223 | |
Jake Farrell | b5a18a1 | 2012-10-09 01:10:43 +0000 | [diff] [blame] | 224 | if RUBY_VERSION >= '1.9' |
| 225 | it 'should write string' do |
| 226 | @prot.write_string('this is a test string') |
| 227 | a = @trans.read(@trans.available) |
| 228 | a.should == '"this is a test string"'.force_encoding(Encoding::BINARY) |
| 229 | a.encoding.should == Encoding::BINARY |
| 230 | end |
| 231 | |
| 232 | it 'should write string with unicode characters' do |
| 233 | @prot.write_string("this is a test string with unicode characters: \u20AC \u20AD") |
| 234 | a = @trans.read(@trans.available) |
| 235 | a.should == "\"this is a test string with unicode characters: \u20AC \u20AD\"".force_encoding(Encoding::BINARY) |
| 236 | a.encoding.should == Encoding::BINARY |
| 237 | end |
| 238 | else |
| 239 | it 'should write string' do |
| 240 | @prot.write_string('this is a test string') |
| 241 | @trans.read(@trans.available).should == '"this is a test string"' |
| 242 | end |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 243 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 244 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 245 | it "should write binary" do |
| 246 | @prot.write_binary("this is a base64 string") |
Jens Geyer | 123258b | 2015-10-02 00:38:17 +0200 | [diff] [blame] | 247 | @trans.read(@trans.available).should == "\"dGhpcyBpcyBhIGJhc2U2NCBzdHJpbmc=\"" |
| 248 | end |
| 249 | |
| 250 | it "should write long binary" do |
| 251 | @prot.write_binary((0...256).to_a.pack('C*')) |
| 252 | @trans.read(@trans.available).should == "\"AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6/wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7/P3+/w==\"" |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 253 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 254 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 255 | it "should get type name for type id" do |
| 256 | expect {@prot.get_type_name_for_type_id(Thrift::Types::STOP)}.to raise_error(NotImplementedError) |
| 257 | expect {@prot.get_type_name_for_type_id(Thrift::Types::VOID)}.to raise_error(NotImplementedError) |
| 258 | @prot.get_type_name_for_type_id(Thrift::Types::BOOL).should == "tf" |
| 259 | @prot.get_type_name_for_type_id(Thrift::Types::BYTE).should == "i8" |
| 260 | @prot.get_type_name_for_type_id(Thrift::Types::DOUBLE).should == "dbl" |
| 261 | @prot.get_type_name_for_type_id(Thrift::Types::I16).should == "i16" |
| 262 | @prot.get_type_name_for_type_id(Thrift::Types::I32).should == "i32" |
| 263 | @prot.get_type_name_for_type_id(Thrift::Types::I64).should == "i64" |
| 264 | @prot.get_type_name_for_type_id(Thrift::Types::STRING).should == "str" |
| 265 | @prot.get_type_name_for_type_id(Thrift::Types::STRUCT).should == "rec" |
| 266 | @prot.get_type_name_for_type_id(Thrift::Types::MAP).should == "map" |
| 267 | @prot.get_type_name_for_type_id(Thrift::Types::SET).should == "set" |
| 268 | @prot.get_type_name_for_type_id(Thrift::Types::LIST).should == "lst" |
| 269 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 270 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 271 | it "should get type id for type name" do |
| 272 | expect {@prot.get_type_id_for_type_name("pp")}.to raise_error(NotImplementedError) |
| 273 | @prot.get_type_id_for_type_name("tf").should == Thrift::Types::BOOL |
| 274 | @prot.get_type_id_for_type_name("i8").should == Thrift::Types::BYTE |
| 275 | @prot.get_type_id_for_type_name("dbl").should == Thrift::Types::DOUBLE |
| 276 | @prot.get_type_id_for_type_name("i16").should == Thrift::Types::I16 |
| 277 | @prot.get_type_id_for_type_name("i32").should == Thrift::Types::I32 |
| 278 | @prot.get_type_id_for_type_name("i64").should == Thrift::Types::I64 |
| 279 | @prot.get_type_id_for_type_name("str").should == Thrift::Types::STRING |
| 280 | @prot.get_type_id_for_type_name("rec").should == Thrift::Types::STRUCT |
| 281 | @prot.get_type_id_for_type_name("map").should == Thrift::Types::MAP |
| 282 | @prot.get_type_id_for_type_name("set").should == Thrift::Types::SET |
| 283 | @prot.get_type_id_for_type_name("lst").should == Thrift::Types::LIST |
| 284 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 285 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 286 | it "should read json syntax char" do |
| 287 | @trans.write('F') |
| 288 | expect {@prot.read_json_syntax_char('G')}.to raise_error(Thrift::ProtocolException) |
| 289 | @trans.write('H') |
| 290 | @prot.read_json_syntax_char('H') |
| 291 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 292 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 293 | it "should read json escape char" do |
| 294 | @trans.write('0054') |
| 295 | @prot.read_json_escape_char.should == 'T' |
| 296 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 297 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 298 | it "should read json string" do |
| 299 | @trans.write("\"\\P") |
| 300 | expect {@prot.read_json_string(false)}.to raise_error(Thrift::ProtocolException) |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 301 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 302 | @trans.write("\"\\n\"") |
| 303 | @prot.read_json_string(false).should == "\\n" |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 304 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 305 | @trans.write("\"this is a test string\"") |
| 306 | @prot.read_json_string.should == "this is a test string" |
| 307 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 308 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 309 | it "should read json base64" do |
| 310 | @trans.write("\"dGhpcyBpcyBhIHRlc3Qgc3RyaW5n\"") |
| 311 | @prot.read_json_base64.should == "this is a test string" |
| 312 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 313 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 314 | it "should is json numeric" do |
| 315 | @prot.is_json_numeric("A").should == false |
| 316 | @prot.is_json_numeric("+").should == true |
| 317 | @prot.is_json_numeric("-").should == true |
| 318 | @prot.is_json_numeric(".").should == true |
| 319 | @prot.is_json_numeric("0").should == true |
| 320 | @prot.is_json_numeric("1").should == true |
| 321 | @prot.is_json_numeric("2").should == true |
| 322 | @prot.is_json_numeric("3").should == true |
| 323 | @prot.is_json_numeric("4").should == true |
| 324 | @prot.is_json_numeric("5").should == true |
| 325 | @prot.is_json_numeric("6").should == true |
| 326 | @prot.is_json_numeric("7").should == true |
| 327 | @prot.is_json_numeric("8").should == true |
| 328 | @prot.is_json_numeric("9").should == true |
| 329 | @prot.is_json_numeric("E").should == true |
| 330 | @prot.is_json_numeric("e").should == true |
| 331 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 332 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 333 | it "should read json numeric chars" do |
| 334 | @trans.write("1.453E45T") |
| 335 | @prot.read_json_numeric_chars.should == "1.453E45" |
| 336 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 337 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 338 | it "should read json integer" do |
| 339 | @trans.write("1.45\"\"") |
| 340 | expect {@prot.read_json_integer}.to raise_error(Thrift::ProtocolException) |
| 341 | @prot.read_string |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 342 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 343 | @trans.write("1453T") |
| 344 | @prot.read_json_integer.should == 1453 |
| 345 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 346 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 347 | it "should read json double" do |
| 348 | @trans.write("1.45e3e01\"\"") |
| 349 | expect {@prot.read_json_double}.to raise_error(Thrift::ProtocolException) |
| 350 | @prot.read_string |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 351 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 352 | @trans.write("\"1.453e01\"") |
| 353 | expect {@prot.read_json_double}.to raise_error(Thrift::ProtocolException) |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 354 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 355 | @trans.write("1.453e01\"\"") |
| 356 | @prot.read_json_double.should == 14.53 |
| 357 | @prot.read_string |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 358 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 359 | @trans.write("\"NaN\"") |
| 360 | @prot.read_json_double.nan?.should == true |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 361 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 362 | @trans.write("\"Infinity\"") |
| 363 | @prot.read_json_double.should == +1.0/0.0 |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 364 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 365 | @trans.write("\"-Infinity\"") |
| 366 | @prot.read_json_double.should == -1.0/0.0 |
| 367 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 368 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 369 | it "should read json object start" do |
| 370 | @trans.write("{") |
| 371 | @prot.read_json_object_start.should == nil |
| 372 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 373 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 374 | it "should read json object end" do |
| 375 | @trans.write("}") |
| 376 | @prot.read_json_object_end.should == nil |
| 377 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 378 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 379 | it "should read json array start" do |
| 380 | @trans.write("[") |
| 381 | @prot.read_json_array_start.should == nil |
| 382 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 383 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 384 | it "should read json array end" do |
| 385 | @trans.write("]") |
| 386 | @prot.read_json_array_end.should == nil |
| 387 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 388 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 389 | it "should read_message_begin" do |
| 390 | @trans.write("[2,") |
| 391 | expect {@prot.read_message_begin}.to raise_error(Thrift::ProtocolException) |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 392 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 393 | @trans.write("[1,\"name\",12,32\"\"") |
| 394 | @prot.read_message_begin.should == ["name", 12, 32] |
| 395 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 396 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 397 | it "should read message end" do |
| 398 | @trans.write("]") |
| 399 | @prot.read_message_end.should == nil |
| 400 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 401 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 402 | it "should read struct begin" do |
| 403 | @trans.write("{") |
| 404 | @prot.read_struct_begin.should == nil |
| 405 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 406 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 407 | it "should read struct end" do |
| 408 | @trans.write("}") |
| 409 | @prot.read_struct_end.should == nil |
| 410 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 411 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 412 | it "should read field begin" do |
| 413 | @trans.write("1{\"rec\"") |
| 414 | @prot.read_field_begin.should == [nil, 12, 1] |
| 415 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 416 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 417 | it "should read field end" do |
| 418 | @trans.write("}") |
| 419 | @prot.read_field_end.should == nil |
| 420 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 421 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 422 | it "should read map begin" do |
| 423 | @trans.write("[\"rec\",\"lst\",2,{") |
| 424 | @prot.read_map_begin.should == [12, 15, 2] |
| 425 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 426 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 427 | it "should read map end" do |
| 428 | @trans.write("}]") |
| 429 | @prot.read_map_end.should == nil |
| 430 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 431 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 432 | it "should read list begin" do |
| 433 | @trans.write("[\"rec\",2\"\"") |
| 434 | @prot.read_list_begin.should == [12, 2] |
| 435 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 436 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 437 | it "should read list end" do |
| 438 | @trans.write("]") |
| 439 | @prot.read_list_end.should == nil |
| 440 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 441 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 442 | it "should read set begin" do |
Roger Meier | 772b2b1 | 2013-01-19 21:04:12 +0100 | [diff] [blame] | 443 | @trans.write("[\"rec\",2\"\"") |
| 444 | @prot.read_set_begin.should == [12, 2] |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 445 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 446 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 447 | it "should read set end" do |
| 448 | @trans.write("]") |
| 449 | @prot.read_set_end.should == nil |
| 450 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 451 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 452 | it "should read bool" do |
| 453 | @trans.write("0\"\"") |
| 454 | @prot.read_bool.should == false |
| 455 | @prot.read_string |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 456 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 457 | @trans.write("1\"\"") |
| 458 | @prot.read_bool.should == true |
| 459 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 460 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 461 | it "should read byte" do |
| 462 | @trans.write("60\"\"") |
| 463 | @prot.read_byte.should == 60 |
| 464 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 465 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 466 | it "should read i16" do |
| 467 | @trans.write("1000\"\"") |
| 468 | @prot.read_i16.should == 1000 |
| 469 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 470 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 471 | it "should read i32" do |
| 472 | @trans.write("3000000000\"\"") |
| 473 | @prot.read_i32.should == 3000000000 |
| 474 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 475 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 476 | it "should read i64" do |
| 477 | @trans.write("6000000000\"\"") |
| 478 | @prot.read_i64.should == 6000000000 |
| 479 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 480 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 481 | it "should read double" do |
| 482 | @trans.write("12.23\"\"") |
| 483 | @prot.read_double.should == 12.23 |
| 484 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 485 | |
Jake Farrell | b5a18a1 | 2012-10-09 01:10:43 +0000 | [diff] [blame] | 486 | if RUBY_VERSION >= '1.9' |
| 487 | it 'should read string' do |
| 488 | @trans.write('"this is a test string"'.force_encoding(Encoding::BINARY)) |
| 489 | a = @prot.read_string |
| 490 | a.should == 'this is a test string' |
| 491 | a.encoding.should == Encoding::UTF_8 |
| 492 | end |
| 493 | |
| 494 | it 'should read string with unicode characters' do |
| 495 | @trans.write('"this is a test string with unicode characters: \u20AC \u20AD"'.force_encoding(Encoding::BINARY)) |
| 496 | a = @prot.read_string |
| 497 | a.should == "this is a test string with unicode characters: \u20AC \u20AD" |
| 498 | a.encoding.should == Encoding::UTF_8 |
| 499 | end |
| 500 | else |
| 501 | it 'should read string' do |
| 502 | @trans.write('"this is a test string"') |
| 503 | @prot.read_string.should == 'this is a test string' |
| 504 | end |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 505 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 506 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 507 | it "should read binary" do |
| 508 | @trans.write("\"dGhpcyBpcyBhIHRlc3Qgc3RyaW5n\"") |
| 509 | @prot.read_binary.should == "this is a test string" |
| 510 | end |
Jens Geyer | 123258b | 2015-10-02 00:38:17 +0200 | [diff] [blame] | 511 | |
| 512 | it "should read long binary" do |
| 513 | @trans.write("\"AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6/wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7/P3+/w==\"") |
| 514 | @prot.read_binary.bytes.to_a.should == (0...256).to_a |
| 515 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 516 | end |
| 517 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 518 | describe Thrift::JsonProtocolFactory do |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 519 | it "should create a JsonProtocol" do |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 520 | Thrift::JsonProtocolFactory.new.get_protocol(mock("MockTransport")).should be_instance_of(Thrift::JsonProtocol) |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 521 | end |
| 522 | end |
| 523 | end |