storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/pkg/net/url_test.go (about) 1 /* 2 * MinIO Cloud Storage, (C) 2018 MinIO, Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package net 18 19 import ( 20 "reflect" 21 "testing" 22 ) 23 24 func TestURLIsEmpty(t *testing.T) { 25 testCases := []struct { 26 url URL 27 expectedResult bool 28 }{ 29 {URL{}, true}, 30 {URL{Scheme: "http", Host: "play"}, false}, 31 {URL{Path: "path/to/play"}, false}, 32 } 33 34 for i, testCase := range testCases { 35 result := testCase.url.IsEmpty() 36 37 if result != testCase.expectedResult { 38 t.Fatalf("test %v: result: expected: %v, got: %v", i+1, testCase.expectedResult, result) 39 } 40 } 41 } 42 43 func TestURLString(t *testing.T) { 44 testCases := []struct { 45 url URL 46 expectedStr string 47 }{ 48 {URL{}, ""}, 49 {URL{Scheme: "http", Host: "play"}, "http://play"}, 50 {URL{Scheme: "https", Host: "play:443"}, "https://play"}, 51 {URL{Scheme: "https", Host: "play.min.io:80"}, "https://play.min.io:80"}, 52 {URL{Scheme: "https", Host: "147.75.201.93:9000", Path: "/"}, "https://147.75.201.93:9000/"}, 53 {URL{Scheme: "https", Host: "s3.amazonaws.com", Path: "/", RawQuery: "location"}, "https://s3.amazonaws.com/?location"}, 54 {URL{Scheme: "http", Host: "myminio:10000", Path: "/mybucket/myobject"}, "http://myminio:10000/mybucket/myobject"}, 55 {URL{Scheme: "ftp", Host: "myftp.server:10000", Path: "/myuser"}, "ftp://myftp.server:10000/myuser"}, 56 {URL{Path: "path/to/play"}, "path/to/play"}, 57 } 58 59 for i, testCase := range testCases { 60 str := testCase.url.String() 61 62 if str != testCase.expectedStr { 63 t.Fatalf("test %v: string: expected: %v, got: %v", i+1, testCase.expectedStr, str) 64 } 65 } 66 } 67 68 func TestURLMarshalJSON(t *testing.T) { 69 testCases := []struct { 70 url URL 71 expectedData []byte 72 expectErr bool 73 }{ 74 {URL{}, []byte(`""`), false}, 75 {URL{Scheme: "http", Host: "play"}, []byte(`"http://play"`), false}, 76 {URL{Scheme: "https", Host: "play.min.io:0"}, []byte(`"https://play.min.io:0"`), false}, 77 {URL{Scheme: "https", Host: "147.75.201.93:9000", Path: "/"}, []byte(`"https://147.75.201.93:9000/"`), false}, 78 {URL{Scheme: "https", Host: "s3.amazonaws.com", Path: "/", RawQuery: "location"}, []byte(`"https://s3.amazonaws.com/?location"`), false}, 79 {URL{Scheme: "http", Host: "myminio:10000", Path: "/mybucket/myobject"}, []byte(`"http://myminio:10000/mybucket/myobject"`), false}, 80 {URL{Scheme: "ftp", Host: "myftp.server:10000", Path: "/myuser"}, []byte(`"ftp://myftp.server:10000/myuser"`), false}, 81 } 82 83 for i, testCase := range testCases { 84 data, err := testCase.url.MarshalJSON() 85 expectErr := (err != nil) 86 87 if expectErr != testCase.expectErr { 88 t.Fatalf("test %v: error: expected: %v, got: %v", i+1, testCase.expectErr, expectErr) 89 } 90 91 if !testCase.expectErr { 92 if !reflect.DeepEqual(data, testCase.expectedData) { 93 t.Fatalf("test %v: data: expected: %v, got: %v", i+1, string(testCase.expectedData), string(data)) 94 } 95 } 96 } 97 } 98 99 func TestURLUnmarshalJSON(t *testing.T) { 100 testCases := []struct { 101 data []byte 102 expectedURL *URL 103 expectErr bool 104 }{ 105 {[]byte(`""`), &URL{}, false}, 106 {[]byte(`"http://play"`), &URL{Scheme: "http", Host: "play"}, false}, 107 {[]byte(`"https://play.min.io:0"`), &URL{Scheme: "https", Host: "play.min.io:0"}, false}, 108 {[]byte(`"https://147.75.201.93:9000/"`), &URL{Scheme: "https", Host: "147.75.201.93:9000", Path: "/"}, false}, 109 {[]byte(`"https://s3.amazonaws.com/?location"`), &URL{Scheme: "https", Host: "s3.amazonaws.com", Path: "/", RawQuery: "location"}, false}, 110 {[]byte(`"http://myminio:10000/mybucket/myobject//"`), &URL{Scheme: "http", Host: "myminio:10000", Path: "/mybucket/myobject/"}, false}, 111 {[]byte(`"ftp://myftp.server:10000/myuser"`), &URL{Scheme: "ftp", Host: "myftp.server:10000", Path: "/myuser"}, false}, 112 {[]byte(`"http://webhook.server:10000/mywebhook/"`), &URL{Scheme: "http", Host: "webhook.server:10000", Path: "/mywebhook/"}, false}, 113 {[]byte(`"myserver:1000"`), nil, true}, 114 {[]byte(`"http://:1000/mybucket"`), nil, true}, 115 {[]byte(`"https://147.75.201.93:90000/"`), nil, true}, 116 {[]byte(`"http:/play"`), nil, true}, 117 } 118 119 for i, testCase := range testCases { 120 var url URL 121 err := url.UnmarshalJSON(testCase.data) 122 expectErr := (err != nil) 123 124 if expectErr != testCase.expectErr { 125 t.Fatalf("test %v: error: expected: %v, got: %v", i+1, testCase.expectErr, expectErr) 126 } 127 128 if !testCase.expectErr { 129 if !reflect.DeepEqual(&url, testCase.expectedURL) { 130 t.Fatalf("test %v: host: expected: %#v, got: %#v", i+1, testCase.expectedURL, url) 131 } 132 } 133 } 134 } 135 136 func TestParseHTTPURL(t *testing.T) { 137 testCases := []struct { 138 s string 139 expectedURL *URL 140 expectErr bool 141 }{ 142 {"http://play", &URL{Scheme: "http", Host: "play"}, false}, 143 {"https://play.min.io:0", &URL{Scheme: "https", Host: "play.min.io:0"}, false}, 144 {"https://147.75.201.93:9000/", &URL{Scheme: "https", Host: "147.75.201.93:9000", Path: "/"}, false}, 145 {"https://s3.amazonaws.com/?location", &URL{Scheme: "https", Host: "s3.amazonaws.com", Path: "/", RawQuery: "location"}, false}, 146 {"http://myminio:10000/mybucket//myobject/", &URL{Scheme: "http", Host: "myminio:10000", Path: "/mybucket/myobject/"}, false}, 147 {"ftp://myftp.server:10000/myuser", nil, true}, 148 {"https://my.server:10000000/myuser", nil, true}, 149 {"myserver:1000", nil, true}, 150 {"http://:1000/mybucket", nil, true}, 151 {"https://147.75.201.93:90000/", nil, true}, 152 {"http:/play", nil, true}, 153 } 154 155 for _, testCase := range testCases { 156 testCase := testCase 157 t.Run(testCase.s, func(t *testing.T) { 158 url, err := ParseHTTPURL(testCase.s) 159 expectErr := (err != nil) 160 if expectErr != testCase.expectErr { 161 t.Fatalf("error: expected: %v, got: %v", testCase.expectErr, expectErr) 162 } 163 if !testCase.expectErr { 164 if !reflect.DeepEqual(url, testCase.expectedURL) { 165 t.Fatalf("host: expected: %#v, got: %#v", testCase.expectedURL, url) 166 } 167 } 168 }) 169 } 170 } 171 172 func TestParseURL(t *testing.T) { 173 testCases := []struct { 174 s string 175 expectedURL *URL 176 expectErr bool 177 }{ 178 {"http://play", &URL{Scheme: "http", Host: "play"}, false}, 179 {"https://play.min.io:0", &URL{Scheme: "https", Host: "play.min.io:0"}, false}, 180 {"https://147.75.201.93:9000/", &URL{Scheme: "https", Host: "147.75.201.93:9000", Path: "/"}, false}, 181 {"https://s3.amazonaws.com/?location", &URL{Scheme: "https", Host: "s3.amazonaws.com", Path: "/", RawQuery: "location"}, false}, 182 {"http://myminio:10000/mybucket//myobject/", &URL{Scheme: "http", Host: "myminio:10000", Path: "/mybucket/myobject/"}, false}, 183 {"ftp://myftp.server:10000/myuser", &URL{Scheme: "ftp", Host: "myftp.server:10000", Path: "/myuser"}, false}, 184 {"myserver:1000", nil, true}, 185 {"http://:1000/mybucket", nil, true}, 186 {"https://147.75.201.93:90000/", nil, true}, 187 {"http:/play", nil, true}, 188 } 189 190 for i, testCase := range testCases { 191 url, err := ParseURL(testCase.s) 192 expectErr := (err != nil) 193 194 if expectErr != testCase.expectErr { 195 t.Fatalf("test %v: error: expected: %v, got: %v", i+1, testCase.expectErr, expectErr) 196 } 197 198 if !testCase.expectErr { 199 if !reflect.DeepEqual(url, testCase.expectedURL) { 200 t.Fatalf("test %v: host: expected: %#v, got: %#v", i+1, testCase.expectedURL, url) 201 } 202 } 203 } 204 }