github.com/micro/go-micro/v2@v2.9.1/util/qson/qson_test.go (about)

     1  package qson
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  )
     7  
     8  func ExampleUnmarshal() {
     9  	type Ex struct {
    10  		A string `json:"a"`
    11  		B struct {
    12  			C int `json:"c"`
    13  		} `json:"b"`
    14  	}
    15  	var ex Ex
    16  	if err := Unmarshal(&ex, "a=xyz&b[c]=456"); err != nil {
    17  		panic(err)
    18  	}
    19  	fmt.Printf("%+v\n", ex)
    20  	// Output: {A:xyz B:{C:456}}
    21  }
    22  
    23  type unmarshalT struct {
    24  	A string     `json:"a"`
    25  	B unmarshalB `json:"b"`
    26  }
    27  type unmarshalB struct {
    28  	C int    `json:"c"`
    29  	D string `json:"D"`
    30  }
    31  
    32  func TestUnmarshal(t *testing.T) {
    33  	query := "a=xyz&b[c]=456"
    34  	expected := unmarshalT{
    35  		A: "xyz",
    36  		B: unmarshalB{
    37  			C: 456,
    38  		},
    39  	}
    40  	var actual unmarshalT
    41  	err := Unmarshal(&actual, query)
    42  	if err != nil {
    43  		t.Error(err)
    44  	}
    45  	if expected != actual {
    46  		t.Errorf("Expected: %+v Actual: %+v", expected, actual)
    47  	}
    48  }
    49  
    50  func ExampleToJSON() {
    51  	b, err := ToJSON("a=xyz&b[c]=456")
    52  	if err != nil {
    53  		panic(err)
    54  	}
    55  	fmt.Printf(string(b))
    56  	// Output: {"a":"xyz","b":{"c":456}}
    57  }
    58  
    59  func TestToJSONNested(t *testing.T) {
    60  	query := "bar%5Bone%5D%5Btwo%5D=2&bar[one][red]=112"
    61  	expected := `{"bar":{"one":{"red":112,"two":2}}}`
    62  	actual, err := ToJSON(query)
    63  	if err != nil {
    64  		t.Error(err)
    65  	}
    66  	actualStr := string(actual)
    67  	if actualStr != expected {
    68  		t.Errorf("Expected: %s Actual: %s", expected, actualStr)
    69  	}
    70  }
    71  
    72  func TestToJSONPlain(t *testing.T) {
    73  	query := "cat=1&dog=2"
    74  	expected := `{"cat":1,"dog":2}`
    75  	actual, err := ToJSON(query)
    76  	if err != nil {
    77  		t.Error(err)
    78  	}
    79  	actualStr := string(actual)
    80  	if actualStr != expected {
    81  		t.Errorf("Expected: %s Actual: %s", expected, actualStr)
    82  	}
    83  }
    84  
    85  func TestToJSONSlice(t *testing.T) {
    86  	query := "cat[]=1&cat[]=34"
    87  	expected := `{"cat":[1,34]}`
    88  	actual, err := ToJSON(query)
    89  	if err != nil {
    90  		t.Error(err)
    91  	}
    92  	actualStr := string(actual)
    93  	if actualStr != expected {
    94  		t.Errorf("Expected: %s Actual: %s", expected, actualStr)
    95  	}
    96  }
    97  
    98  func TestToJSONBig(t *testing.T) {
    99  	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"
   100  	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}`
   101  	actual, err := ToJSON(query)
   102  	if err != nil {
   103  		t.Error(err)
   104  	}
   105  	actualStr := string(actual)
   106  	if actualStr != expected {
   107  		t.Errorf("Expected: %s Actual: %s", expected, actualStr)
   108  	}
   109  }
   110  
   111  func TestToJSONDuplicateKey(t *testing.T) {
   112  	query := "cat=1&cat=2"
   113  	expected := `{"cat":2}`
   114  	actual, err := ToJSON(query)
   115  	if err != nil {
   116  		t.Error(err)
   117  	}
   118  	actualStr := string(actual)
   119  	if actualStr != expected {
   120  		t.Errorf("Expected: %s Actual: %s", expected, actualStr)
   121  	}
   122  }
   123  
   124  func TestSplitKeyAndValue(t *testing.T) {
   125  	param := "a[dog][=cat]=123"
   126  	eKey, eValue := "a[dog][=cat]", "123"
   127  	aKey, aValue, err := splitKeyAndValue(param)
   128  	if err != nil {
   129  		t.Error(err)
   130  	}
   131  	if eKey != aKey {
   132  		t.Errorf("Keys do not match. Expected: %s Actual: %s", eKey, aKey)
   133  	}
   134  	if eValue != aValue {
   135  		t.Errorf("Values do not match. Expected: %s Actual: %s", eValue, aValue)
   136  	}
   137  }
   138  
   139  func TestEncodedAmpersand(t *testing.T) {
   140  	query := "a=xyz&b[d]=ben%26jerry"
   141  	expected := unmarshalT{
   142  		A: "xyz",
   143  		B: unmarshalB{
   144  			D: "ben&jerry",
   145  		},
   146  	}
   147  	var actual unmarshalT
   148  	err := Unmarshal(&actual, query)
   149  	if err != nil {
   150  		t.Error(err)
   151  	}
   152  	if expected != actual {
   153  		t.Errorf("Expected: %+v Actual: %+v", expected, actual)
   154  	}
   155  }
   156  
   157  func TestEncodedAmpersand2(t *testing.T) {
   158  	query := "filter=parent%3Dflow12345%26request%3Dreq12345&meta.limit=20&meta.offset=0"
   159  	expected := map[string]interface{}{"filter": "parent=flow12345&request=req12345", "meta.limit": float64(20), "meta.offset": float64(0)}
   160  	actual := make(map[string]interface{})
   161  	err := Unmarshal(&actual, query)
   162  	if err != nil {
   163  		t.Error(err)
   164  	}
   165  	for k, v := range actual {
   166  		if nv, ok := expected[k]; !ok || nv != v {
   167  			t.Errorf("Expected: %+v Actual: %+v", expected, actual)
   168  		}
   169  	}
   170  }