Skip to content

Commit

Permalink
Move http file to relevant package and add tests (kyma-project#522)
Browse files Browse the repository at this point in the history
Signed-off-by: Borja Clemente <[email protected]>
  • Loading branch information
clebs authored Jul 24, 2020
1 parent e5909cc commit 24dbdc5
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 28 deletions.
19 changes: 19 additions & 0 deletions internal/net/net.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package net

import (
"fmt"
"net"
"net/http"
"strconv"
)

Expand All @@ -22,3 +24,20 @@ func GetAvailablePort() (int, error) {
}
return port, err
}

func DoGet(url string) (int, error) {
httpClient := &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return 0, fmt.Errorf("cannot create a new HTTP request: %v", err)
}
resp, err := httpClient.Do(req)
if err != nil {
return 0, fmt.Errorf("cannot send HTTP request to %s: %v", url, err)
}
return resp.StatusCode, nil
}
16 changes: 16 additions & 0 deletions internal/net/net_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,19 @@ func TestGetAvailablePort(t *testing.T) {
require.True(t, p <= 65535)
require.NoError(t, err)
}

func TestDoGet(t *testing.T) {

// Happy path
sc, err := DoGet("http://google.com")
require.NoError(t, err)
require.Equal(t, 301, sc)

// Non existing URL
_, err = DoGet("http://fake-url.com")
require.Error(t, err)

// BAD URL
_, err = DoGet("this-is%not_a=URL")
require.Error(t, err)
}
27 changes: 0 additions & 27 deletions pkg/installation/http.go

This file was deleted.

3 changes: 2 additions & 1 deletion pkg/installation/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
installationSDK "github.com/kyma-incubator/hydroform/install/installation"
"github.com/kyma-project/cli/cmd/kyma/version"
"github.com/kyma-project/cli/internal/kube"
"github.com/kyma-project/cli/internal/net"
pkgErrors "github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
)
Expand Down Expand Up @@ -150,7 +151,7 @@ func (i *Installation) promptMigrationGuide(kymaVersion string, cliVersion strin
kymaSemVersion.Major(), kymaSemVersion.Minor(),
cliSemVersion.Major(), cliSemVersion.Minor(),
)
statusCode, err := doGet(guideURL)
statusCode, err := net.DoGet(guideURL)
if err != nil {
return fmt.Errorf("unable to check migration guide url: %v", err)
}
Expand Down

0 comments on commit 24dbdc5

Please sign in to comment.