simple_json_protocol_test.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  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. package thrift
  20. import (
  21. "encoding/base64"
  22. "encoding/json"
  23. "fmt"
  24. "math"
  25. "strconv"
  26. "strings"
  27. "testing"
  28. )
  29. func TestWriteSimpleJSONProtocolBool(t *testing.T) {
  30. thetype := "boolean"
  31. trans := NewTMemoryBuffer()
  32. p := NewTSimpleJSONProtocol(trans)
  33. for _, value := range BOOL_VALUES {
  34. if e := p.WriteBool(value); e != nil {
  35. t.Fatalf("Unable to write %s value %v due to error: %s", thetype, value, e.Error())
  36. }
  37. if e := p.Flush(); e != nil {
  38. t.Fatalf("Unable to write %s value %v due to error flushing: %s", thetype, value, e.Error())
  39. }
  40. s := trans.String()
  41. if s != fmt.Sprint(value) {
  42. t.Fatalf("Bad value for %s %v: %s", thetype, value, s)
  43. }
  44. v := false
  45. if err := json.Unmarshal([]byte(s), &v); err != nil || v != value {
  46. t.Fatalf("Bad json-decoded value for %s %v, wrote: '%s', expected: '%v'", thetype, value, s, v)
  47. }
  48. trans.Reset()
  49. }
  50. trans.Close()
  51. }
  52. func TestReadSimpleJSONProtocolBool(t *testing.T) {
  53. thetype := "boolean"
  54. for _, value := range BOOL_VALUES {
  55. trans := NewTMemoryBuffer()
  56. p := NewTSimpleJSONProtocol(trans)
  57. if value {
  58. trans.Write(JSON_TRUE)
  59. } else {
  60. trans.Write(JSON_FALSE)
  61. }
  62. trans.Flush()
  63. s := trans.String()
  64. v, e := p.ReadBool()
  65. if e != nil {
  66. t.Fatalf("Unable to read %s value %v due to error: %s", thetype, value, e.Error())
  67. }
  68. if v != value {
  69. t.Fatalf("Bad value for %s value %v, wrote: %v, received: %v", thetype, value, s, v)
  70. }
  71. if err := json.Unmarshal([]byte(s), &v); err != nil || v != value {
  72. t.Fatalf("Bad json-decoded value for %s %v, wrote: '%s', expected: '%v'", thetype, value, s, v)
  73. }
  74. trans.Reset()
  75. trans.Close()
  76. }
  77. }
  78. func TestWriteSimpleJSONProtocolByte(t *testing.T) {
  79. thetype := "byte"
  80. trans := NewTMemoryBuffer()
  81. p := NewTSimpleJSONProtocol(trans)
  82. for _, value := range BYTE_VALUES {
  83. if e := p.WriteByte(value); e != nil {
  84. t.Fatalf("Unable to write %s value %v due to error: %s", thetype, value, e.Error())
  85. }
  86. if e := p.Flush(); e != nil {
  87. t.Fatalf("Unable to write %s value %v due to error flushing: %s", thetype, value, e.Error())
  88. }
  89. s := trans.String()
  90. if s != fmt.Sprint(value) {
  91. t.Fatalf("Bad value for %s %v: %s", thetype, value, s)
  92. }
  93. v := byte(0)
  94. if err := json.Unmarshal([]byte(s), &v); err != nil || v != value {
  95. t.Fatalf("Bad json-decoded value for %s %v, wrote: '%s', expected: '%v'", thetype, value, s, v)
  96. }
  97. trans.Reset()
  98. }
  99. trans.Close()
  100. }
  101. func TestReadSimpleJSONProtocolByte(t *testing.T) {
  102. thetype := "byte"
  103. for _, value := range BYTE_VALUES {
  104. trans := NewTMemoryBuffer()
  105. p := NewTSimpleJSONProtocol(trans)
  106. trans.WriteString(strconv.Itoa(int(value)))
  107. trans.Flush()
  108. s := trans.String()
  109. v, e := p.ReadByte()
  110. if e != nil {
  111. t.Fatalf("Unable to read %s value %v due to error: %s", thetype, value, e.Error())
  112. }
  113. if v != value {
  114. t.Fatalf("Bad value for %s value %v, wrote: %v, received: %v", thetype, value, s, v)
  115. }
  116. if err := json.Unmarshal([]byte(s), &v); err != nil || v != value {
  117. t.Fatalf("Bad json-decoded value for %s %v, wrote: '%s', expected: '%v'", thetype, value, s, v)
  118. }
  119. trans.Reset()
  120. trans.Close()
  121. }
  122. }
  123. func TestWriteSimpleJSONProtocolI16(t *testing.T) {
  124. thetype := "int16"
  125. trans := NewTMemoryBuffer()
  126. p := NewTSimpleJSONProtocol(trans)
  127. for _, value := range INT16_VALUES {
  128. if e := p.WriteI16(value); e != nil {
  129. t.Fatalf("Unable to write %s value %v due to error: %s", thetype, value, e.Error())
  130. }
  131. if e := p.Flush(); e != nil {
  132. t.Fatalf("Unable to write %s value %v due to error flushing: %s", thetype, value, e.Error())
  133. }
  134. s := trans.String()
  135. if s != fmt.Sprint(value) {
  136. t.Fatalf("Bad value for %s %v: %s", thetype, value, s)
  137. }
  138. v := int16(0)
  139. if err := json.Unmarshal([]byte(s), &v); err != nil || v != value {
  140. t.Fatalf("Bad json-decoded value for %s %v, wrote: '%s', expected: '%v'", thetype, value, s, v)
  141. }
  142. trans.Reset()
  143. }
  144. trans.Close()
  145. }
  146. func TestReadSimpleJSONProtocolI16(t *testing.T) {
  147. thetype := "int16"
  148. for _, value := range INT16_VALUES {
  149. trans := NewTMemoryBuffer()
  150. p := NewTSimpleJSONProtocol(trans)
  151. trans.WriteString(strconv.Itoa(int(value)))
  152. trans.Flush()
  153. s := trans.String()
  154. v, e := p.ReadI16()
  155. if e != nil {
  156. t.Fatalf("Unable to read %s value %v due to error: %s", thetype, value, e.Error())
  157. }
  158. if v != value {
  159. t.Fatalf("Bad value for %s value %v, wrote: %v, received: %v", thetype, value, s, v)
  160. }
  161. if err := json.Unmarshal([]byte(s), &v); err != nil || v != value {
  162. t.Fatalf("Bad json-decoded value for %s %v, wrote: '%s', expected: '%v'", thetype, value, s, v)
  163. }
  164. trans.Reset()
  165. trans.Close()
  166. }
  167. }
  168. func TestWriteSimpleJSONProtocolI32(t *testing.T) {
  169. thetype := "int32"
  170. trans := NewTMemoryBuffer()
  171. p := NewTSimpleJSONProtocol(trans)
  172. for _, value := range INT32_VALUES {
  173. if e := p.WriteI32(value); e != nil {
  174. t.Fatalf("Unable to write %s value %v due to error: %s", thetype, value, e.Error())
  175. }
  176. if e := p.Flush(); e != nil {
  177. t.Fatalf("Unable to write %s value %v due to error flushing: %s", thetype, value, e.Error())
  178. }
  179. s := trans.String()
  180. if s != fmt.Sprint(value) {
  181. t.Fatalf("Bad value for %s %v: %s", thetype, value, s)
  182. }
  183. v := int32(0)
  184. if err := json.Unmarshal([]byte(s), &v); err != nil || v != value {
  185. t.Fatalf("Bad json-decoded value for %s %v, wrote: '%s', expected: '%v'", thetype, value, s, v)
  186. }
  187. trans.Reset()
  188. }
  189. trans.Close()
  190. }
  191. func TestReadSimpleJSONProtocolI32(t *testing.T) {
  192. thetype := "int32"
  193. for _, value := range INT32_VALUES {
  194. trans := NewTMemoryBuffer()
  195. p := NewTSimpleJSONProtocol(trans)
  196. trans.WriteString(strconv.Itoa(int(value)))
  197. trans.Flush()
  198. s := trans.String()
  199. v, e := p.ReadI32()
  200. if e != nil {
  201. t.Fatalf("Unable to read %s value %v due to error: %s", thetype, value, e.Error())
  202. }
  203. if v != value {
  204. t.Fatalf("Bad value for %s value %v, wrote: %v, received: %v", thetype, value, s, v)
  205. }
  206. if err := json.Unmarshal([]byte(s), &v); err != nil || v != value {
  207. t.Fatalf("Bad json-decoded value for %s %v, wrote: '%s', expected: '%v'", thetype, value, s, v)
  208. }
  209. trans.Reset()
  210. trans.Close()
  211. }
  212. }
  213. func TestWriteSimpleJSONProtocolI64(t *testing.T) {
  214. thetype := "int64"
  215. trans := NewTMemoryBuffer()
  216. p := NewTSimpleJSONProtocol(trans)
  217. for _, value := range INT64_VALUES {
  218. if e := p.WriteI64(value); e != nil {
  219. t.Fatalf("Unable to write %s value %v due to error: %s", thetype, value, e.Error())
  220. }
  221. if e := p.Flush(); e != nil {
  222. t.Fatalf("Unable to write %s value %v due to error flushing: %s", thetype, value, e.Error())
  223. }
  224. s := trans.String()
  225. if s != fmt.Sprint(value) {
  226. t.Fatalf("Bad value for %s %v: %s", thetype, value, s)
  227. }
  228. v := int64(0)
  229. if err := json.Unmarshal([]byte(s), &v); err != nil || v != value {
  230. t.Fatalf("Bad json-decoded value for %s %v, wrote: '%s', expected: '%v'", thetype, value, s, v)
  231. }
  232. trans.Reset()
  233. }
  234. trans.Close()
  235. }
  236. func TestReadSimpleJSONProtocolI64(t *testing.T) {
  237. thetype := "int64"
  238. for _, value := range INT64_VALUES {
  239. trans := NewTMemoryBuffer()
  240. p := NewTSimpleJSONProtocol(trans)
  241. trans.WriteString(strconv.FormatInt(value, 10))
  242. trans.Flush()
  243. s := trans.String()
  244. v, e := p.ReadI64()
  245. if e != nil {
  246. t.Fatalf("Unable to read %s value %v due to error: %s", thetype, value, e.Error())
  247. }
  248. if v != value {
  249. t.Fatalf("Bad value for %s value %v, wrote: %v, received: %v", thetype, value, s, v)
  250. }
  251. if err := json.Unmarshal([]byte(s), &v); err != nil || v != value {
  252. t.Fatalf("Bad json-decoded value for %s %v, wrote: '%s', expected: '%v'", thetype, value, s, v)
  253. }
  254. trans.Reset()
  255. trans.Close()
  256. }
  257. }
  258. func TestWriteSimpleJSONProtocolDouble(t *testing.T) {
  259. thetype := "double"
  260. trans := NewTMemoryBuffer()
  261. p := NewTSimpleJSONProtocol(trans)
  262. for _, value := range DOUBLE_VALUES {
  263. if e := p.WriteDouble(value); e != nil {
  264. t.Fatalf("Unable to write %s value %v due to error: %s", thetype, value, e.Error())
  265. }
  266. if e := p.Flush(); e != nil {
  267. t.Fatalf("Unable to write %s value %v due to error flushing: %s", thetype, value, e.Error())
  268. }
  269. s := trans.String()
  270. if math.IsInf(value, 1) {
  271. if s != jsonQuote(JSON_INFINITY) {
  272. t.Fatalf("Bad value for %s %v, wrote: %v, expected: %v", thetype, value, s, jsonQuote(JSON_INFINITY))
  273. }
  274. } else if math.IsInf(value, -1) {
  275. if s != jsonQuote(JSON_NEGATIVE_INFINITY) {
  276. t.Fatalf("Bad value for %s %v, wrote: %v, expected: %v", thetype, value, s, jsonQuote(JSON_NEGATIVE_INFINITY))
  277. }
  278. } else if math.IsNaN(value) {
  279. if s != jsonQuote(JSON_NAN) {
  280. t.Fatalf("Bad value for %s %v, wrote: %v, expected: %v", thetype, value, s, jsonQuote(JSON_NAN))
  281. }
  282. } else {
  283. if s != fmt.Sprint(value) {
  284. t.Fatalf("Bad value for %s %v: %s", thetype, value, s)
  285. }
  286. v := float64(0)
  287. if err := json.Unmarshal([]byte(s), &v); err != nil || v != value {
  288. t.Fatalf("Bad json-decoded value for %s %v, wrote: '%s', expected: '%v'", thetype, value, s, v)
  289. }
  290. }
  291. trans.Reset()
  292. }
  293. trans.Close()
  294. }
  295. func TestReadSimpleJSONProtocolDouble(t *testing.T) {
  296. thetype := "double"
  297. for _, value := range DOUBLE_VALUES {
  298. trans := NewTMemoryBuffer()
  299. p := NewTSimpleJSONProtocol(trans)
  300. n := NewNumericFromDouble(value)
  301. trans.WriteString(n.String())
  302. trans.Flush()
  303. s := trans.String()
  304. v, e := p.ReadDouble()
  305. if e != nil {
  306. t.Fatalf("Unable to read %s value %v due to error: %s", thetype, value, e.Error())
  307. }
  308. if math.IsInf(value, 1) {
  309. if !math.IsInf(v, 1) {
  310. t.Fatalf("Bad value for %s %v, wrote: %v, received: %v", thetype, value, s, v)
  311. }
  312. } else if math.IsInf(value, -1) {
  313. if !math.IsInf(v, -1) {
  314. t.Fatalf("Bad value for %s %v, wrote: %v, received: %v", thetype, value, s, v)
  315. }
  316. } else if math.IsNaN(value) {
  317. if !math.IsNaN(v) {
  318. t.Fatalf("Bad value for %s %v, wrote: %v, received: %v", thetype, value, s, v)
  319. }
  320. } else {
  321. if v != value {
  322. t.Fatalf("Bad value for %s value %v, wrote: %v, received: %v", thetype, value, s, v)
  323. }
  324. if err := json.Unmarshal([]byte(s), &v); err != nil || v != value {
  325. t.Fatalf("Bad json-decoded value for %s %v, wrote: '%s', expected: '%v'", thetype, value, s, v)
  326. }
  327. }
  328. trans.Reset()
  329. trans.Close()
  330. }
  331. }
  332. func TestWriteSimpleJSONProtocolString(t *testing.T) {
  333. thetype := "string"
  334. trans := NewTMemoryBuffer()
  335. p := NewTSimpleJSONProtocol(trans)
  336. for _, value := range STRING_VALUES {
  337. if e := p.WriteString(value); e != nil {
  338. t.Fatalf("Unable to write %s value %v due to error: %s", thetype, value, e.Error())
  339. }
  340. if e := p.Flush(); e != nil {
  341. t.Fatalf("Unable to write %s value %v due to error flushing: %s", thetype, value, e.Error())
  342. }
  343. s := trans.String()
  344. if s[0] != '"' || s[len(s)-1] != '"' {
  345. t.Fatalf("Bad value for %s '%v', wrote '%v', expected: %v", thetype, value, s, fmt.Sprint("\"", value, "\""))
  346. }
  347. v := new(string)
  348. if err := json.Unmarshal([]byte(s), v); err != nil || *v != value {
  349. t.Fatalf("Bad json-decoded value for %s %v, wrote: '%s', expected: '%v'", thetype, value, s, *v)
  350. }
  351. trans.Reset()
  352. }
  353. trans.Close()
  354. }
  355. func TestReadSimpleJSONProtocolString(t *testing.T) {
  356. thetype := "string"
  357. for _, value := range STRING_VALUES {
  358. trans := NewTMemoryBuffer()
  359. p := NewTSimpleJSONProtocol(trans)
  360. trans.WriteString(jsonQuote(value))
  361. trans.Flush()
  362. s := trans.String()
  363. v, e := p.ReadString()
  364. if e != nil {
  365. t.Fatalf("Unable to read %s value %v due to error: %s", thetype, value, e.Error())
  366. }
  367. if v != value {
  368. t.Fatalf("Bad value for %s value %v, wrote: %v, received: %v", thetype, value, s, v)
  369. }
  370. v1 := new(string)
  371. if err := json.Unmarshal([]byte(s), v1); err != nil || *v1 != value {
  372. t.Fatalf("Bad json-decoded value for %s %v, wrote: '%s', expected: '%v'", thetype, value, s, *v1)
  373. }
  374. trans.Reset()
  375. trans.Close()
  376. }
  377. }
  378. func TestWriteSimpleJSONProtocolBinary(t *testing.T) {
  379. thetype := "binary"
  380. value := protocol_bdata
  381. b64value := make([]byte, base64.StdEncoding.EncodedLen(len(protocol_bdata)))
  382. base64.StdEncoding.Encode(b64value, value)
  383. b64String := string(b64value)
  384. trans := NewTMemoryBuffer()
  385. p := NewTSimpleJSONProtocol(trans)
  386. if e := p.WriteBinary(value); e != nil {
  387. t.Fatalf("Unable to write %s value %v due to error: %s", thetype, value, e.Error())
  388. }
  389. if e := p.Flush(); e != nil {
  390. t.Fatalf("Unable to write %s value %v due to error flushing: %s", thetype, value, e.Error())
  391. }
  392. s := trans.String()
  393. if s != fmt.Sprint("\"", b64String, "\"") {
  394. t.Fatalf("Bad value for %s %v\n wrote: %v\nexpected: %v", thetype, value, s, "\""+b64String+"\"")
  395. }
  396. v1 := new(string)
  397. if err := json.Unmarshal([]byte(s), v1); err != nil || *v1 != b64String {
  398. t.Fatalf("Bad json-decoded value for %s %v, wrote: '%s', expected: '%v'", thetype, value, s, *v1)
  399. }
  400. trans.Close()
  401. }
  402. func TestReadSimpleJSONProtocolBinary(t *testing.T) {
  403. thetype := "binary"
  404. value := protocol_bdata
  405. b64value := make([]byte, base64.StdEncoding.EncodedLen(len(protocol_bdata)))
  406. base64.StdEncoding.Encode(b64value, value)
  407. b64String := string(b64value)
  408. trans := NewTMemoryBuffer()
  409. p := NewTSimpleJSONProtocol(trans)
  410. trans.WriteString(jsonQuote(b64String))
  411. trans.Flush()
  412. s := trans.String()
  413. v, e := p.ReadBinary()
  414. if e != nil {
  415. t.Fatalf("Unable to read %s value %v due to error: %s", thetype, value, e.Error())
  416. }
  417. if len(v) != len(value) {
  418. t.Fatalf("Bad value for %s value length %v, wrote: %v, received length: %v", thetype, len(value), s, len(v))
  419. }
  420. for i := 0; i < len(v); i++ {
  421. if v[i] != value[i] {
  422. t.Fatalf("Bad value for %s at index %d value %v, wrote: %v, received: %v", thetype, i, value[i], s, v[i])
  423. }
  424. }
  425. v1 := new(string)
  426. if err := json.Unmarshal([]byte(s), v1); err != nil || *v1 != b64String {
  427. t.Fatalf("Bad json-decoded value for %s %v, wrote: '%s', expected: '%v'", thetype, value, s, *v1)
  428. }
  429. trans.Reset()
  430. trans.Close()
  431. }
  432. func TestWriteSimpleJSONProtocolList(t *testing.T) {
  433. thetype := "list"
  434. trans := NewTMemoryBuffer()
  435. p := NewTSimpleJSONProtocol(trans)
  436. p.WriteListBegin(TType(DOUBLE), len(DOUBLE_VALUES))
  437. for _, value := range DOUBLE_VALUES {
  438. if e := p.WriteDouble(value); e != nil {
  439. t.Fatalf("Unable to write %s value %v due to error: %s", thetype, value, e.Error())
  440. }
  441. }
  442. p.WriteListEnd()
  443. if e := p.Flush(); e != nil {
  444. t.Fatalf("Unable to write %s due to error flushing: %s", thetype, e.Error())
  445. }
  446. str := trans.String()
  447. str1 := new([]interface{})
  448. err := json.Unmarshal([]byte(str), str1)
  449. if err != nil {
  450. t.Fatalf("Unable to decode %s, wrote: %s", thetype, str)
  451. }
  452. l := *str1
  453. if len(l) < 2 {
  454. t.Fatalf("List must be at least of length two to include metadata")
  455. }
  456. if int(l[0].(float64)) != DOUBLE {
  457. t.Fatal("Invalid type for list, expected: ", DOUBLE, ", but was: ", l[0])
  458. }
  459. if int(l[1].(float64)) != len(DOUBLE_VALUES) {
  460. t.Fatal("Invalid length for list, expected: ", len(DOUBLE_VALUES), ", but was: ", l[1])
  461. }
  462. for k, value := range DOUBLE_VALUES {
  463. s := l[k+2]
  464. if math.IsInf(value, 1) {
  465. if s.(string) != JSON_INFINITY {
  466. t.Fatalf("Bad value for %s at index %v %v, wrote: %q, expected: %q, originally wrote: %q", thetype, k, value, s, jsonQuote(JSON_INFINITY), str)
  467. }
  468. } else if math.IsInf(value, 0) {
  469. if s.(string) != JSON_NEGATIVE_INFINITY {
  470. t.Fatalf("Bad value for %s at index %v %v, wrote: %q, expected: %q, originally wrote: %q", thetype, k, value, s, jsonQuote(JSON_NEGATIVE_INFINITY), str)
  471. }
  472. } else if math.IsNaN(value) {
  473. if s.(string) != JSON_NAN {
  474. t.Fatalf("Bad value for %s at index %v %v, wrote: %q, expected: %q, originally wrote: %q", thetype, k, value, s, jsonQuote(JSON_NAN), str)
  475. }
  476. } else {
  477. if s.(float64) != value {
  478. t.Fatalf("Bad json-decoded value for %s %v, wrote: '%s'", thetype, value, s)
  479. }
  480. }
  481. trans.Reset()
  482. }
  483. trans.Close()
  484. }
  485. func TestWriteSimpleJSONProtocolSet(t *testing.T) {
  486. thetype := "set"
  487. trans := NewTMemoryBuffer()
  488. p := NewTSimpleJSONProtocol(trans)
  489. p.WriteSetBegin(TType(DOUBLE), len(DOUBLE_VALUES))
  490. for _, value := range DOUBLE_VALUES {
  491. if e := p.WriteDouble(value); e != nil {
  492. t.Fatalf("Unable to write %s value %v due to error: %s", thetype, value, e.Error())
  493. }
  494. }
  495. p.WriteSetEnd()
  496. if e := p.Flush(); e != nil {
  497. t.Fatalf("Unable to write %s due to error flushing: %s", thetype, e.Error())
  498. }
  499. str := trans.String()
  500. str1 := new([]interface{})
  501. err := json.Unmarshal([]byte(str), str1)
  502. if err != nil {
  503. t.Fatalf("Unable to decode %s, wrote: %s", thetype, str)
  504. }
  505. l := *str1
  506. if len(l) < 2 {
  507. t.Fatalf("Set must be at least of length two to include metadata")
  508. }
  509. if int(l[0].(float64)) != DOUBLE {
  510. t.Fatal("Invalid type for set, expected: ", DOUBLE, ", but was: ", l[0])
  511. }
  512. if int(l[1].(float64)) != len(DOUBLE_VALUES) {
  513. t.Fatal("Invalid length for set, expected: ", len(DOUBLE_VALUES), ", but was: ", l[1])
  514. }
  515. for k, value := range DOUBLE_VALUES {
  516. s := l[k+2]
  517. if math.IsInf(value, 1) {
  518. if s.(string) != JSON_INFINITY {
  519. t.Fatalf("Bad value for %s at index %v %v, wrote: %q, expected: %q, originally wrote: %q", thetype, k, value, s, jsonQuote(JSON_INFINITY), str)
  520. }
  521. } else if math.IsInf(value, 0) {
  522. if s.(string) != JSON_NEGATIVE_INFINITY {
  523. t.Fatalf("Bad value for %s at index %v %v, wrote: %q, expected: %q, originally wrote: %q", thetype, k, value, s, jsonQuote(JSON_NEGATIVE_INFINITY), str)
  524. }
  525. } else if math.IsNaN(value) {
  526. if s.(string) != JSON_NAN {
  527. t.Fatalf("Bad value for %s at index %v %v, wrote: %q, expected: %q, originally wrote: %q", thetype, k, value, s, jsonQuote(JSON_NAN), str)
  528. }
  529. } else {
  530. if s.(float64) != value {
  531. t.Fatalf("Bad json-decoded value for %s %v, wrote: '%s'", thetype, value, s)
  532. }
  533. }
  534. trans.Reset()
  535. }
  536. trans.Close()
  537. }
  538. func TestWriteSimpleJSONProtocolMap(t *testing.T) {
  539. thetype := "map"
  540. trans := NewTMemoryBuffer()
  541. p := NewTSimpleJSONProtocol(trans)
  542. p.WriteMapBegin(TType(I32), TType(DOUBLE), len(DOUBLE_VALUES))
  543. for k, value := range DOUBLE_VALUES {
  544. if e := p.WriteI32(int32(k)); e != nil {
  545. t.Fatalf("Unable to write %s key int32 value %v due to error: %s", thetype, k, e.Error())
  546. }
  547. if e := p.WriteDouble(value); e != nil {
  548. t.Fatalf("Unable to write %s value float64 value %v due to error: %s", thetype, value, e.Error())
  549. }
  550. }
  551. p.WriteMapEnd()
  552. if e := p.Flush(); e != nil {
  553. t.Fatalf("Unable to write %s due to error flushing: %s", thetype, e.Error())
  554. }
  555. str := trans.String()
  556. if str[0] != '[' || str[len(str)-1] != ']' {
  557. t.Fatalf("Bad value for %s, wrote: %q, in go: %q", thetype, str, DOUBLE_VALUES)
  558. }
  559. l := strings.Split(str[1:len(str)-1], ",")
  560. if len(l) < 3 {
  561. t.Fatal("Expected list of at least length 3 for map for metadata, but was of length ", len(l))
  562. }
  563. expectedKeyType, _ := strconv.Atoi(l[0])
  564. expectedValueType, _ := strconv.Atoi(l[1])
  565. expectedSize, _ := strconv.Atoi(l[2])
  566. if expectedKeyType != I32 {
  567. t.Fatal("Expected map key type ", I32, ", but was ", l[0])
  568. }
  569. if expectedValueType != DOUBLE {
  570. t.Fatal("Expected map value type ", DOUBLE, ", but was ", l[1])
  571. }
  572. if expectedSize != len(DOUBLE_VALUES) {
  573. t.Fatal("Expected map size of ", len(DOUBLE_VALUES), ", but was ", l[2])
  574. }
  575. for k, value := range DOUBLE_VALUES {
  576. strk := l[k*2+3]
  577. strv := l[k*2+4]
  578. ik, err := strconv.Atoi(strk)
  579. if err != nil {
  580. t.Fatalf("Bad value for %s index %v, wrote: %v, expected: %v, error: %s", thetype, k, strk, string(k), err.Error())
  581. }
  582. if ik != k {
  583. t.Fatalf("Bad value for %s index %v, wrote: %v, expected: %v", thetype, k, strk, k)
  584. }
  585. s := strv
  586. if math.IsInf(value, 1) {
  587. if s != jsonQuote(JSON_INFINITY) {
  588. t.Fatalf("Bad value for %s at index %v %v, wrote: %v, expected: %v", thetype, k, value, s, jsonQuote(JSON_INFINITY))
  589. }
  590. } else if math.IsInf(value, 0) {
  591. if s != jsonQuote(JSON_NEGATIVE_INFINITY) {
  592. t.Fatalf("Bad value for %s at index %v %v, wrote: %v, expected: %v", thetype, k, value, s, jsonQuote(JSON_NEGATIVE_INFINITY))
  593. }
  594. } else if math.IsNaN(value) {
  595. if s != jsonQuote(JSON_NAN) {
  596. t.Fatalf("Bad value for %s at index %v %v, wrote: %v, expected: %v", thetype, k, value, s, jsonQuote(JSON_NAN))
  597. }
  598. } else {
  599. expected := strconv.FormatFloat(value, 'g', 10, 64)
  600. if s != expected {
  601. t.Fatalf("Bad value for %s at index %v %v, wrote: %v, expected %v", thetype, k, value, s, expected)
  602. }
  603. v := float64(0)
  604. if err := json.Unmarshal([]byte(s), &v); err != nil || v != value {
  605. t.Fatalf("Bad json-decoded value for %s %v, wrote: '%s', expected: '%v'", thetype, value, s, v)
  606. }
  607. }
  608. trans.Reset()
  609. }
  610. trans.Close()
  611. }