github.com/qxnw/lib4go@v0.0.0-20180426074627-c80c7e84b925/encoding/base64/url.base64_test.go (about)

     1  package base64
     2  
     3  import (
     4  	"bytes"
     5  	"strings"
     6  	"testing"
     7  )
     8  
     9  func TestUrlToByte(t *testing.T) {
    10  	input := []byte("http://www.baidu.com")
    11  	except := "aHR0cDovL3d3dy5iYWlkdS5jb20="
    12  	actual := URLEncodeBytes(input)
    13  	if !strings.EqualFold(except, actual) {
    14  		t.Errorf("test fail actual:%s, except:%s", actual, except)
    15  	}
    16  
    17  	actualByte, err := URLDecodeBytes(actual)
    18  	if err != nil {
    19  		t.Errorf("test fail err : %v", err)
    20  	}
    21  	if !bytes.EqualFold(actualByte, input) {
    22  		t.Error("test fail")
    23  	}
    24  
    25  	errInput := "!@#!"
    26  	_, err = URLDecodeBytes(errInput)
    27  	if err == nil {
    28  		t.Error("测试错误")
    29  		return
    30  	}
    31  }
    32  
    33  func TestUrlToString(t *testing.T) {
    34  	input := "http://www.baidu.com"
    35  	except := "aHR0cDovL3d3dy5iYWlkdS5jb20="
    36  	actual := URLEncode(input)
    37  	if !strings.EqualFold(except, actual) {
    38  		t.Errorf("test fail actual:%s, except:%s", actual, except)
    39  	}
    40  
    41  	actual, err := URLDecode(actual)
    42  	if err != nil {
    43  		t.Errorf("test fail err : %v", err)
    44  	}
    45  	if !strings.EqualFold(actual, input) {
    46  		t.Error("test fail")
    47  	}
    48  
    49  	errInput := "!@#!"
    50  	_, err = URLDecode(errInput)
    51  	if err == nil {
    52  		t.Error("测试错误")
    53  		return
    54  	}
    55  }