github.com/ltltlt/go-source-code@v0.0.0-20190830023027-95be009773aa/encoding/json/stream_test.go (about) 1 // Copyright 2010 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package json 6 7 import ( 8 "bytes" 9 "io" 10 "io/ioutil" 11 "log" 12 "net" 13 "net/http" 14 "net/http/httptest" 15 "reflect" 16 "strings" 17 "testing" 18 ) 19 20 // Test values for the stream test. 21 // One of each JSON kind. 22 var streamTest = []interface{}{ 23 0.1, 24 "hello", 25 nil, 26 true, 27 false, 28 []interface{}{"a", "b", "c"}, 29 map[string]interface{}{"K": "Kelvin", "ß": "long s"}, 30 3.14, // another value to make sure something can follow map 31 } 32 33 var streamEncoded = `0.1 34 "hello" 35 null 36 true 37 false 38 ["a","b","c"] 39 {"ß":"long s","K":"Kelvin"} 40 3.14 41 ` 42 43 func TestEncoder(t *testing.T) { 44 for i := 0; i <= len(streamTest); i++ { 45 var buf bytes.Buffer 46 enc := NewEncoder(&buf) 47 // Check that enc.SetIndent("", "") turns off indentation. 48 enc.SetIndent(">", ".") 49 enc.SetIndent("", "") 50 for j, v := range streamTest[0:i] { 51 if err := enc.Encode(v); err != nil { 52 t.Fatalf("encode #%d: %v", j, err) 53 } 54 } 55 if have, want := buf.String(), nlines(streamEncoded, i); have != want { 56 t.Errorf("encoding %d items: mismatch", i) 57 diff(t, []byte(have), []byte(want)) 58 break 59 } 60 } 61 } 62 63 var streamEncodedIndent = `0.1 64 "hello" 65 null 66 true 67 false 68 [ 69 >."a", 70 >."b", 71 >."c" 72 >] 73 { 74 >."ß": "long s", 75 >."K": "Kelvin" 76 >} 77 3.14 78 ` 79 80 func TestEncoderIndent(t *testing.T) { 81 var buf bytes.Buffer 82 enc := NewEncoder(&buf) 83 enc.SetIndent(">", ".") 84 for _, v := range streamTest { 85 enc.Encode(v) 86 } 87 if have, want := buf.String(), streamEncodedIndent; have != want { 88 t.Error("indented encoding mismatch") 89 diff(t, []byte(have), []byte(want)) 90 } 91 } 92 93 func TestEncoderSetEscapeHTML(t *testing.T) { 94 var c C 95 var ct CText 96 for _, tt := range []struct { 97 name string 98 v interface{} 99 wantEscape string 100 want string 101 }{ 102 {"c", c, `"\u003c\u0026\u003e"`, `"<&>"`}, 103 {"ct", ct, `"\"\u003c\u0026\u003e\""`, `"\"<&>\""`}, 104 {`"<&>"`, "<&>", `"\u003c\u0026\u003e"`, `"<&>"`}, 105 } { 106 var buf bytes.Buffer 107 enc := NewEncoder(&buf) 108 if err := enc.Encode(tt.v); err != nil { 109 t.Fatalf("Encode(%s): %s", tt.name, err) 110 } 111 if got := strings.TrimSpace(buf.String()); got != tt.wantEscape { 112 t.Errorf("Encode(%s) = %#q, want %#q", tt.name, got, tt.wantEscape) 113 } 114 buf.Reset() 115 enc.SetEscapeHTML(false) 116 if err := enc.Encode(tt.v); err != nil { 117 t.Fatalf("SetEscapeHTML(false) Encode(%s): %s", tt.name, err) 118 } 119 if got := strings.TrimSpace(buf.String()); got != tt.want { 120 t.Errorf("SetEscapeHTML(false) Encode(%s) = %#q, want %#q", 121 tt.name, got, tt.want) 122 } 123 } 124 } 125 126 func TestDecoder(t *testing.T) { 127 for i := 0; i <= len(streamTest); i++ { 128 // Use stream without newlines as input, 129 // just to stress the decoder even more. 130 // Our test input does not include back-to-back numbers. 131 // Otherwise stripping the newlines would 132 // merge two adjacent JSON values. 133 var buf bytes.Buffer 134 for _, c := range nlines(streamEncoded, i) { 135 if c != '\n' { 136 buf.WriteRune(c) 137 } 138 } 139 out := make([]interface{}, i) 140 dec := NewDecoder(&buf) 141 for j := range out { 142 if err := dec.Decode(&out[j]); err != nil { 143 t.Fatalf("decode #%d/%d: %v", j, i, err) 144 } 145 } 146 if !reflect.DeepEqual(out, streamTest[0:i]) { 147 t.Errorf("decoding %d items: mismatch", i) 148 for j := range out { 149 if !reflect.DeepEqual(out[j], streamTest[j]) { 150 t.Errorf("#%d: have %v want %v", j, out[j], streamTest[j]) 151 } 152 } 153 break 154 } 155 } 156 } 157 158 func TestDecoderBuffered(t *testing.T) { 159 r := strings.NewReader(`{"Name": "Gopher"} extra `) 160 var m struct { 161 Name string 162 } 163 d := NewDecoder(r) 164 err := d.Decode(&m) 165 if err != nil { 166 t.Fatal(err) 167 } 168 if m.Name != "Gopher" { 169 t.Errorf("Name = %q; want Gopher", m.Name) 170 } 171 rest, err := ioutil.ReadAll(d.Buffered()) 172 if err != nil { 173 t.Fatal(err) 174 } 175 if g, w := string(rest), " extra "; g != w { 176 t.Errorf("Remaining = %q; want %q", g, w) 177 } 178 } 179 180 func nlines(s string, n int) string { 181 if n <= 0 { 182 return "" 183 } 184 for i, c := range s { 185 if c == '\n' { 186 if n--; n == 0 { 187 return s[0 : i+1] 188 } 189 } 190 } 191 return s 192 } 193 194 func TestRawMessage(t *testing.T) { 195 // TODO(rsc): Should not need the * in *RawMessage 196 var data struct { 197 X float64 198 Id *RawMessage 199 Y float32 200 } 201 const raw = `["\u0056",null]` 202 const msg = `{"X":0.1,"Id":["\u0056",null],"Y":0.2}` 203 err := Unmarshal([]byte(msg), &data) 204 if err != nil { 205 t.Fatalf("Unmarshal: %v", err) 206 } 207 if string([]byte(*data.Id)) != raw { 208 t.Fatalf("Raw mismatch: have %#q want %#q", []byte(*data.Id), raw) 209 } 210 b, err := Marshal(&data) 211 if err != nil { 212 t.Fatalf("Marshal: %v", err) 213 } 214 if string(b) != msg { 215 t.Fatalf("Marshal: have %#q want %#q", b, msg) 216 } 217 } 218 219 func TestNullRawMessage(t *testing.T) { 220 // TODO(rsc): Should not need the * in *RawMessage 221 var data struct { 222 X float64 223 Id *RawMessage 224 Y float32 225 } 226 data.Id = new(RawMessage) 227 const msg = `{"X":0.1,"Id":null,"Y":0.2}` 228 err := Unmarshal([]byte(msg), &data) 229 if err != nil { 230 t.Fatalf("Unmarshal: %v", err) 231 } 232 if data.Id != nil { 233 t.Fatalf("Raw mismatch: have non-nil, want nil") 234 } 235 b, err := Marshal(&data) 236 if err != nil { 237 t.Fatalf("Marshal: %v", err) 238 } 239 if string(b) != msg { 240 t.Fatalf("Marshal: have %#q want %#q", b, msg) 241 } 242 } 243 244 var blockingTests = []string{ 245 `{"x": 1}`, 246 `[1, 2, 3]`, 247 } 248 249 func TestBlocking(t *testing.T) { 250 for _, enc := range blockingTests { 251 r, w := net.Pipe() 252 go w.Write([]byte(enc)) 253 var val interface{} 254 255 // If Decode reads beyond what w.Write writes above, 256 // it will block, and the test will deadlock. 257 if err := NewDecoder(r).Decode(&val); err != nil { 258 t.Errorf("decoding %s: %v", enc, err) 259 } 260 r.Close() 261 w.Close() 262 } 263 } 264 265 func BenchmarkEncoderEncode(b *testing.B) { 266 b.ReportAllocs() 267 type T struct { 268 X, Y string 269 } 270 v := &T{"foo", "bar"} 271 b.RunParallel(func(pb *testing.PB) { 272 for pb.Next() { 273 if err := NewEncoder(ioutil.Discard).Encode(v); err != nil { 274 b.Fatal(err) 275 } 276 } 277 }) 278 } 279 280 type tokenStreamCase struct { 281 json string 282 expTokens []interface{} 283 } 284 285 type decodeThis struct { 286 v interface{} 287 } 288 289 var tokenStreamCases []tokenStreamCase = []tokenStreamCase{ 290 // streaming token cases 291 {json: `10`, expTokens: []interface{}{float64(10)}}, 292 {json: ` [10] `, expTokens: []interface{}{ 293 Delim('['), float64(10), Delim(']')}}, 294 {json: ` [false,10,"b"] `, expTokens: []interface{}{ 295 Delim('['), false, float64(10), "b", Delim(']')}}, 296 {json: `{ "a": 1 }`, expTokens: []interface{}{ 297 Delim('{'), "a", float64(1), Delim('}')}}, 298 {json: `{"a": 1, "b":"3"}`, expTokens: []interface{}{ 299 Delim('{'), "a", float64(1), "b", "3", Delim('}')}}, 300 {json: ` [{"a": 1},{"a": 2}] `, expTokens: []interface{}{ 301 Delim('['), 302 Delim('{'), "a", float64(1), Delim('}'), 303 Delim('{'), "a", float64(2), Delim('}'), 304 Delim(']')}}, 305 {json: `{"obj": {"a": 1}}`, expTokens: []interface{}{ 306 Delim('{'), "obj", Delim('{'), "a", float64(1), Delim('}'), 307 Delim('}')}}, 308 {json: `{"obj": [{"a": 1}]}`, expTokens: []interface{}{ 309 Delim('{'), "obj", Delim('['), 310 Delim('{'), "a", float64(1), Delim('}'), 311 Delim(']'), Delim('}')}}, 312 313 // streaming tokens with intermittent Decode() 314 {json: `{ "a": 1 }`, expTokens: []interface{}{ 315 Delim('{'), "a", 316 decodeThis{float64(1)}, 317 Delim('}')}}, 318 {json: ` [ { "a" : 1 } ] `, expTokens: []interface{}{ 319 Delim('['), 320 decodeThis{map[string]interface{}{"a": float64(1)}}, 321 Delim(']')}}, 322 {json: ` [{"a": 1},{"a": 2}] `, expTokens: []interface{}{ 323 Delim('['), 324 decodeThis{map[string]interface{}{"a": float64(1)}}, 325 decodeThis{map[string]interface{}{"a": float64(2)}}, 326 Delim(']')}}, 327 {json: `{ "obj" : [ { "a" : 1 } ] }`, expTokens: []interface{}{ 328 Delim('{'), "obj", Delim('['), 329 decodeThis{map[string]interface{}{"a": float64(1)}}, 330 Delim(']'), Delim('}')}}, 331 332 {json: `{"obj": {"a": 1}}`, expTokens: []interface{}{ 333 Delim('{'), "obj", 334 decodeThis{map[string]interface{}{"a": float64(1)}}, 335 Delim('}')}}, 336 {json: `{"obj": [{"a": 1}]}`, expTokens: []interface{}{ 337 Delim('{'), "obj", 338 decodeThis{[]interface{}{ 339 map[string]interface{}{"a": float64(1)}, 340 }}, 341 Delim('}')}}, 342 {json: ` [{"a": 1} {"a": 2}] `, expTokens: []interface{}{ 343 Delim('['), 344 decodeThis{map[string]interface{}{"a": float64(1)}}, 345 decodeThis{&SyntaxError{"expected comma after array element", 11}}, 346 }}, 347 {json: `{ "` + strings.Repeat("a", 513) + `" 1 }`, expTokens: []interface{}{ 348 Delim('{'), strings.Repeat("a", 513), 349 decodeThis{&SyntaxError{"expected colon after object key", 518}}, 350 }}, 351 {json: `{ "\a" }`, expTokens: []interface{}{ 352 Delim('{'), 353 &SyntaxError{"invalid character 'a' in string escape code", 3}, 354 }}, 355 {json: ` \a`, expTokens: []interface{}{ 356 &SyntaxError{"invalid character '\\\\' looking for beginning of value", 1}, 357 }}, 358 } 359 360 func TestDecodeInStream(t *testing.T) { 361 362 for ci, tcase := range tokenStreamCases { 363 364 dec := NewDecoder(strings.NewReader(tcase.json)) 365 for i, etk := range tcase.expTokens { 366 367 var tk interface{} 368 var err error 369 370 if dt, ok := etk.(decodeThis); ok { 371 etk = dt.v 372 err = dec.Decode(&tk) 373 } else { 374 tk, err = dec.Token() 375 } 376 if experr, ok := etk.(error); ok { 377 if err == nil || !reflect.DeepEqual(err, experr) { 378 t.Errorf("case %v: Expected error %#v in %q, but was %#v", ci, experr, tcase.json, err) 379 } 380 break 381 } else if err == io.EOF { 382 t.Errorf("case %v: Unexpected EOF in %q", ci, tcase.json) 383 break 384 } else if err != nil { 385 t.Errorf("case %v: Unexpected error '%#v' in %q", ci, err, tcase.json) 386 break 387 } 388 if !reflect.DeepEqual(tk, etk) { 389 t.Errorf(`case %v: %q @ %v expected %T(%v) was %T(%v)`, ci, tcase.json, i, etk, etk, tk, tk) 390 break 391 } 392 } 393 } 394 395 } 396 397 // Test from golang.org/issue/11893 398 func TestHTTPDecoding(t *testing.T) { 399 const raw = `{ "foo": "bar" }` 400 401 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 402 w.Write([]byte(raw)) 403 })) 404 defer ts.Close() 405 res, err := http.Get(ts.URL) 406 if err != nil { 407 log.Fatalf("GET failed: %v", err) 408 } 409 defer res.Body.Close() 410 411 foo := struct { 412 Foo string 413 }{} 414 415 d := NewDecoder(res.Body) 416 err = d.Decode(&foo) 417 if err != nil { 418 t.Fatalf("Decode: %v", err) 419 } 420 if foo.Foo != "bar" { 421 t.Errorf("decoded %q; want \"bar\"", foo.Foo) 422 } 423 424 // make sure we get the EOF the second time 425 err = d.Decode(&foo) 426 if err != io.EOF { 427 t.Errorf("err = %v; want io.EOF", err) 428 } 429 }