Skip to content

Commit 495671d

Browse files
authored
Merge pull request #465 from aniket866/fix/parsing-for-port-url
fix (cmd) : URL Port Corrupts Input in Import-URL Command
2 parents 75009c7 + a4b8f37 commit 495671d

2 files changed

Lines changed: 165 additions & 16 deletions

File tree

‎cmd/importURL.go‎

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -100,22 +100,7 @@ func NewImportURLCommand(globalClientOpts *connectors.ClientOptions) *cobra.Comm
100100
mainArtifact := true
101101
secret := ""
102102

103-
// Check if URL starts with https or http
104-
if strings.HasPrefix(f, "https://") || strings.HasPrefix(f, "http://") {
105-
urlAndMainAtrifactAndSecretName := strings.Split(f, ":")
106-
n := len(urlAndMainAtrifactAndSecretName)
107-
f = urlAndMainAtrifactAndSecretName[0] + ":" + urlAndMainAtrifactAndSecretName[1]
108-
if n > 2 {
109-
val, err := strconv.ParseBool(urlAndMainAtrifactAndSecretName[2])
110-
if err != nil {
111-
fmt.Println(err)
112-
}
113-
mainArtifact = val
114-
}
115-
if n > 3 {
116-
secret = urlAndMainAtrifactAndSecretName[3]
117-
}
118-
}
103+
f, mainArtifact, secret = parseImportURLArg(f)
119104

120105
// Try downloading the artifcat
121106
msg, err := mc.DownloadArtifact(f, mainArtifact, secret)
@@ -130,3 +115,26 @@ func NewImportURLCommand(globalClientOpts *connectors.ClientOptions) *cobra.Comm
130115

131116
return importURLCmd
132117
}
118+
119+
func parseImportURLArg(f string) (string, bool, string) {
120+
mainArtifact := true
121+
secret := ""
122+
123+
// Check if URL starts with https or http
124+
if strings.HasPrefix(f, "https://") || strings.HasPrefix(f, "http://") {
125+
parts := strings.Split(f, ":")
126+
n := len(parts)
127+
128+
for i := n - 1; i >= 2; i-- {
129+
if val, parseErr := strconv.ParseBool(parts[i]); parseErr == nil {
130+
mainArtifact = val
131+
if i+1 < n {
132+
secret = strings.Join(parts[i+1:], ":")
133+
}
134+
f = strings.Join(parts[:i], ":")
135+
break
136+
}
137+
}
138+
}
139+
return f, mainArtifact, secret
140+
}

‎cmd/importURL_test.go‎

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
/*
2+
* Copyright The Microcks Authors.
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 cmd
18+
19+
import (
20+
"testing"
21+
22+
"github.com/stretchr/testify/assert"
23+
)
24+
25+
func TestParseImportURLArg(t *testing.T) {
26+
tests := []struct {
27+
name string
28+
input string
29+
expectedURL string
30+
expectedMainArtifact bool
31+
expectedSecret string
32+
}{
33+
{
34+
name: "standard URL without suffixes",
35+
input: "https://example.com/openapi.yaml",
36+
expectedURL: "https://example.com/openapi.yaml",
37+
expectedMainArtifact: true,
38+
expectedSecret: "",
39+
},
40+
{
41+
name: "standard URL with mainArtifact true",
42+
input: "https://example.com/spec1.yaml:true",
43+
expectedURL: "https://example.com/spec1.yaml",
44+
expectedMainArtifact: true,
45+
expectedSecret: "",
46+
},
47+
{
48+
name: "standard URL with mainArtifact false",
49+
input: "https://example.com/spec1.yaml:false",
50+
expectedURL: "https://example.com/spec1.yaml",
51+
expectedMainArtifact: false,
52+
expectedSecret: "",
53+
},
54+
{
55+
name: "standard URL with mainArtifact and secret",
56+
input: "https://example.com/spec1.yaml:true:my-secret",
57+
expectedURL: "https://example.com/spec1.yaml",
58+
expectedMainArtifact: true,
59+
expectedSecret: "my-secret",
60+
},
61+
{
62+
name: "standard URL with mainArtifact false and secret",
63+
input: "https://example.com/spec1.yaml:false:my-secret-token",
64+
expectedURL: "https://example.com/spec1.yaml",
65+
expectedMainArtifact: false,
66+
expectedSecret: "my-secret-token",
67+
},
68+
{
69+
name: "URL with port and no suffixes",
70+
input: "http://localhost:8585/spec.yaml",
71+
expectedURL: "http://localhost:8585/spec.yaml",
72+
expectedMainArtifact: true,
73+
expectedSecret: "",
74+
},
75+
{
76+
name: "URL with port and mainArtifact true",
77+
input: "http://localhost:8585/spec.yaml:true",
78+
expectedURL: "http://localhost:8585/spec.yaml",
79+
expectedMainArtifact: true,
80+
expectedSecret: "",
81+
},
82+
{
83+
name: "URL with port and mainArtifact false",
84+
input: "http://localhost:8585/spec.yaml:false",
85+
expectedURL: "http://localhost:8585/spec.yaml",
86+
expectedMainArtifact: false,
87+
expectedSecret: "",
88+
},
89+
{
90+
name: "URL with port, mainArtifact and secret",
91+
input: "http://localhost:8585/spec.yaml:true:my-secret-token",
92+
expectedURL: "http://localhost:8585/spec.yaml",
93+
expectedMainArtifact: true,
94+
expectedSecret: "my-secret-token",
95+
},
96+
{
97+
name: "URL with port, mainArtifact false and secret",
98+
input: "http://localhost:8585/spec.yaml:false:my-secret-token",
99+
expectedURL: "http://localhost:8585/spec.yaml",
100+
expectedMainArtifact: false,
101+
expectedSecret: "my-secret-token",
102+
},
103+
{
104+
name: "malformed bool suffix preserves URL",
105+
input: "http://localhost:8585/spec.yaml:tru",
106+
expectedURL: "http://localhost:8585/spec.yaml:tru",
107+
expectedMainArtifact: true,
108+
expectedSecret: "",
109+
},
110+
{
111+
name: "malformed bool with secret preserves URL",
112+
input: "http://localhost:8585/spec.yaml:tru:mysecret",
113+
expectedURL: "http://localhost:8585/spec.yaml:tru:mysecret",
114+
expectedMainArtifact: true,
115+
expectedSecret: "",
116+
},
117+
{
118+
name: "URL with path and no port",
119+
input: "https://raw.githubusercontent.com/org/repo/main/spec.yaml",
120+
expectedURL: "https://raw.githubusercontent.com/org/repo/main/spec.yaml",
121+
expectedMainArtifact: true,
122+
expectedSecret: "",
123+
},
124+
{
125+
name: "short URL without path",
126+
input: "http://localhost:true",
127+
expectedURL: "http://localhost",
128+
expectedMainArtifact: true,
129+
expectedSecret: "",
130+
},
131+
}
132+
133+
for _, tt := range tests {
134+
t.Run(tt.name, func(t *testing.T) {
135+
url, mainArtifact, secret := parseImportURLArg(tt.input)
136+
assert.Equal(t, tt.expectedURL, url)
137+
assert.Equal(t, tt.expectedMainArtifact, mainArtifact)
138+
assert.Equal(t, tt.expectedSecret, secret)
139+
})
140+
}
141+
}

0 commit comments

Comments
 (0)