diff options
author | Igor Drozdov <idrozdov@gitlab.com> | 2019-04-23 14:07:38 +0300 |
---|---|---|
committer | Igor Drozdov <idrozdov@gitlab.com> | 2019-04-23 14:23:31 +0300 |
commit | fbaba66e124441d0e283fd4e2966d7af790ce23a (patch) | |
tree | b7560b534f9290b4f9b341f956c22d10d7db95dc /go/internal/gitlabnet | |
parent | 9ed567b6fd14ef389df11b46be3022a90eda68ec (diff) | |
download | gitlab-shell-id-api-regular-http.tar.gz |
Move http client building to configid-api-regular-http
Diffstat (limited to 'go/internal/gitlabnet')
-rw-r--r-- | go/internal/gitlabnet/client.go | 50 | ||||
-rw-r--r-- | go/internal/gitlabnet/client_test.go | 12 | ||||
-rw-r--r-- | go/internal/gitlabnet/discover/client.go | 2 | ||||
-rw-r--r-- | go/internal/gitlabnet/httpclient.go | 69 | ||||
-rw-r--r-- | go/internal/gitlabnet/httpclient_test.go | 15 | ||||
-rw-r--r-- | go/internal/gitlabnet/twofactorrecover/client.go | 2 |
6 files changed, 43 insertions, 107 deletions
diff --git a/go/internal/gitlabnet/client.go b/go/internal/gitlabnet/client.go index 839f206..c0f7f97 100644 --- a/go/internal/gitlabnet/client.go +++ b/go/internal/gitlabnet/client.go @@ -17,25 +17,24 @@ const ( secretHeaderName = "Gitlab-Shared-Secret" ) -type GitlabClient interface { - Get(path string) (*http.Response, error) - Post(path string, data interface{}) (*http.Response, error) -} - type ErrorResponse struct { Message string `json:"message"` } -func GetClient(config *config.Config) (GitlabClient, error) { - url := config.GitlabUrl - if strings.HasPrefix(url, UnixSocketProtocol) { - return buildSocketClient(config), nil - } - if strings.HasPrefix(url, HttpProtocol) { - return buildHttpClient(config), nil +type GitlabClient struct { + httpClient *http.Client + config *config.Config + host string +} + +func GetClient(config *config.Config) (*GitlabClient, error) { + client := config.GetHttpClient() + + if client == nil { + return nil, fmt.Errorf("Unsupported protocol") } - return nil, fmt.Errorf("Unsupported protocol") + return &GitlabClient{httpClient: client.HttpClient, config: config, host: client.Host}, nil } func normalizePath(path string) string { @@ -85,13 +84,32 @@ func parseError(resp *http.Response) error { } -func doRequest(client *http.Client, config *config.Config, request *http.Request) (*http.Response, error) { - encodedSecret := base64.StdEncoding.EncodeToString([]byte(config.Secret)) +func (c *GitlabClient) Get(path string) (*http.Response, error) { + return c.doRequest("GET", path, nil) +} + +func (c *GitlabClient) Post(path string, data interface{}) (*http.Response, error) { + return c.doRequest("POST", path, data) +} + +func (c *GitlabClient) doRequest(method, path string, data interface{}) (*http.Response, error) { + request, err := newRequest(method, c.host, path, data) + if err != nil { + return nil, err + } + + user, password := c.config.HttpSettings.User, c.config.HttpSettings.Password + if user != "" && password != "" { + request.SetBasicAuth(user, password) + } + + encodedSecret := base64.StdEncoding.EncodeToString([]byte(c.config.Secret)) request.Header.Set(secretHeaderName, encodedSecret) request.Header.Add("Content-Type", "application/json") + request.Close = true - response, err := client.Do(request) + response, err := c.httpClient.Do(request) if err != nil { return nil, fmt.Errorf("Internal API unreachable") } diff --git a/go/internal/gitlabnet/client_test.go b/go/internal/gitlabnet/client_test.go index 5925273..f9aa289 100644 --- a/go/internal/gitlabnet/client_test.go +++ b/go/internal/gitlabnet/client_test.go @@ -98,7 +98,7 @@ func TestClients(t *testing.T) { } } -func testSuccessfulGet(t *testing.T, client GitlabClient) { +func testSuccessfulGet(t *testing.T, client *GitlabClient) { t.Run("Successful get", func(t *testing.T) { response, err := client.Get("/hello") defer response.Body.Close() @@ -112,7 +112,7 @@ func testSuccessfulGet(t *testing.T, client GitlabClient) { }) } -func testSuccessfulPost(t *testing.T, client GitlabClient) { +func testSuccessfulPost(t *testing.T, client *GitlabClient) { t.Run("Successful Post", func(t *testing.T) { data := map[string]string{"key": "value"} @@ -128,7 +128,7 @@ func testSuccessfulPost(t *testing.T, client GitlabClient) { }) } -func testMissing(t *testing.T, client GitlabClient) { +func testMissing(t *testing.T, client *GitlabClient) { t.Run("Missing error for GET", func(t *testing.T) { response, err := client.Get("/missing") assert.EqualError(t, err, "Internal API error (404)") @@ -142,7 +142,7 @@ func testMissing(t *testing.T, client GitlabClient) { }) } -func testErrorMessage(t *testing.T, client GitlabClient) { +func testErrorMessage(t *testing.T, client *GitlabClient) { t.Run("Error with message for GET", func(t *testing.T) { response, err := client.Get("/error") assert.EqualError(t, err, "Don't do that") @@ -156,7 +156,7 @@ func testErrorMessage(t *testing.T, client GitlabClient) { }) } -func testBrokenRequest(t *testing.T, client GitlabClient) { +func testBrokenRequest(t *testing.T, client *GitlabClient) { t.Run("Broken request for GET", func(t *testing.T) { response, err := client.Get("/broken") assert.EqualError(t, err, "Internal API unreachable") @@ -170,7 +170,7 @@ func testBrokenRequest(t *testing.T, client GitlabClient) { }) } -func testAuthenticationHeader(t *testing.T, client GitlabClient) { +func testAuthenticationHeader(t *testing.T, client *GitlabClient) { t.Run("Authentication headers for GET", func(t *testing.T) { response, err := client.Get("/auth") defer response.Body.Close() diff --git a/go/internal/gitlabnet/discover/client.go b/go/internal/gitlabnet/discover/client.go index e84b1b4..1266379 100644 --- a/go/internal/gitlabnet/discover/client.go +++ b/go/internal/gitlabnet/discover/client.go @@ -13,7 +13,7 @@ import ( type Client struct { config *config.Config - client gitlabnet.GitlabClient + client *gitlabnet.GitlabClient } type Response struct { diff --git a/go/internal/gitlabnet/httpclient.go b/go/internal/gitlabnet/httpclient.go deleted file mode 100644 index de40051..0000000 --- a/go/internal/gitlabnet/httpclient.go +++ /dev/null @@ -1,69 +0,0 @@ -package gitlabnet - -import ( - "context" - "net" - "net/http" - "strings" - - "gitlab.com/gitlab-org/gitlab-shell/go/internal/config" -) - -const ( - socketBaseUrl = "http://unix" - UnixSocketProtocol = "http+unix://" - HttpProtocol = "http://" -) - -type GitlabHttpClient struct { - httpClient *http.Client - config *config.Config - host string -} - -func buildSocketClient(config *config.Config) *GitlabHttpClient { - path := strings.TrimPrefix(config.GitlabUrl, UnixSocketProtocol) - transport := &http.Transport{ - DialContext: func(ctx context.Context, _, _ string) (net.Conn, error) { - dialer := net.Dialer{} - return dialer.DialContext(ctx, "unix", path) - }, - } - - return buildClient(config, transport, socketBaseUrl) -} - -func buildHttpClient(config *config.Config) *GitlabHttpClient { - return buildClient(config, &http.Transport{}, config.GitlabUrl) -} - -func buildClient(config *config.Config, transport *http.Transport, host string) *GitlabHttpClient { - httpClient := &http.Client{ - Transport: transport, - Timeout: config.HttpSettings.ReadTimeout(), - } - - return &GitlabHttpClient{httpClient: httpClient, config: config, host: host} -} - -func (c *GitlabHttpClient) Get(path string) (*http.Response, error) { - return c.doRequest("GET", path, nil) -} - -func (c *GitlabHttpClient) Post(path string, data interface{}) (*http.Response, error) { - return c.doRequest("POST", path, data) -} - -func (c *GitlabHttpClient) doRequest(method, path string, data interface{}) (*http.Response, error) { - request, err := newRequest(method, c.host, path, data) - if err != nil { - return nil, err - } - - user, password := c.config.HttpSettings.User, c.config.HttpSettings.Password - if user != "" && password != "" { - request.SetBasicAuth(user, password) - } - - return doRequest(c.httpClient, c.config, request) -} diff --git a/go/internal/gitlabnet/httpclient_test.go b/go/internal/gitlabnet/httpclient_test.go index 5b01b24..885a6d1 100644 --- a/go/internal/gitlabnet/httpclient_test.go +++ b/go/internal/gitlabnet/httpclient_test.go @@ -7,7 +7,6 @@ import ( "net/http" "strings" "testing" - "time" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -86,19 +85,7 @@ func TestEmptyBasicAuthSettings(t *testing.T) { require.NoError(t, err) } -func TestReadTimeoutSetting(t *testing.T) { - expectedTimeout := 500 - - config := &config.Config{HttpSettings: config.HttpSettingsConfig{ReadTimeoutSeconds: uint64(expectedTimeout)}} - - client := buildHttpClient(config) - assert.Equal(t, time.Duration(expectedTimeout)*time.Second, client.httpClient.Timeout) - - socketClient := buildSocketClient(config) - assert.Equal(t, time.Duration(expectedTimeout)*time.Second, socketClient.httpClient.Timeout) -} - -func setup(t *testing.T, config *config.Config, requests []testserver.TestRequestHandler) (GitlabClient, func()) { +func setup(t *testing.T, config *config.Config, requests []testserver.TestRequestHandler) (*GitlabClient, func()) { cleanup, url, err := testserver.StartHttpServer(requests) require.NoError(t, err) diff --git a/go/internal/gitlabnet/twofactorrecover/client.go b/go/internal/gitlabnet/twofactorrecover/client.go index 2e47c64..d26b141 100644 --- a/go/internal/gitlabnet/twofactorrecover/client.go +++ b/go/internal/gitlabnet/twofactorrecover/client.go @@ -15,7 +15,7 @@ import ( type Client struct { config *config.Config - client gitlabnet.GitlabClient + client *gitlabnet.GitlabClient } type Response struct { |