Skip to content

Commit f6a0137

Browse files
committed
api/http_lifecycle: use httputil's client, update tests
1 parent 00105ac commit f6a0137

File tree

2 files changed

+45
-31
lines changed

2 files changed

+45
-31
lines changed

api/http_lifecycle.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ import (
88
"io/ioutil"
99
"net/http"
1010
"net/url"
11+
12+
"github.com/github/git-lfs/auth"
13+
"github.com/github/git-lfs/config"
14+
"github.com/github/git-lfs/httputil"
1115
)
1216

1317
// HttpLifecycle serves as the default implementation of the Lifecycle interface
@@ -18,7 +22,7 @@ type HttpLifecycle struct {
1822
// relativized
1923
root *url.URL
2024
// client is the *http.Client used to execute these requests.
21-
client *http.Client
25+
client *httputil.HttpClient
2226
// authenticateRequests stores whether or not the HttpLifecycle should
2327
// authenticate its HTTP requests
2428
authenticateRequests bool
@@ -31,7 +35,7 @@ var _ Lifecycle = new(HttpLifecycle)
3135
func NewHttpLifecycle(root *url.URL) *HttpLifecycle {
3236
return &HttpLifecycle{
3337
root: root,
34-
client: new(http.Client),
38+
client: httputil.NewHttpClient(config.Config, root.Host),
3539
}
3640
}
3741

@@ -67,10 +71,9 @@ func (l *HttpLifecycle) Build(schema *RequestSchema) (*http.Request, error) {
6771
return nil, err
6872
}
6973

70-
// ASK(@sinbad): is this the correct usage?
71-
// if _, err = auth.GetCreds(req); err != nil {
72-
// return nil, err
73-
// }
74+
if _, err = auth.GetCreds(req); err != nil {
75+
return nil, err
76+
}
7477

7578
req.URL.RawQuery = l.queryParameters(schema).Encode()
7679

api/http_lifecycle_test.go

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,38 @@ import (
55
"net/http"
66
"net/http/httptest"
77
"net/url"
8-
"testing"
98

109
"github.com/github/git-lfs/api"
11-
"github.com/github/git-lfs/vendor/_nuts/github.com/stretchr/testify/assert"
10+
"github.com/stretchr/testify/suite"
1211
)
1312

1413
var (
1514
root, _ = url.Parse("https://example.com")
1615
)
1716

18-
func TestHttpLifecycleMakesRequestsAgainstAbsolutePath(t *testing.T) {
17+
type HttpLifecycleTestSuite struct {
18+
suite.Suite
19+
}
20+
21+
func (suite *HttpLifecycleTestSuite) SetupTest() {
22+
SetupTestCredentialsFunc()
23+
}
24+
25+
func (suite *HttpLifecycleTestSuite) TearDownTest() {
26+
RestoreCredentialsFunc()
27+
}
28+
29+
func (suite *HttpLifecycleTestSuite) TestHttpLifecycleMakesRequestsAgainstAbsolutePath() {
1930
l := api.NewHttpLifecycle(root)
2031
req, err := l.Build(&api.RequestSchema{
2132
Path: "/foo",
2233
})
2334

24-
assert.Nil(t, err)
25-
assert.Equal(t, "https://example.com/foo", req.URL.String())
35+
suite.Assert().Nil(err)
36+
suite.Assert().Equal("https://example.com/foo", req.URL.String())
2637
}
2738

28-
func TestHttpLifecycleAttachesQueryParameters(t *testing.T) {
39+
func (suite *HttpLifecycleTestSuite) TestHttpLifecycleAttachesQueryParameters() {
2940
l := api.NewHttpLifecycle(root)
3041
req, err := l.Build(&api.RequestSchema{
3142
Path: "/foo",
@@ -34,52 +45,52 @@ func TestHttpLifecycleAttachesQueryParameters(t *testing.T) {
3445
},
3546
})
3647

37-
assert.Nil(t, err)
38-
assert.Equal(t, "https://example.com/foo?a=b", req.URL.String())
48+
suite.Assert().Nil(err)
49+
suite.Assert().Equal("https://example.com/foo?a=b", req.URL.String())
3950
}
4051

41-
func TestHttpLifecycleAttachesBodyWhenPresent(t *testing.T) {
52+
func (suite *HttpLifecycleTestSuite) TestHttpLifecycleAttachesBodyWhenPresent() {
4253
l := api.NewHttpLifecycle(root)
4354
req, err := l.Build(&api.RequestSchema{
4455
Body: struct {
4556
Foo string `json:"foo"`
4657
}{"bar"},
4758
})
4859

49-
assert.Nil(t, err)
60+
suite.Assert().Nil(err)
5061

5162
body, err := ioutil.ReadAll(req.Body)
52-
assert.Nil(t, err)
53-
assert.Equal(t, "{\"foo\":\"bar\"}", string(body))
63+
suite.Assert().Nil(err)
64+
suite.Assert().Equal("{\"foo\":\"bar\"}", string(body))
5465
}
5566

56-
func TestHttpLifecycleDoesNotAttachBodyWhenEmpty(t *testing.T) {
67+
func (suite *HttpLifecycleTestSuite) TestHttpLifecycleDoesNotAttachBodyWhenEmpty() {
5768
l := api.NewHttpLifecycle(root)
5869
req, err := l.Build(&api.RequestSchema{})
5970

60-
assert.Nil(t, err)
61-
assert.Nil(t, req.Body)
71+
suite.Assert().Nil(err)
72+
suite.Assert().Nil(req.Body)
6273
}
6374

64-
func TestHttpLifecycleExecutesRequestWithoutBody(t *testing.T) {
75+
func (suite *HttpLifecycleTestSuite) TestHttpLifecycleExecutesRequestWithoutBody() {
6576
var called bool
6677
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
6778
called = true
6879

69-
assert.Equal(t, "/path", r.URL.RequestURI())
80+
suite.Assert().Equal("/path", r.URL.RequestURI())
7081
}))
7182
defer server.Close()
7283

7384
req, _ := http.NewRequest(http.MethodGet, server.URL+"/path", nil)
7485

75-
l := api.NewHttpLifecycle(nil)
86+
l := api.NewHttpLifecycle(root)
7687
_, err := l.Execute(req, nil)
7788

78-
assert.True(t, called)
79-
assert.Nil(t, err)
89+
suite.Assert().True(called)
90+
suite.Assert().Nil(err)
8091
}
8192

82-
func TestHttpLifecycleExecutesRequestWithBody(t *testing.T) {
93+
func (suite *HttpLifecycleTestSuite) TestHttpLifecycleExecutesRequestWithBody() {
8394
type Response struct {
8495
Foo string `json:"foo"`
8596
}
@@ -94,11 +105,11 @@ func TestHttpLifecycleExecutesRequestWithBody(t *testing.T) {
94105

95106
req, _ := http.NewRequest(http.MethodGet, server.URL+"/path", nil)
96107

97-
l := api.NewHttpLifecycle(nil)
108+
l := api.NewHttpLifecycle(root)
98109
resp := new(Response)
99110
_, err := l.Execute(req, resp)
100111

101-
assert.True(t, called)
102-
assert.Nil(t, err)
103-
assert.Equal(t, "bar", resp.Foo)
112+
suite.Assert().True(called)
113+
suite.Assert().Nil(err)
114+
suite.Assert().Equal("bar", resp.Foo)
104115
}

0 commit comments

Comments
 (0)