...

Source file src/github.com/gabriel-vasile/mimetype/internal/json/json_test.go

Documentation: github.com/gabriel-vasile/mimetype/internal/json

     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  	"strings"
     9  	"testing"
    10  )
    11  
    12  func TestScan(t *testing.T) {
    13  	tCases := []struct {
    14  		data   string
    15  		length int
    16  		ok     bool
    17  	}{
    18  		{`foo`, 2, false},
    19  		{`}{`, 1, false},
    20  		{`{]`, 2, false},
    21  		{`{}`, 2, true},
    22  		{`[]`, 2, true},
    23  		{`"foo"`, 5, true},
    24  		{`120`, 3, true},
    25  		{`1e20`, 4, true},
    26  		{`0.120`, 5, true},
    27  		{`null`, 4, true},
    28  		{`{"foo":"bar"}`, 13, true},
    29  		{`{"foo":"21\t\u0009 \u1234","bar":{"baz":["qux"]}`, 48, false},
    30  		{`{"foo":"bar","bar":{"baz":["qux"]}}`, 35, true},
    31  		{`{"foo":-1,"bar":{"baz":[true, false, null, 100, 0.123]}}`, 56, true},
    32  		{`{"foo":-1,"bar":{"baz":[tru]}}`, 28, false},
    33  		{`{"foo":-1,"bar":{"baz":[nul]}}`, 28, false},
    34  		{`{"foo":-1,"bar":{"baz":[314e+1]}}`, 33, true},
    35  	}
    36  	for _, st := range tCases {
    37  		scanned, err := Scan([]byte(st.data))
    38  		if scanned != st.length {
    39  			t.Errorf("Scan length error: expected: %d; got: %d; input: %s",
    40  				st.length, scanned, st.data)
    41  		}
    42  
    43  		if err != nil && st.ok {
    44  			t.Errorf("Scan failed with err: %s; input: %s", err, st.data)
    45  		}
    46  
    47  		if err == nil && !st.ok {
    48  			t.Errorf("Scan should fail for input: %s", st.data)
    49  		}
    50  	}
    51  }
    52  
    53  func TestScannerMaxDepth(t *testing.T) {
    54  	tCases := []struct {
    55  		name        string
    56  		data        string
    57  		errMaxDepth bool
    58  	}{
    59  		{
    60  			name:        "ArrayUnderMaxNestingDepth",
    61  			data:        `{"a":` + strings.Repeat(`[`, 10000-1) + strings.Repeat(`]`, 10000-1) + `}`,
    62  			errMaxDepth: false,
    63  		},
    64  		{
    65  			name:        "ArrayOverMaxNestingDepth",
    66  			data:        `{"a":` + strings.Repeat(`[`, 10000) + strings.Repeat(`]`, 10000) + `}`,
    67  			errMaxDepth: true,
    68  		},
    69  		{
    70  			name:        "ObjectUnderMaxNestingDepth",
    71  			data:        `{"a":` + strings.Repeat(`{"a":`, 10000-1) + `0` + strings.Repeat(`}`, 10000-1) + `}`,
    72  			errMaxDepth: false,
    73  		},
    74  		{
    75  			name:        "ObjectOverMaxNestingDepth",
    76  			data:        `{"a":` + strings.Repeat(`{"a":`, 10000) + `0` + strings.Repeat(`}`, 10000) + `}`,
    77  			errMaxDepth: true,
    78  		},
    79  	}
    80  
    81  	for _, tt := range tCases {
    82  		t.Run(tt.name, func(t *testing.T) {
    83  			_, err := Scan([]byte(tt.data))
    84  			if !tt.errMaxDepth {
    85  				if err != nil {
    86  					t.Errorf("unexpected error: %v", err)
    87  				}
    88  			} else {
    89  				if err == nil {
    90  					t.Errorf("expected error containing 'exceeded max depth', got none")
    91  				} else if !strings.Contains(err.Error(), "exceeded max depth") {
    92  					t.Errorf("expected error containing 'exceeded max depth', got: %v", err)
    93  				}
    94  			}
    95  		})
    96  	}
    97  }
    98  

View as plain text