Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +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' |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 21 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 22 | describe 'JsonProtocol' do |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 23 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 24 | describe Thrift::JsonProtocol do |
| 25 | before(:each) do |
| 26 | @trans = Thrift::MemoryBufferTransport.new |
| 27 | @prot = Thrift::JsonProtocol.new(@trans) |
| 28 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 29 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 30 | it "should write json escaped char" do |
| 31 | @prot.write_json_escape_char("\n") |
| 32 | @trans.read(@trans.available).should == '\u000a' |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 33 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 34 | @prot.write_json_escape_char(" ") |
| 35 | @trans.read(@trans.available).should == '\u0020' |
| 36 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 37 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 38 | it "should write json char" do |
| 39 | @prot.write_json_char("\n") |
| 40 | @trans.read(@trans.available).should == '\\n' |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 41 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 42 | @prot.write_json_char(" ") |
| 43 | @trans.read(@trans.available).should == ' ' |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 44 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 45 | @prot.write_json_char("\\") |
| 46 | @trans.read(@trans.available).should == "\\\\" |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 47 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 48 | @prot.write_json_char("@") |
| 49 | @trans.read(@trans.available).should == '@' |
| 50 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 51 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 52 | it "should write json string" do |
| 53 | @prot.write_json_string("this is a \\ json\nstring") |
| 54 | @trans.read(@trans.available).should == "\"this is a \\\\ json\\nstring\"" |
| 55 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 56 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 57 | it "should write json base64" do |
| 58 | @prot.write_json_base64("this is a base64 string") |
| 59 | @trans.read(@trans.available).should == "\"\"dGhpcyBpcyBhIGJhc2U2NCBzdHJpbmc=\\n\"\"" |
| 60 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 61 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 62 | it "should write json integer" do |
| 63 | @prot.write_json_integer(45) |
| 64 | @trans.read(@trans.available).should == "45" |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 65 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 66 | @prot.write_json_integer(33000) |
| 67 | @trans.read(@trans.available).should == "33000" |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 68 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 69 | @prot.write_json_integer(3000000000) |
| 70 | @trans.read(@trans.available).should == "3000000000" |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 71 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 72 | @prot.write_json_integer(6000000000) |
| 73 | @trans.read(@trans.available).should == "6000000000" |
| 74 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 75 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 76 | it "should write json double" do |
| 77 | @prot.write_json_double(12.3) |
| 78 | @trans.read(@trans.available).should == "12.3" |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 79 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 80 | @prot.write_json_double(-3.21) |
| 81 | @trans.read(@trans.available).should == "-3.21" |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 82 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 83 | @prot.write_json_double(((+1.0/0.0)/(+1.0/0.0))) |
| 84 | @trans.read(@trans.available).should == "\"NaN\"" |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 85 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 86 | @prot.write_json_double((+1.0/0.0)) |
| 87 | @trans.read(@trans.available).should == "\"Infinity\"" |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 88 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 89 | @prot.write_json_double((-1.0/0.0)) |
| 90 | @trans.read(@trans.available).should == "\"-Infinity\"" |
| 91 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 92 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 93 | it "should write json object start" do |
| 94 | @prot.write_json_object_start |
| 95 | @trans.read(@trans.available).should == "{" |
| 96 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 97 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 98 | it "should write json object end" do |
| 99 | @prot.write_json_object_end |
| 100 | @trans.read(@trans.available).should == "}" |
| 101 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 102 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 103 | it "should write json array start" do |
| 104 | @prot.write_json_array_start |
| 105 | @trans.read(@trans.available).should == "[" |
| 106 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 107 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 108 | it "should write json array end" do |
| 109 | @prot.write_json_array_end |
| 110 | @trans.read(@trans.available).should == "]" |
| 111 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 112 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 113 | it "should write message begin" do |
| 114 | @prot.write_message_begin("name", 12, 32) |
| 115 | @trans.read(@trans.available).should == "[1,\"name\",12,32" |
| 116 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 117 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 118 | it "should write message end" do |
| 119 | @prot.write_message_end |
| 120 | @trans.read(@trans.available).should == "]" |
| 121 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 122 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 123 | it "should write struct begin" do |
| 124 | @prot.write_struct_begin("name") |
| 125 | @trans.read(@trans.available).should == "{" |
| 126 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 127 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 128 | it "should write struct end" do |
| 129 | @prot.write_struct_end |
| 130 | @trans.read(@trans.available).should == "}" |
| 131 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 132 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 133 | it "should write field begin" do |
| 134 | @prot.write_field_begin("name", Thrift::Types::STRUCT, 32) |
| 135 | @trans.read(@trans.available).should == "32{\"rec\"" |
| 136 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 137 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 138 | it "should write field end" do |
| 139 | @prot.write_field_end |
| 140 | @trans.read(@trans.available).should == "}" |
| 141 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 142 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 143 | it "should write field stop" do |
| 144 | @prot.write_field_stop |
| 145 | @trans.read(@trans.available).should == "" |
| 146 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 147 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 148 | it "should write map begin" do |
| 149 | @prot.write_map_begin(Thrift::Types::STRUCT, Thrift::Types::LIST, 32) |
| 150 | @trans.read(@trans.available).should == "[\"rec\",\"lst\",32,{" |
| 151 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 152 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 153 | it "should write map end" do |
| 154 | @prot.write_map_end |
| 155 | @trans.read(@trans.available).should == "}]" |
| 156 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 157 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 158 | it "should write list begin" do |
| 159 | @prot.write_list_begin(Thrift::Types::STRUCT, 32) |
| 160 | @trans.read(@trans.available).should == "[\"rec\",32" |
| 161 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 162 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 163 | it "should write list end" do |
| 164 | @prot.write_list_end |
| 165 | @trans.read(@trans.available).should == "]" |
| 166 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 167 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 168 | it "should write set begin" do |
| 169 | @prot.write_set_begin(Thrift::Types::STRUCT, 32) |
| 170 | @trans.read(@trans.available).should == "[\"rec\",32" |
| 171 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 172 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 173 | it "should write set end" do |
| 174 | @prot.write_set_end |
| 175 | @trans.read(@trans.available).should == "]" |
| 176 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 177 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 178 | it "should write bool" do |
| 179 | @prot.write_bool(true) |
| 180 | @trans.read(@trans.available).should == "1" |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 181 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 182 | @prot.write_bool(false) |
| 183 | @trans.read(@trans.available).should == "0" |
| 184 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 185 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 186 | it "should write byte" do |
| 187 | @prot.write_byte(100) |
| 188 | @trans.read(@trans.available).should == "100" |
| 189 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 190 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 191 | it "should write i16" do |
| 192 | @prot.write_i16(1000) |
| 193 | @trans.read(@trans.available).should == "1000" |
| 194 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 195 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 196 | it "should write i32" do |
| 197 | @prot.write_i32(3000000000) |
| 198 | @trans.read(@trans.available).should == "3000000000" |
| 199 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 200 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 201 | it "should write i64" do |
| 202 | @prot.write_i64(6000000000) |
| 203 | @trans.read(@trans.available).should == "6000000000" |
| 204 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 205 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 206 | it "should write double" do |
| 207 | @prot.write_double(1.23) |
| 208 | @trans.read(@trans.available).should == "1.23" |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 209 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 210 | @prot.write_double(-32.1) |
| 211 | @trans.read(@trans.available).should == "-32.1" |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 212 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 213 | @prot.write_double(((+1.0/0.0)/(+1.0/0.0))) |
| 214 | @trans.read(@trans.available).should == "\"NaN\"" |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 215 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 216 | @prot.write_double((+1.0/0.0)) |
| 217 | @trans.read(@trans.available).should == "\"Infinity\"" |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 218 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 219 | @prot.write_double((-1.0/0.0)) |
| 220 | @trans.read(@trans.available).should == "\"-Infinity\"" |
| 221 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 222 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 223 | it "should write string" do |
| 224 | @prot.write_string("this is a test string") |
| 225 | @trans.read(@trans.available).should == "\"this is a test string\"" |
| 226 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 227 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 228 | it "should write binary" do |
| 229 | @prot.write_binary("this is a base64 string") |
| 230 | @trans.read(@trans.available).should == "\"\"dGhpcyBpcyBhIGJhc2U2NCBzdHJpbmc=\\n\"\"" |
| 231 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 232 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 233 | it "should get type name for type id" do |
| 234 | expect {@prot.get_type_name_for_type_id(Thrift::Types::STOP)}.to raise_error(NotImplementedError) |
| 235 | expect {@prot.get_type_name_for_type_id(Thrift::Types::VOID)}.to raise_error(NotImplementedError) |
| 236 | @prot.get_type_name_for_type_id(Thrift::Types::BOOL).should == "tf" |
| 237 | @prot.get_type_name_for_type_id(Thrift::Types::BYTE).should == "i8" |
| 238 | @prot.get_type_name_for_type_id(Thrift::Types::DOUBLE).should == "dbl" |
| 239 | @prot.get_type_name_for_type_id(Thrift::Types::I16).should == "i16" |
| 240 | @prot.get_type_name_for_type_id(Thrift::Types::I32).should == "i32" |
| 241 | @prot.get_type_name_for_type_id(Thrift::Types::I64).should == "i64" |
| 242 | @prot.get_type_name_for_type_id(Thrift::Types::STRING).should == "str" |
| 243 | @prot.get_type_name_for_type_id(Thrift::Types::STRUCT).should == "rec" |
| 244 | @prot.get_type_name_for_type_id(Thrift::Types::MAP).should == "map" |
| 245 | @prot.get_type_name_for_type_id(Thrift::Types::SET).should == "set" |
| 246 | @prot.get_type_name_for_type_id(Thrift::Types::LIST).should == "lst" |
| 247 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 248 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 249 | it "should get type id for type name" do |
| 250 | expect {@prot.get_type_id_for_type_name("pp")}.to raise_error(NotImplementedError) |
| 251 | @prot.get_type_id_for_type_name("tf").should == Thrift::Types::BOOL |
| 252 | @prot.get_type_id_for_type_name("i8").should == Thrift::Types::BYTE |
| 253 | @prot.get_type_id_for_type_name("dbl").should == Thrift::Types::DOUBLE |
| 254 | @prot.get_type_id_for_type_name("i16").should == Thrift::Types::I16 |
| 255 | @prot.get_type_id_for_type_name("i32").should == Thrift::Types::I32 |
| 256 | @prot.get_type_id_for_type_name("i64").should == Thrift::Types::I64 |
| 257 | @prot.get_type_id_for_type_name("str").should == Thrift::Types::STRING |
| 258 | @prot.get_type_id_for_type_name("rec").should == Thrift::Types::STRUCT |
| 259 | @prot.get_type_id_for_type_name("map").should == Thrift::Types::MAP |
| 260 | @prot.get_type_id_for_type_name("set").should == Thrift::Types::SET |
| 261 | @prot.get_type_id_for_type_name("lst").should == Thrift::Types::LIST |
| 262 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 263 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 264 | it "should read json syntax char" do |
| 265 | @trans.write('F') |
| 266 | expect {@prot.read_json_syntax_char('G')}.to raise_error(Thrift::ProtocolException) |
| 267 | @trans.write('H') |
| 268 | @prot.read_json_syntax_char('H') |
| 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 read json escape char" do |
| 272 | @trans.write('0054') |
| 273 | @prot.read_json_escape_char.should == 'T' |
| 274 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 275 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 276 | it "should read json string" do |
| 277 | @trans.write("\"\\P") |
| 278 | expect {@prot.read_json_string(false)}.to raise_error(Thrift::ProtocolException) |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 279 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 280 | @trans.write("\"\\n\"") |
| 281 | @prot.read_json_string(false).should == "\\n" |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 282 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 283 | @trans.write("\"this is a test string\"") |
| 284 | @prot.read_json_string.should == "this is a test string" |
| 285 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 286 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 287 | it "should read json base64" do |
| 288 | @trans.write("\"dGhpcyBpcyBhIHRlc3Qgc3RyaW5n\"") |
| 289 | @prot.read_json_base64.should == "this is a test string" |
| 290 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 291 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 292 | it "should is json numeric" do |
| 293 | @prot.is_json_numeric("A").should == false |
| 294 | @prot.is_json_numeric("+").should == true |
| 295 | @prot.is_json_numeric("-").should == true |
| 296 | @prot.is_json_numeric(".").should == true |
| 297 | @prot.is_json_numeric("0").should == true |
| 298 | @prot.is_json_numeric("1").should == true |
| 299 | @prot.is_json_numeric("2").should == true |
| 300 | @prot.is_json_numeric("3").should == true |
| 301 | @prot.is_json_numeric("4").should == true |
| 302 | @prot.is_json_numeric("5").should == true |
| 303 | @prot.is_json_numeric("6").should == true |
| 304 | @prot.is_json_numeric("7").should == true |
| 305 | @prot.is_json_numeric("8").should == true |
| 306 | @prot.is_json_numeric("9").should == true |
| 307 | @prot.is_json_numeric("E").should == true |
| 308 | @prot.is_json_numeric("e").should == true |
| 309 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 310 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 311 | it "should read json numeric chars" do |
| 312 | @trans.write("1.453E45T") |
| 313 | @prot.read_json_numeric_chars.should == "1.453E45" |
| 314 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 315 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 316 | it "should read json integer" do |
| 317 | @trans.write("1.45\"\"") |
| 318 | expect {@prot.read_json_integer}.to raise_error(Thrift::ProtocolException) |
| 319 | @prot.read_string |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 320 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 321 | @trans.write("1453T") |
| 322 | @prot.read_json_integer.should == 1453 |
| 323 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 324 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 325 | it "should read json double" do |
| 326 | @trans.write("1.45e3e01\"\"") |
| 327 | expect {@prot.read_json_double}.to raise_error(Thrift::ProtocolException) |
| 328 | @prot.read_string |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 329 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 330 | @trans.write("\"1.453e01\"") |
| 331 | expect {@prot.read_json_double}.to raise_error(Thrift::ProtocolException) |
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 | @trans.write("1.453e01\"\"") |
| 334 | @prot.read_json_double.should == 14.53 |
| 335 | @prot.read_string |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 336 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 337 | @trans.write("\"NaN\"") |
| 338 | @prot.read_json_double.nan?.should == true |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 339 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 340 | @trans.write("\"Infinity\"") |
| 341 | @prot.read_json_double.should == +1.0/0.0 |
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("\"-Infinity\"") |
| 344 | @prot.read_json_double.should == -1.0/0.0 |
| 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 object start" do |
| 348 | @trans.write("{") |
| 349 | @prot.read_json_object_start.should == nil |
| 350 | end |
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 | it "should read json object end" do |
| 353 | @trans.write("}") |
| 354 | @prot.read_json_object_end.should == nil |
| 355 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 356 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 357 | it "should read json array start" do |
| 358 | @trans.write("[") |
| 359 | @prot.read_json_array_start.should == nil |
| 360 | end |
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 | it "should read json array end" do |
| 363 | @trans.write("]") |
| 364 | @prot.read_json_array_end.should == nil |
| 365 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 366 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 367 | it "should read_message_begin" do |
| 368 | @trans.write("[2,") |
| 369 | expect {@prot.read_message_begin}.to raise_error(Thrift::ProtocolException) |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 370 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 371 | @trans.write("[1,\"name\",12,32\"\"") |
| 372 | @prot.read_message_begin.should == ["name", 12, 32] |
| 373 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 374 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 375 | it "should read message end" do |
| 376 | @trans.write("]") |
| 377 | @prot.read_message_end.should == nil |
| 378 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 379 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 380 | it "should read struct begin" do |
| 381 | @trans.write("{") |
| 382 | @prot.read_struct_begin.should == nil |
| 383 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 384 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 385 | it "should read struct end" do |
| 386 | @trans.write("}") |
| 387 | @prot.read_struct_end.should == nil |
| 388 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 389 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 390 | it "should read field begin" do |
| 391 | @trans.write("1{\"rec\"") |
| 392 | @prot.read_field_begin.should == [nil, 12, 1] |
| 393 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 394 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 395 | it "should read field end" do |
| 396 | @trans.write("}") |
| 397 | @prot.read_field_end.should == nil |
| 398 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 399 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 400 | it "should read map begin" do |
| 401 | @trans.write("[\"rec\",\"lst\",2,{") |
| 402 | @prot.read_map_begin.should == [12, 15, 2] |
| 403 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 404 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 405 | it "should read map end" do |
| 406 | @trans.write("}]") |
| 407 | @prot.read_map_end.should == nil |
| 408 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 409 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 410 | it "should read list begin" do |
| 411 | @trans.write("[\"rec\",2\"\"") |
| 412 | @prot.read_list_begin.should == [12, 2] |
| 413 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 414 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 415 | it "should read list end" do |
| 416 | @trans.write("]") |
| 417 | @prot.read_list_end.should == nil |
| 418 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 419 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 420 | it "should read set begin" do |
| 421 | @trans.write("[") |
| 422 | @prot.read_set_begin.should == nil |
| 423 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 424 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 425 | it "should read set end" do |
| 426 | @trans.write("]") |
| 427 | @prot.read_set_end.should == nil |
| 428 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 429 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 430 | it "should read bool" do |
| 431 | @trans.write("0\"\"") |
| 432 | @prot.read_bool.should == false |
| 433 | @prot.read_string |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 434 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 435 | @trans.write("1\"\"") |
| 436 | @prot.read_bool.should == true |
| 437 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 438 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 439 | it "should read byte" do |
| 440 | @trans.write("60\"\"") |
| 441 | @prot.read_byte.should == 60 |
| 442 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 443 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 444 | it "should read i16" do |
| 445 | @trans.write("1000\"\"") |
| 446 | @prot.read_i16.should == 1000 |
| 447 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 448 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 449 | it "should read i32" do |
| 450 | @trans.write("3000000000\"\"") |
| 451 | @prot.read_i32.should == 3000000000 |
| 452 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 453 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 454 | it "should read i64" do |
| 455 | @trans.write("6000000000\"\"") |
| 456 | @prot.read_i64.should == 6000000000 |
| 457 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 458 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 459 | it "should read double" do |
| 460 | @trans.write("12.23\"\"") |
| 461 | @prot.read_double.should == 12.23 |
| 462 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 463 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 464 | it "should read string" do |
| 465 | @trans.write("\"this is a test string\"") |
| 466 | @prot.read_string.should == "this is a test string" |
| 467 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 468 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 469 | it "should read binary" do |
| 470 | @trans.write("\"dGhpcyBpcyBhIHRlc3Qgc3RyaW5n\"") |
| 471 | @prot.read_binary.should == "this is a test string" |
| 472 | end |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 473 | end |
| 474 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 475 | describe Thrift::JsonProtocolFactory do |
Jake Farrell | 6f0f527 | 2012-01-31 03:39:30 +0000 | [diff] [blame] | 476 | it "should create a JsonProtocol" do |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame^] | 477 | 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] | 478 | end |
| 479 | end |
| 480 | end |