github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/util/qson/qson_test.go (about)

     1  // Licensed under the Apache License, Version 2.0 (the "License");
     2  // you may not use this file except in compliance with the License.
     3  // You may obtain a copy of the License at
     4  //
     5  //     https://www.apache.org/licenses/LICENSE-2.0
     6  //
     7  // Unless required by applicable law or agreed to in writing, software
     8  // distributed under the License is distributed on an "AS IS" BASIS,
     9  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    10  // See the License for the specific language governing permissions and
    11  // limitations under the License.
    12  //
    13  // Original source: github.com/micro/go-micro/v3/util/qson/qson_test.go
    14  
    15  package qson
    16  
    17  import (
    18  	"fmt"
    19  	"testing"
    20  )
    21  
    22  func TestFuzz1(t *testing.T) {
    23  	b, err := ToJSON(`[^@hGl5=`)
    24  	if err != nil {
    25  		t.Fatal(err)
    26  	}
    27  	_ = b
    28  }
    29  
    30  func ExampleUnmarshal() {
    31  	type Ex struct {
    32  		A string `json:"a"`
    33  		B struct {
    34  			C int `json:"c"`
    35  		} `json:"b"`
    36  	}
    37  	var ex Ex
    38  	if err := Unmarshal(&ex, "a=xyz&b[c]=456"); err != nil {
    39  		panic(err)
    40  	}
    41  	fmt.Printf("%+v\n", ex)
    42  	// Output: {A:xyz B:{C:456}}
    43  }
    44  
    45  type unmarshalT struct {
    46  	A string     `json:"a"`
    47  	B unmarshalB `json:"b"`
    48  }
    49  type unmarshalB struct {
    50  	C int    `json:"c"`
    51  	D string `json:"D"`
    52  }
    53  
    54  func TestUnmarshal(t *testing.T) {
    55  	query := "a=xyz&b[c]=456"
    56  	expected := unmarshalT{
    57  		A: "xyz",
    58  		B: unmarshalB{
    59  			C: 456,
    60  		},
    61  	}
    62  	var actual unmarshalT
    63  	err := Unmarshal(&actual, query)
    64  	if err != nil {
    65  		t.Error(err)
    66  	}
    67  	if expected != actual {
    68  		t.Errorf("Expected: %+v Actual: %+v", expected, actual)
    69  	}
    70  }
    71  
    72  func ExampleToJSON() {
    73  	b, err := ToJSON("a=xyz&b[c]=456")
    74  	if err != nil {
    75  		panic(err)
    76  	}
    77  	fmt.Printf(string(b))
    78  	// Output: {"a":"xyz","b":{"c":456}}
    79  }
    80  
    81  func TestToJSONNested(t *testing.T) {
    82  	query := "bar%5Bone%5D%5Btwo%5D=2&bar[one][red]=112"
    83  	expected := `{"bar":{"one":{"red":112,"two":2}}}`
    84  	actual, err := ToJSON(query)
    85  	if err != nil {
    86  		t.Error(err)
    87  	}
    88  	actualStr := string(actual)
    89  	if actualStr != expected {
    90  		t.Errorf("Expected: %s Actual: %s", expected, actualStr)
    91  	}
    92  }
    93  
    94  func TestToJSONPlain(t *testing.T) {
    95  	query := "cat=1&dog=2"
    96  	expected := `{"cat":1,"dog":2}`
    97  	actual, err := ToJSON(query)
    98  	if err != nil {
    99  		t.Error(err)
   100  	}
   101  	actualStr := string(actual)
   102  	if actualStr != expected {
   103  		t.Errorf("Expected: %s Actual: %s", expected, actualStr)
   104  	}
   105  }
   106  
   107  func TestToJSONSlice(t *testing.T) {
   108  	query := "cat[]=1&cat[]=34"
   109  	expected := `{"cat":[1,34]}`
   110  	actual, err := ToJSON(query)
   111  	if err != nil {
   112  		t.Error(err)
   113  	}
   114  	actualStr := string(actual)
   115  	if actualStr != expected {
   116  		t.Errorf("Expected: %s Actual: %s", expected, actualStr)
   117  	}
   118  }
   119  
   120  func TestToJSONBig(t *testing.T) {
   121  	query := "distinct_id=763_1495187301909_3495&timestamp=1495187523&event=product_add_cart&params%5BproductRefId%5D=8284563078&params%5Bapps%5D%5B%5D=precommend&params%5Bapps%5D%5B%5D=bsales&params%5Bsource%5D=item&params%5Boptions%5D%5Bsegment%5D=cart_recommendation&params%5Boptions%5D%5Btype%5D=up_sell&params%5BtimeExpire%5D=1495187599642&params%5Brecommend_system_product_source%5D=item&params%5Bproduct_id%5D=8284563078&params%5Bvariant_id%5D=27661944134&params%5Bsku%5D=00483332%20(black)&params%5Bsources%5D%5B%5D=product_recommendation&params%5Bcart_token%5D=dc2c336a009edf2762128e65806dfb1d&params%5Bquantity%5D=1&params%5Bnew_popup_upsell_mobile%5D=false&params%5BclientDevice%5D=desktop&params%5BclientIsMobile%5D=false&params%5BclientIsSmallScreen%5D=false&params%5Bnew_popup_crossell_mobile%5D=false&api_key=14c5b7dacea9157029265b174491d340"
   122  	expected := `{"api_key":"14c5b7dacea9157029265b174491d340","distinct_id":"763_1495187301909_3495","event":"product_add_cart","params":{"apps":["precommend","bsales"],"cart_token":"dc2c336a009edf2762128e65806dfb1d","clientDevice":"desktop","clientIsMobile":false,"clientIsSmallScreen":false,"new_popup_crossell_mobile":false,"new_popup_upsell_mobile":false,"options":{"segment":"cart_recommendation","type":"up_sell"},"productRefId":8284563078,"product_id":8284563078,"quantity":1,"recommend_system_product_source":"item","sku":"00483332 (black)","source":"item","sources":["product_recommendation"],"timeExpire":1495187599642,"variant_id":27661944134},"timestamp":1495187523}`
   123  	actual, err := ToJSON(query)
   124  	if err != nil {
   125  		t.Error(err)
   126  	}
   127  	actualStr := string(actual)
   128  	if actualStr != expected {
   129  		t.Errorf("Expected: %s Actual: %s", expected, actualStr)
   130  	}
   131  }
   132  
   133  func TestToJSONDuplicateKey(t *testing.T) {
   134  	query := "cat=1&cat=2"
   135  	expected := `{"cat":2}`
   136  	actual, err := ToJSON(query)
   137  	if err != nil {
   138  		t.Error(err)
   139  	}
   140  	actualStr := string(actual)
   141  	if actualStr != expected {
   142  		t.Errorf("Expected: %s Actual: %s", expected, actualStr)
   143  	}
   144  }
   145  
   146  func TestSplitKeyAndValue(t *testing.T) {
   147  	param := "a[dog][=cat]=123"
   148  	eKey, eValue := "a[dog][=cat]", "123"
   149  	aKey, aValue, err := splitKeyAndValue(param)
   150  	if err != nil {
   151  		t.Error(err)
   152  	}
   153  	if eKey != aKey {
   154  		t.Errorf("Keys do not match. Expected: %s Actual: %s", eKey, aKey)
   155  	}
   156  	if eValue != aValue {
   157  		t.Errorf("Values do not match. Expected: %s Actual: %s", eValue, aValue)
   158  	}
   159  }
   160  
   161  func TestEncodedAmpersand(t *testing.T) {
   162  	query := "a=xyz&b[d]=ben%26jerry"
   163  	expected := unmarshalT{
   164  		A: "xyz",
   165  		B: unmarshalB{
   166  			D: "ben&jerry",
   167  		},
   168  	}
   169  	var actual unmarshalT
   170  	err := Unmarshal(&actual, query)
   171  	if err != nil {
   172  		t.Error(err)
   173  	}
   174  	if expected != actual {
   175  		t.Errorf("Expected: %+v Actual: %+v", expected, actual)
   176  	}
   177  }
   178  
   179  func TestEncodedAmpersand2(t *testing.T) {
   180  	query := "filter=parent%3Dflow12345%26request%3Dreq12345&meta.limit=20&meta.offset=0"
   181  	expected := map[string]interface{}{"filter": "parent=flow12345&request=req12345", "meta.limit": float64(20), "meta.offset": float64(0)}
   182  	actual := make(map[string]interface{})
   183  	err := Unmarshal(&actual, query)
   184  	if err != nil {
   185  		t.Error(err)
   186  	}
   187  	for k, v := range actual {
   188  		if nv, ok := expected[k]; !ok || nv != v {
   189  			t.Errorf("Expected: %+v Actual: %+v", expected, actual)
   190  		}
   191  	}
   192  }
   193  
   194  func TestMergeSlice(t *testing.T) {
   195  	a := []interface{}{"a"}
   196  	b := []interface{}{"b"}
   197  	actual := mergeSlice(a, b)
   198  	if len(actual) != 2 {
   199  		t.Errorf("Expected size to be 2.")
   200  	}
   201  	if actual[0] != "a" {
   202  		t.Errorf("Expected index 0 to have value a. Actual: %s", actual[0])
   203  	}
   204  	if actual[1] != "b" {
   205  		t.Errorf("Expected index 1 to have value b. Actual: %s", actual[1])
   206  	}
   207  }
   208  
   209  func TestMergeMap(t *testing.T) {
   210  	a := map[string]interface{}{
   211  		"a": "b",
   212  	}
   213  	b := map[string]interface{}{
   214  		"b": "c",
   215  	}
   216  	actual := mergeMap(a, b)
   217  	if len(actual) != 2 {
   218  		t.Errorf("Expected size to be 2.")
   219  	}
   220  	if actual["a"] != "b" {
   221  		t.Errorf("Expected key \"a\" to have value b. Actual: %s", actual["a"])
   222  	}
   223  	if actual["b"] != "c" {
   224  		t.Errorf("Expected key \"b\" to have value c. Actual: %s", actual["b"])
   225  	}
   226  }