github.com/mh-cbon/go@v0.0.0-20160603070303-9e112a3fe4c0/src/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  	for i := 0; i < b.N; i++ {
   272  		if err := NewEncoder(ioutil.Discard).Encode(v); err != nil {
   273  			b.Fatal(err)
   274  		}
   275  	}
   276  }
   277  
   278  type tokenStreamCase struct {
   279  	json      string
   280  	expTokens []interface{}
   281  }
   282  
   283  type decodeThis struct {
   284  	v interface{}
   285  }
   286  
   287  var tokenStreamCases []tokenStreamCase = []tokenStreamCase{
   288  	// streaming token cases
   289  	{json: `10`, expTokens: []interface{}{float64(10)}},
   290  	{json: ` [10] `, expTokens: []interface{}{
   291  		Delim('['), float64(10), Delim(']')}},
   292  	{json: ` [false,10,"b"] `, expTokens: []interface{}{
   293  		Delim('['), false, float64(10), "b", Delim(']')}},
   294  	{json: `{ "a": 1 }`, expTokens: []interface{}{
   295  		Delim('{'), "a", float64(1), Delim('}')}},
   296  	{json: `{"a": 1, "b":"3"}`, expTokens: []interface{}{
   297  		Delim('{'), "a", float64(1), "b", "3", Delim('}')}},
   298  	{json: ` [{"a": 1},{"a": 2}] `, expTokens: []interface{}{
   299  		Delim('['),
   300  		Delim('{'), "a", float64(1), Delim('}'),
   301  		Delim('{'), "a", float64(2), Delim('}'),
   302  		Delim(']')}},
   303  	{json: `{"obj": {"a": 1}}`, expTokens: []interface{}{
   304  		Delim('{'), "obj", Delim('{'), "a", float64(1), Delim('}'),
   305  		Delim('}')}},
   306  	{json: `{"obj": [{"a": 1}]}`, expTokens: []interface{}{
   307  		Delim('{'), "obj", Delim('['),
   308  		Delim('{'), "a", float64(1), Delim('}'),
   309  		Delim(']'), Delim('}')}},
   310  
   311  	// streaming tokens with intermittent Decode()
   312  	{json: `{ "a": 1 }`, expTokens: []interface{}{
   313  		Delim('{'), "a",
   314  		decodeThis{float64(1)},
   315  		Delim('}')}},
   316  	{json: ` [ { "a" : 1 } ] `, expTokens: []interface{}{
   317  		Delim('['),
   318  		decodeThis{map[string]interface{}{"a": float64(1)}},
   319  		Delim(']')}},
   320  	{json: ` [{"a": 1},{"a": 2}] `, expTokens: []interface{}{
   321  		Delim('['),
   322  		decodeThis{map[string]interface{}{"a": float64(1)}},
   323  		decodeThis{map[string]interface{}{"a": float64(2)}},
   324  		Delim(']')}},
   325  	{json: `{ "obj" : [ { "a" : 1 } ] }`, expTokens: []interface{}{
   326  		Delim('{'), "obj", Delim('['),
   327  		decodeThis{map[string]interface{}{"a": float64(1)}},
   328  		Delim(']'), Delim('}')}},
   329  
   330  	{json: `{"obj": {"a": 1}}`, expTokens: []interface{}{
   331  		Delim('{'), "obj",
   332  		decodeThis{map[string]interface{}{"a": float64(1)}},
   333  		Delim('}')}},
   334  	{json: `{"obj": [{"a": 1}]}`, expTokens: []interface{}{
   335  		Delim('{'), "obj",
   336  		decodeThis{[]interface{}{
   337  			map[string]interface{}{"a": float64(1)},
   338  		}},
   339  		Delim('}')}},
   340  	{json: ` [{"a": 1} {"a": 2}] `, expTokens: []interface{}{
   341  		Delim('['),
   342  		decodeThis{map[string]interface{}{"a": float64(1)}},
   343  		decodeThis{&SyntaxError{"expected comma after array element", 0}},
   344  	}},
   345  	{json: `{ "a" 1 }`, expTokens: []interface{}{
   346  		Delim('{'), "a",
   347  		decodeThis{&SyntaxError{"expected colon after object key", 0}},
   348  	}},
   349  }
   350  
   351  func TestDecodeInStream(t *testing.T) {
   352  
   353  	for ci, tcase := range tokenStreamCases {
   354  
   355  		dec := NewDecoder(strings.NewReader(tcase.json))
   356  		for i, etk := range tcase.expTokens {
   357  
   358  			var tk interface{}
   359  			var err error
   360  
   361  			if dt, ok := etk.(decodeThis); ok {
   362  				etk = dt.v
   363  				err = dec.Decode(&tk)
   364  			} else {
   365  				tk, err = dec.Token()
   366  			}
   367  			if experr, ok := etk.(error); ok {
   368  				if err == nil || err.Error() != experr.Error() {
   369  					t.Errorf("case %v: Expected error %v in %q, but was %v", ci, experr, tcase.json, err)
   370  				}
   371  				break
   372  			} else if err == io.EOF {
   373  				t.Errorf("case %v: Unexpected EOF in %q", ci, tcase.json)
   374  				break
   375  			} else if err != nil {
   376  				t.Errorf("case %v: Unexpected error '%v' in %q", ci, err, tcase.json)
   377  				break
   378  			}
   379  			if !reflect.DeepEqual(tk, etk) {
   380  				t.Errorf(`case %v: %q @ %v expected %T(%v) was %T(%v)`, ci, tcase.json, i, etk, etk, tk, tk)
   381  				break
   382  			}
   383  		}
   384  	}
   385  
   386  }
   387  
   388  // Test from golang.org/issue/11893
   389  func TestHTTPDecoding(t *testing.T) {
   390  	const raw = `{ "foo": "bar" }`
   391  
   392  	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   393  		w.Write([]byte(raw))
   394  	}))
   395  	defer ts.Close()
   396  	res, err := http.Get(ts.URL)
   397  	if err != nil {
   398  		log.Fatalf("GET failed: %v", err)
   399  	}
   400  	defer res.Body.Close()
   401  
   402  	foo := struct {
   403  		Foo string
   404  	}{}
   405  
   406  	d := NewDecoder(res.Body)
   407  	err = d.Decode(&foo)
   408  	if err != nil {
   409  		t.Fatalf("Decode: %v", err)
   410  	}
   411  	if foo.Foo != "bar" {
   412  		t.Errorf("decoded %q; want \"bar\"", foo.Foo)
   413  	}
   414  
   415  	// make sure we get the EOF the second time
   416  	err = d.Decode(&foo)
   417  	if err != io.EOF {
   418  		t.Errorf("err = %v; want io.EOF", err)
   419  	}
   420  }