test http client close

This commit is contained in:
Murat Kabilov 2017-06-09 11:34:08 +02:00
parent 80038239f7
commit 7d1c7924f2
5 changed files with 95 additions and 41 deletions

View File

@ -223,7 +223,7 @@ var unmarshalCluster = []struct {
TeamID: "ACID",
AllowedSourceRanges: []string{"127.0.0.1/32"},
NumberOfInstances: 2,
Users: map[string]UserFlags{"zalando": {"superuser", "createdb"}},
Users: map[string]userFlags{"zalando": {"superuser", "createdb"}},
MaintenanceWindows: []MaintenanceWindow{{
Everyday: false,
Weekday: time.Monday,

View File

@ -4,8 +4,6 @@ import (
"fmt"
"strings"
"database/sql"
"fmt"
"strings"
"k8s.io/client-go/pkg/api/v1"
"k8s.io/client-go/pkg/types"

View File

@ -39,10 +39,13 @@ type team struct {
InfrastructureAccounts []infrastructureAccount `json:"infrastructure-accounts"`
}
//
type httpClient interface {
Do(req *http.Request) (*http.Response, error)
}
type API struct {
httpClient
url string
httpClient *http.Client
logger *logrus.Entry
}
@ -58,23 +61,28 @@ func NewTeamsAPI(url string, log *logrus.Logger) *API {
}
// TeamInfo returns information about a given team using its ID and a token to authenticate to the API service.
func (t *API) TeamInfo(teamID, token string) (tm *team, er error) {
func (t *API) TeamInfo(teamID, token string) (tm *team, err error) {
var (
req *http.Request
resp *http.Response
)
url := fmt.Sprintf("%s/teams/%s", t.url, teamID)
t.logger.Debugf("Request url: %s", url)
req, err := http.NewRequest("GET", url, nil)
req, err = http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
return
}
req.Header.Add("Authorization", "Bearer "+token)
resp, err := t.httpClient.Do(req)
resp, err = t.httpClient.Do(req)
if err != nil {
return nil, err
return
}
defer func() {
if err:= resp.Body.Close(); err != nil {
er = fmt.Errorf("error when closing response; %v", err)
tm = nil
closeErr := resp.Body.Close()
if closeErr != nil {
err = fmt.Errorf("error when closing response: %v", closeErr)
}
}()
if resp.StatusCode != 200 {
@ -82,21 +90,27 @@ func (t *API) TeamInfo(teamID, token string) (tm *team, er error) {
d := json.NewDecoder(resp.Body)
err = d.Decode(&raw)
if err != nil {
return nil, fmt.Errorf("team API query failed with status code %d and malformed response: %v", resp.StatusCode, err)
err = fmt.Errorf("team API query failed with status code %d and malformed response: %v", resp.StatusCode, err)
return
}
if errMessage, ok := raw["error"]; ok {
return nil, fmt.Errorf("team API query failed with status code %d and message: '%v'", resp.StatusCode, string(errMessage))
err = fmt.Errorf("team API query failed with status code %d and message: '%v'", resp.StatusCode, string(errMessage))
return
}
err = fmt.Errorf("team API query failed with status code %d", resp.StatusCode)
return
}
return nil, fmt.Errorf("team API query failed with status code %d", resp.StatusCode)
}
teamInfo := &team{}
tm = &team{}
d := json.NewDecoder(resp.Body)
err = d.Decode(teamInfo)
err = d.Decode(tm)
if err != nil {
return nil, fmt.Errorf("could not parse team API response: %v", err)
err = fmt.Errorf("could not parse team API response: %v", err)
tm = nil
return
}
return teamInfo, nil
return
}

View File

@ -2,11 +2,12 @@ package teams
import (
"fmt"
"github.com/Sirupsen/logrus"
"net/http"
"net/http/httptest"
"reflect"
"testing"
"github.com/Sirupsen/logrus"
)
var (
@ -168,6 +169,56 @@ func TestInfo(t *testing.T) {
}
}
type mockHttpClient struct {
}
type mockBody struct {
}
func (b *mockBody) Read(p []byte) (n int, err error) {
return 2, nil
}
func (b *mockBody) Close() error {
return fmt.Errorf("close error")
}
func (c *mockHttpClient) Do(req *http.Request) (*http.Response, error) {
fmt.Printf("do request: %v", *req)
resp := http.Response{
Status: "200 OK",
StatusCode: 200,
ContentLength: 2,
Close: false,
Request: req,
}
resp.Body = &mockBody{}
return &resp, nil
}
func TestHttpClientClose(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("Authorization") != "Bearer "+token {
t.Errorf("Authorization token is wrong or not provided")
}
w.WriteHeader(200)
if _, err := fmt.Fprint(w, "{}"); err != nil {
t.Errorf("Error writing teams api response %v", err)
}
}))
api := NewTeamsAPI(ts.URL, logger)
api.httpClient = &mockHttpClient{}
_, err := api.TeamInfo("acid", token)
expError := fmt.Errorf("error when closing response: close error")
if err.Error() != expError.Error() {
t.Errorf("Expected error: %v, got: %v", expError, err)
}
}
func TestRequest(t *testing.T) {
for _, tc := range requestsURLtc {
api := NewTeamsAPI(tc.url, logger)

View File

@ -1,7 +1,6 @@
package util
import (
"fmt"
"reflect"
"testing"
@ -86,14 +85,6 @@ func TestPGUserPassword(t *testing.T) {
}
}
func TestPretty(t *testing.T) {
for _, tt := range prettyTest {
if actual := Pretty(tt.in); fmt.Sprintf("%v", actual) != tt.out {
t.Errorf("Pretty expected: %s, got: %s", tt.out, actual)
}
}
}
func TestPrettyDiff(t *testing.T) {
for _, tt := range prettyDiffTest {
if actual := PrettyDiff(tt.inA, tt.inB); actual != tt.out {