github.com/keysonzzz/kmg@v0.0.0-20151121023212-05317bfd7d39/kmgNet/kmgHttp/url_test.go (about) 1 package kmgHttp 2 3 import ( 4 "testing" 5 6 . "github.com/bronze1man/kmg/kmgTest" 7 ) 8 9 func TestAddParameterToUrl(ot *testing.T) { 10 u, err := AddParameterToUrl("http://foo.com/", "a", "b") 11 Equal(err, nil) 12 Equal(u, "http://foo.com/?a=b") 13 14 u, err = AddParameterToUrl("/?n=a&n1=b&n2=c&a=c", "a", "b") 15 Equal(err, nil) 16 Equal(u, "/?a=c&a=b&n=a&n1=b&n2=c") 17 } 18 19 func TestSetParameterToUrl(ot *testing.T) { 20 u, err := SetParameterToUrl("http://foo.com/", "a", "b") 21 Equal(err, nil) 22 Equal(u, "http://foo.com/?a=b") 23 24 u, err = SetParameterToUrl("/?n=a&n1=b&n2=c&a=c&b=d", "a", "b") 25 Equal(err, nil) 26 Equal(u, "/?a=b&b=d&n=a&n1=b&n2=c") 27 } 28 29 /* 30 import ( 31 "github.com/bronze1man/kmg/kmgTest" 32 "testing" 33 ) 34 35 func TestNewUrlByString(ot *testing.T) { 36 t := kmgTest.NewTestTools(ot) 37 url, err := NewUrlByString("http://www.google.com") 38 t.Equal(nil, err) 39 t.Equal("http://www.google.com", url.String()) 40 } 41 */ 42 43 func TestGetDomainName(t *testing.T) { 44 testCaseAList := []string{ 45 "http://abc.com", 46 "https://abc.com", 47 "https://abc.com/", 48 "http://abc.com/index.html", 49 "file://abc.com/upload/index.css", 50 "ftp://abc.com/upload/index.css", 51 } 52 testCaseBList := []string{ 53 "abc.com/upload/index.css", 54 "/upload/index.css", 55 "upload/index.css", 56 } 57 for index, url := range testCaseAList { 58 domain, protocol := GetDomainName(url) 59 Equal(domain, "abc.com") 60 if index == 2 { 61 Equal(protocol, "https://") 62 } 63 if index == 3 { 64 Equal(protocol, "http://") 65 } 66 } 67 for _, url := range testCaseBList { 68 domain, _ := GetDomainName(url) 69 Equal(domain, "") 70 } 71 }