github.com/gogf/gf/v2@v2.7.4/encoding/gurl/url_z_unit_test.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/gogf/gf.
     6  package gurl_test
     7  
     8  import (
     9  	"net/url"
    10  	"testing"
    11  
    12  	"github.com/gogf/gf/v2/encoding/gurl"
    13  	"github.com/gogf/gf/v2/test/gtest"
    14  )
    15  
    16  var (
    17  	urlStr       = `https://golang.org/x/crypto?go-get=1 +`
    18  	urlEncode    = `https%3A%2F%2Fgolang.org%2Fx%2Fcrypto%3Fgo-get%3D1+%2B`
    19  	rawUrlEncode = `https%3A%2F%2Fgolang.org%2Fx%2Fcrypto%3Fgo-get%3D1%20%2B`
    20  )
    21  
    22  func TestEncodeAndDecode(t *testing.T) {
    23  	gtest.C(t, func(t *gtest.T) {
    24  		t.Assert(gurl.Encode(urlStr), urlEncode)
    25  
    26  		res, err := gurl.Decode(urlEncode)
    27  		if err != nil {
    28  			t.Errorf("decode failed. %v", err)
    29  			return
    30  		}
    31  		t.Assert(res, urlStr)
    32  	})
    33  }
    34  
    35  func TestRowEncodeAndDecode(t *testing.T) {
    36  	gtest.C(t, func(t *gtest.T) {
    37  		t.Assert(gurl.RawEncode(urlStr), rawUrlEncode)
    38  
    39  		res, err := gurl.RawDecode(rawUrlEncode)
    40  		if err != nil {
    41  			t.Errorf("decode failed. %v", err)
    42  			return
    43  		}
    44  		t.Assert(res, urlStr)
    45  	})
    46  }
    47  
    48  func TestBuildQuery(t *testing.T) {
    49  	gtest.C(t, func(t *gtest.T) {
    50  		src := url.Values{
    51  			"a": {"a2", "a1"},
    52  			"b": {"b2", "b1"},
    53  			"c": {"c1", "c2"},
    54  		}
    55  		expect := "a=a2&a=a1&b=b2&b=b1&c=c1&c=c2"
    56  		t.Assert(gurl.BuildQuery(src), expect)
    57  	})
    58  }
    59  
    60  func TestParseURL(t *testing.T) {
    61  	src := `http://username:password@hostname:9090/path?arg=value#anchor`
    62  	expect := map[string]string{
    63  		"scheme":   "http",
    64  		"host":     "hostname",
    65  		"port":     "9090",
    66  		"user":     "username",
    67  		"pass":     "password",
    68  		"path":     "/path",
    69  		"query":    "arg=value",
    70  		"fragment": "anchor",
    71  	}
    72  
    73  	gtest.C(t, func(t *gtest.T) {
    74  		component := 0
    75  		for k, v := range []string{"all", "scheme", "host", "port", "user", "pass", "path", "query", "fragment"} {
    76  			if v == "all" {
    77  				component = -1
    78  			} else {
    79  				component = 1 << (uint(k - 1))
    80  			}
    81  
    82  			res, err := gurl.ParseURL(src, component)
    83  			if err != nil {
    84  				t.Errorf("ParseURL failed. component:%v, err:%v", component, err)
    85  				return
    86  			}
    87  
    88  			if v == "all" {
    89  				t.Assert(res, expect)
    90  			} else {
    91  				t.Assert(res[v], expect[v])
    92  			}
    93  
    94  		}
    95  	})
    96  }