json_protocol_test.go 21 KB

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