|
| 1 | +/* |
| 2 | +Copyright © contributors to CloudNativePG, established as |
| 3 | +CloudNativePG a Series of LF Projects, LLC. |
| 4 | +
|
| 5 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +you may not use this file except in compliance with the License. |
| 7 | +You may obtain a copy of the License at |
| 8 | +
|
| 9 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +
|
| 11 | +Unless required by applicable law or agreed to in writing, software |
| 12 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +See the License for the specific language governing permissions and |
| 15 | +limitations under the License. |
| 16 | +
|
| 17 | +SPDX-License-Identifier: Apache-2.0 |
| 18 | +*/ |
| 19 | + |
| 20 | +package url |
| 21 | + |
| 22 | +import ( |
| 23 | + "net" |
| 24 | + neturl "net/url" |
| 25 | + "strconv" |
| 26 | + "strings" |
| 27 | + |
| 28 | + . "github.com/onsi/ginkgo/v2" |
| 29 | + . "github.com/onsi/gomega" |
| 30 | +) |
| 31 | + |
| 32 | +var _ = Describe("URL", func() { |
| 33 | + cases := []struct { |
| 34 | + scheme string |
| 35 | + host string |
| 36 | + port int32 |
| 37 | + path string |
| 38 | + expect string |
| 39 | + }{ |
| 40 | + { |
| 41 | + // path has leading slash |
| 42 | + scheme: "http", |
| 43 | + host: "localhost", |
| 44 | + port: 1234, |
| 45 | + path: "/foo", |
| 46 | + expect: "http://localhost:1234/foo", |
| 47 | + }, |
| 48 | + { |
| 49 | + // path has no leading slash |
| 50 | + scheme: "http", |
| 51 | + host: "localhost", |
| 52 | + port: 1234, |
| 53 | + path: "foo", |
| 54 | + expect: "http://localhost:1234/foo", |
| 55 | + }, |
| 56 | + { |
| 57 | + // ipv4 address |
| 58 | + scheme: "http", |
| 59 | + host: "1.2.3.4", |
| 60 | + port: 1234, |
| 61 | + path: "foo", |
| 62 | + expect: "http://1.2.3.4:1234/foo", |
| 63 | + }, |
| 64 | + { |
| 65 | + // ipv6 address |
| 66 | + scheme: "http", |
| 67 | + host: "1:2:3::4", |
| 68 | + port: 1234, |
| 69 | + path: "foo", |
| 70 | + expect: "http://[1:2:3::4]:1234/foo", |
| 71 | + }, |
| 72 | + } |
| 73 | + |
| 74 | + It("generates the expected url", func() { |
| 75 | + for _, unit := range cases { |
| 76 | + Expect(Build(unit.scheme, unit.host, unit.path, unit.port)).To(Equal(unit.expect)) |
| 77 | + } |
| 78 | + }) |
| 79 | + |
| 80 | + It("roundtrips", func() { |
| 81 | + for _, unit := range cases { |
| 82 | + url := Build(unit.scheme, unit.host, unit.path, unit.port) |
| 83 | + parsed, err := neturl.Parse(url) |
| 84 | + Expect(err).ToNot(HaveOccurred()) |
| 85 | + Expect(parsed.Scheme).To(Equal(unit.scheme)) |
| 86 | + port := strconv.Itoa(int(unit.port)) |
| 87 | + Expect(parsed.Host).To(Equal(net.JoinHostPort(unit.host, port))) |
| 88 | + Expect(parsed.Port()).To(Equal(port)) |
| 89 | + // parsed.Path always has a leading slash |
| 90 | + Expect(parsed.Path).To(Equal("/" + strings.TrimPrefix(unit.path, "/"))) |
| 91 | + } |
| 92 | + }) |
| 93 | +}) |
0 commit comments