You've already forked go-semantic-release
test(gitlab): add missing tests
This commit is contained in:
@@ -133,21 +133,6 @@ func zipFile(repository string, file string) (string, error) {
|
||||
return zipFileName, nil
|
||||
}
|
||||
|
||||
// CheckURL if is valid
|
||||
func CheckURL(urlStr string) (string, error) {
|
||||
|
||||
if !strings.HasSuffix(urlStr, "/") {
|
||||
urlStr += "/"
|
||||
}
|
||||
|
||||
_, err := url.Parse(urlStr)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return urlStr, nil
|
||||
}
|
||||
|
||||
//PathEscape to be url save
|
||||
func PathEscape(s string) string {
|
||||
return strings.Replace(url.PathEscape(s), ".", "%2E", -1)
|
||||
@@ -162,7 +147,7 @@ func Do(client *http.Client, req *http.Request, v interface{}) (*http.Response,
|
||||
defer resp.Body.Close()
|
||||
|
||||
switch resp.StatusCode {
|
||||
case 200, 201, 202, 204, 304:
|
||||
case 200, 201, 202, 204:
|
||||
if v != nil {
|
||||
if w, ok := v.(io.Writer); ok {
|
||||
_, err = io.Copy(w, resp.Body)
|
||||
@@ -178,7 +163,7 @@ func Do(client *http.Client, req *http.Request, v interface{}) (*http.Response,
|
||||
// IsValidResult validates response code
|
||||
func IsValidResult(resp *http.Response) error {
|
||||
switch resp.StatusCode {
|
||||
case 200, 201, 202, 204, 304:
|
||||
case 200, 201, 202, 204:
|
||||
return nil
|
||||
default:
|
||||
return fmt.Errorf("%s %s: %d", resp.Request.Method, resp.Request.URL, resp.StatusCode)
|
||||
|
||||
@@ -3,9 +3,15 @@ package util_test
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
||||
"github.com/Nightapes/go-semantic-release/pkg/config"
|
||||
|
||||
@@ -117,3 +123,108 @@ func TestPrepareAssets(t *testing.T) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestShouldRetry(t *testing.T) {
|
||||
assert.True(t, util.ShouldRetry(&http.Response{StatusCode: 429}))
|
||||
assert.False(t, util.ShouldRetry(&http.Response{StatusCode: 200}))
|
||||
}
|
||||
|
||||
func TestIsValidResult(t *testing.T) {
|
||||
assert.NoError(t, util.IsValidResult(&http.Response{StatusCode: 200}))
|
||||
assert.NoError(t, util.IsValidResult(&http.Response{StatusCode: 201}))
|
||||
assert.NoError(t, util.IsValidResult(&http.Response{StatusCode: 202}))
|
||||
assert.NoError(t, util.IsValidResult(&http.Response{StatusCode: 204}))
|
||||
|
||||
u, err := url.Parse("https://localhost")
|
||||
assert.NoError(t, err)
|
||||
assert.Error(t, util.IsValidResult(&http.Response{StatusCode: 500, Request: &http.Request{
|
||||
Method: "POST",
|
||||
URL: u,
|
||||
}}))
|
||||
}
|
||||
|
||||
func TestPathEscape(t *testing.T) {
|
||||
assert.Equal(t, "test%2Ftest", util.PathEscape("test/test"))
|
||||
assert.Equal(t, "test", util.PathEscape("test"))
|
||||
assert.Equal(t, "test%2Etest", util.PathEscape("test.test"))
|
||||
}
|
||||
|
||||
type example struct {
|
||||
Test string `json:"test"`
|
||||
}
|
||||
|
||||
func TestDoAndRoundTrip(t *testing.T) {
|
||||
tokenHeader := util.NewAddHeaderTransport(nil, "PRIVATE-TOKEN", "aToken")
|
||||
acceptHeader := util.NewAddHeaderTransport(tokenHeader, "Accept", "application/json")
|
||||
httpClient := &http.Client{
|
||||
Transport: acceptHeader,
|
||||
Timeout: time.Second * 60,
|
||||
}
|
||||
|
||||
testsDoMethod := []struct {
|
||||
statusCode int
|
||||
body string
|
||||
responseBody interface{}
|
||||
responseBodyType interface{}
|
||||
hasError bool
|
||||
path string
|
||||
}{
|
||||
{
|
||||
statusCode: 200,
|
||||
body: `{"test" : "hallo"}`,
|
||||
responseBody: &example{
|
||||
Test: "hallo",
|
||||
},
|
||||
responseBodyType: &example{},
|
||||
hasError: false,
|
||||
path: "",
|
||||
},
|
||||
{
|
||||
statusCode: 400,
|
||||
body: `{"test" : "hallo"}`,
|
||||
responseBody: &example{},
|
||||
responseBodyType: &example{},
|
||||
hasError: false,
|
||||
path: "",
|
||||
},
|
||||
{
|
||||
statusCode: 200,
|
||||
body: `{"test" : "hallo"}`,
|
||||
hasError: true,
|
||||
responseBody: &example{},
|
||||
responseBodyType: &example{},
|
||||
path: "broken",
|
||||
},
|
||||
}
|
||||
|
||||
for _, testOject := range testsDoMethod {
|
||||
testServer := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
||||
|
||||
log.Infof("Got call from %s %s", req.Method, req.URL.String())
|
||||
|
||||
assert.Equal(t, req.Header.Get("PRIVATE-TOKEN"), "aToken")
|
||||
assert.Equal(t, req.Header.Get("Accept"), "application/json")
|
||||
|
||||
rw.WriteHeader(testOject.statusCode)
|
||||
rw.Header().Set("Content-Type", "application/json")
|
||||
|
||||
if _, err := rw.Write([]byte(testOject.body)); err != nil {
|
||||
log.Info(err)
|
||||
}
|
||||
|
||||
}))
|
||||
|
||||
req, err := http.NewRequest("POST", testServer.URL+testOject.path, nil)
|
||||
assert.NoError(t, err)
|
||||
|
||||
resp, err := util.Do(httpClient, req, testOject.responseBodyType)
|
||||
|
||||
assert.Equal(t, testOject.hasError, err != nil)
|
||||
|
||||
if !testOject.hasError {
|
||||
assert.Equal(t, testOject.statusCode, resp.StatusCode)
|
||||
assert.Equal(t, testOject.responseBody, testOject.responseBodyType)
|
||||
}
|
||||
testServer.Close()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user