Browse Source

add url test case

fancl 2 years ago
parent
commit
a3a4edac45
2 changed files with 30 additions and 5 deletions
  1. 4 5
      helper/url/url.go
  2. 26 0
      helper/url/url_test.go

+ 4 - 5
helper/url/url.go

@@ -64,11 +64,10 @@ func Create(opts ...Option) string {
 		qs.Set(k, v)
 	}
 	uri := url.URL{
-		Scheme:     o.Schema,
-		Host:       o.Host,
-		Path:       o.Path,
-		ForceQuery: true,
-		RawQuery:   qs.Encode(),
+		Scheme:   o.Schema,
+		Host:     o.Host,
+		Path:     o.Path,
+		RawQuery: qs.Encode(),
 	}
 	return uri.String()
 }

+ 26 - 0
helper/url/url_test.go

@@ -0,0 +1,26 @@
+package url
+
+import "testing"
+
+func TestCreate(t *testing.T) {
+	type args struct {
+		opts []Option
+	}
+	tests := []struct {
+		name string
+		args args
+		want string
+	}{
+		{"1", args{opts: []Option{WithHost("baidu.com")}}, "https://baidu.com"},
+		{"2", args{opts: []Option{WithSchema("http"), WithHost("baidu.com")}}, "http://baidu.com"},
+		{"3", args{opts: []Option{WithSchema("http"), WithHost("baidu.com"), WithPath("/cgi-bin")}}, "http://baidu.com/cgi-bin"},
+		{"4", args{opts: []Option{WithSchema("http"), WithHost("baidu.com"), WithParams(map[string]string{"a": "b"})}}, "http://baidu.com?a=b"},
+	}
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			if got := Create(tt.args.opts...); got != tt.want {
+				t.Errorf("Create() = %v, want %v", got, tt.want)
+			}
+		})
+	}
+}