Skip to content

Commit d1f6379

Browse files
Merge pull request #132 from OutSystems/add-new-flow-layer
chore(RDGRS-1371): add new flow layer
2 parents fdd9280 + 8c34041 commit d1f6379

5 files changed

Lines changed: 129 additions & 3 deletions

File tree

go.mod

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
module github.com/outsystems/cloud-connector
22

3-
require github.com/jpillora/chisel v1.10.1
3+
require (
4+
github.com/go-resty/resty/v2 v2.16.5
5+
github.com/jarcoal/httpmock v1.4.0
6+
github.com/jpillora/chisel v1.10.1
7+
)
48

59
require (
610
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 // indirect

go.sum

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio=
22
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
3+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
4+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
35
github.com/fsnotify/fsnotify v1.8.0 h1:dAwr6QBTBZIkG8roQaJjGof0pp0EeF+tNV7YBP3F/8M=
46
github.com/fsnotify/fsnotify v1.8.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0=
7+
github.com/go-resty/resty/v2 v2.16.5 h1:hBKqmWrr7uRc3euHVqmh1HTHcKn99Smr7o5spptdhTM=
8+
github.com/go-resty/resty/v2 v2.16.5/go.mod h1:hkJtXbA2iKHzJheXYvQ8snQES5ZLGKMwQ07xAwp/fiA=
59
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
610
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
11+
github.com/jarcoal/httpmock v1.4.0 h1:BvhqnH0JAYbNudL2GMJKgOHe2CtKlzJ/5rWKyp+hc2k=
12+
github.com/jarcoal/httpmock v1.4.0/go.mod h1:ftW1xULwo+j0R0JJkJIIi7UKigZUXCLLanykgjwBXL0=
713
github.com/jpillora/backoff v1.0.0 h1:uvFg412JmmHBHw7iwprIxkPMI+sGQ4kzOWsMeHnm2EA=
814
github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4=
915
github.com/jpillora/sizestr v1.0.0 h1:4tr0FLxs1Mtq3TnsLDV+GYUWG7Q26a6s+tV5Zfw2ygw=
1016
github.com/jpillora/sizestr v1.0.0/go.mod h1:bUhLv4ctkknatr6gR42qPxirmd5+ds1u7mzD+MZ33f0=
17+
github.com/maxatome/go-testdeep v1.14.0 h1:rRlLv1+kI8eOI3OaBXZwb3O7xY3exRzdW5QyX48g9wI=
18+
github.com/maxatome/go-testdeep v1.14.0/go.mod h1:lPZc/HAcJMP92l7yI6TRz1aZN5URwUBUAfUNvrclaNM=
1119
github.com/outsystems/chisel v1.10.1-os.4 h1:PQhBs8URg7QQdLNct5l8OUDusClOgxrnSKarUvb0fss=
1220
github.com/outsystems/chisel v1.10.1-os.4/go.mod h1:MyQSnDHZfhuBduS13BSHgAQkQHjfoSUVlvJ/rItyPcQ=
1321
golang.org/x/crypto v0.36.0 h1:AnAEvhDddvBdpY+uR+MyHmuZzzNqXSe/GvuDeob5L34=
@@ -20,3 +28,5 @@ golang.org/x/sys v0.31.0 h1:ioabZlmFYtWhL+TRYpcnNlLwhyxaM9kWTDEmfnprqik=
2028
golang.org/x/sys v0.31.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
2129
golang.org/x/term v0.30.0 h1:PQ39fJZ+mfadBm0y5WlL4vlM7Sx1Hgf13sMIY2+QS9Y=
2230
golang.org/x/term v0.30.0/go.mod h1:NYYFdzHoI5wRh/h5tDMdMqCqPJZEuNqVR5xJLd/n67g=
31+
golang.org/x/time v0.6.0 h1:eTDhh4ZXt5Qf0augr54TN6suAUudPcawVZeIAPU7D4U=
32+
golang.org/x/time v0.6.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=

main.go

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import (
1414

1515
"math/rand"
1616

17+
"github.com/go-resty/resty/v2"
18+
1719
chclient "github.com/jpillora/chisel/client"
1820
"github.com/jpillora/chisel/share/cos"
1921
"github.com/jpillora/chisel/share/settings"
@@ -150,7 +152,10 @@ func client(args []string) {
150152

151153
queryParams := generateQueryParameters(localPorts)
152154

153-
config.Server = fmt.Sprintf("%s%s", args[0], queryParams)
155+
//get server URL
156+
serverURL := fetchURL(resty.New(), args[0])
157+
158+
config.Server = fmt.Sprintf("%s%s", serverURL, queryParams)
154159
config.Remotes = args[1:]
155160

156161
//default auth
@@ -180,6 +185,30 @@ func client(args []string) {
180185
}
181186
}
182187

188+
func fetchURL(client *resty.Client, requestLocation string) string {
189+
190+
client.SetRedirectPolicy(resty.NoRedirectPolicy())
191+
192+
if !strings.HasPrefix(requestLocation, "http") {
193+
requestLocation = "http://" + requestLocation
194+
}
195+
196+
resp, err := client.SetDoNotParseResponse(true).R().Get(requestLocation)
197+
if err != nil {
198+
if resp != nil && resp.StatusCode() == http.StatusFound {
199+
redirectURL := resp.Header().Get("Location")
200+
if redirectURL == "" {
201+
log.Fatalf("Redirect response missing 'Location' header")
202+
}
203+
return redirectURL
204+
} else {
205+
log.Fatalf("Failed to fetch URL '%s': %v", requestLocation, err)
206+
}
207+
}
208+
209+
return requestLocation
210+
}
211+
183212
func generateQueryParameters(localPorts string) string {
184213
return fmt.Sprintf("?id=%v&ports=%v", rand.Intn(999999999-100000000)+100000000, localPorts)
185214
}

main_test.go

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
package main
22

3-
import "testing"
3+
import (
4+
"net/http"
5+
"testing"
6+
7+
"strings"
8+
9+
"github.com/go-resty/resty/v2"
10+
"github.com/jarcoal/httpmock"
11+
)
412

513
func Test_validateRemotes(t *testing.T) {
614

@@ -35,3 +43,78 @@ func Test_validateRemotes(t *testing.T) {
3543
})
3644
}
3745
}
46+
47+
func Test_getURL(t *testing.T) {
48+
tests := []struct {
49+
name string
50+
requestLocation string
51+
responseStatusCode int
52+
redirectLocation string
53+
want string
54+
}{
55+
{
56+
name: "valid location",
57+
requestLocation: "https://service.us-east-1.example.com",
58+
responseStatusCode: http.StatusOK,
59+
redirectLocation: "",
60+
want: "https://service.us-east-1.example.com",
61+
},
62+
63+
{
64+
name: "valid location - no scheme",
65+
requestLocation: "service.us-east-1.example.com",
66+
responseStatusCode: http.StatusOK,
67+
redirectLocation: "",
68+
want: "http://service.us-east-1.example.com",
69+
},
70+
{
71+
name: "302 redirect",
72+
requestLocation: "https://service.us-east-1.example.com",
73+
responseStatusCode: http.StatusFound,
74+
redirectLocation: "https://redirected.example.com",
75+
want: "https://redirected.example.com",
76+
},
77+
{
78+
name: "302 redirect - no scheme",
79+
requestLocation: "service.us-east-1.example.com",
80+
responseStatusCode: http.StatusFound,
81+
redirectLocation: "https://redirected.example.com",
82+
want: "https://redirected.example.com",
83+
},
84+
}
85+
86+
for _, tt := range tests {
87+
t.Run(tt.name, func(t *testing.T) {
88+
client := resty.New()
89+
90+
// Activate httpmock for this client
91+
httpmock.ActivateNonDefault(client.GetClient())
92+
defer httpmock.DeactivateAndReset()
93+
mockRequestLocation := tt.requestLocation
94+
95+
if !strings.HasPrefix(tt.requestLocation, "http") {
96+
mockRequestLocation = "http://" + mockRequestLocation
97+
}
98+
99+
// Register a mocked response
100+
httpmock.RegisterResponder("GET", mockRequestLocation,
101+
func(req *http.Request) (*http.Response, error) {
102+
resp := httpmock.NewStringResponse(tt.responseStatusCode, "")
103+
resp.Header.Set("Location", tt.redirectLocation)
104+
return resp, nil
105+
},
106+
)
107+
108+
got := fetchURL(client, tt.requestLocation)
109+
if got != tt.want {
110+
t.Errorf("getURL() = %v, want %v", got, tt.want)
111+
}
112+
if tt.responseStatusCode == http.StatusFound {
113+
// Check if the redirect location was set correctly
114+
httpmock.RegisterResponder("GET", mockRequestLocation,
115+
httpmock.NewStringResponder(tt.responseStatusCode, "Location: "+tt.redirectLocation),
116+
)
117+
}
118+
})
119+
}
120+
}

outsystemscc

10.6 MB
Binary file not shown.

0 commit comments

Comments
 (0)