From ff2a96b09d43fcb29149e36fd42eae21ab49ade9 Mon Sep 17 00:00:00 2001 From: Taylor Blau Date: Wed, 18 May 2016 11:43:56 -0600 Subject: [PATCH] api/response: HttpResponse tests --- api/client.go | 10 +++------- api/http_lifecycle.go | 12 +++++------- api/http_response_test.go | 27 +++++++++++++++++++++++++++ api/lifecycle.go | 12 ++++-------- 4 files changed, 39 insertions(+), 22 deletions(-) create mode 100644 api/http_response_test.go diff --git a/api/client.go b/api/client.go index e1c16816..4fb7af64 100644 --- a/api/client.go +++ b/api/client.go @@ -1,11 +1,7 @@ // NOTE: Subject to change, do not rely on this package from outside git-lfs source package api -import ( - "net/url" - - "github.com/github/git-lfs/api" -) +import "net/url" const ( MediaType = "application/vnd.git-lfs+json; charset=utf-8" @@ -64,7 +60,7 @@ func NewClient(root string) (*Client, error) { // If no error occured, an some api.Response implementation will be returned, // along with a `nil` error. At this point, the body of the response has been // serialized into `schema.Into`, and the body is closed. -func (c *Client) Do(schema *api.RequestSchema) (api.Response, error) { +func (c *Client) Do(schema *RequestSchema) (Response, error) { req, err := c.http.Build(schema) if err != nil { return nil, err @@ -75,5 +71,5 @@ func (c *Client) Do(schema *api.RequestSchema) (api.Response, error) { return nil, err } - return c.http.Cleanup(resp) + return resp, c.http.Cleanup(resp) } diff --git a/api/http_lifecycle.go b/api/http_lifecycle.go index 0d06610b..6d59fe46 100644 --- a/api/http_lifecycle.go +++ b/api/http_lifecycle.go @@ -8,8 +8,6 @@ import ( "io/ioutil" "net/http" "net/url" - - "github.com/github/git-lfs/api" ) // HttpLifecycle serves as the default implementation of the Lifecycle interface @@ -47,7 +45,7 @@ func NewHttpLifecycle(root *url.URL) *HttpLifecycle { // // Finally, all of these components are combined together and the resulting // request is returned. -func (l *HttpLifecycle) Build(schema *api.RequestSchema) (*http.Request, error) { +func (l *HttpLifecycle) Build(schema *RequestSchema) (*http.Request, error) { path, err := l.AbsolutePath(schema.Path) if err != nil { return nil, err @@ -76,8 +74,8 @@ func (l *HttpLifecycle) Build(schema *api.RequestSchema) (*http.Request, error) // // Otherwise, the api.Response is returned, along with no error, signaling that // the request completed successfully. -func (l *HttpLifecycle) Execute(req *http.Request, into interface{}) (api.Response, error) { - resp, err := l.c.Do(req) +func (l *HttpLifecycle) Execute(req *http.Request, into interface{}) (Response, error) { + resp, err := l.client.Do(req) if err != nil { return nil, err } @@ -94,7 +92,7 @@ func (l *HttpLifecycle) Execute(req *http.Request, into interface{}) (api.Respon // Cleanup implements the Lifecycle.Cleanup function by closing the Body // attached to the repsonse. -func (l *HttpLifecycle) Cleanup(resp api.Response) error { +func (l *HttpLifecycle) Cleanup(resp Response) error { return resp.Body().Close() } @@ -117,7 +115,7 @@ func (l *HttpLifecycle) AbsolutePath(path string) (*url.URL, error) { // // If an error was encountered while attempting to marshal the body, then that // will be returned instead, along with a nil io.Reader. -func (l *HttpLifecycle) Body(schema *api.RequestSchema) (io.ReadCloser, error) { +func (l *HttpLifecycle) Body(schema *RequestSchema) (io.ReadCloser, error) { if schema.Body == nil { return nil, nil } diff --git a/api/http_response_test.go b/api/http_response_test.go new file mode 100644 index 00000000..6a858eab --- /dev/null +++ b/api/http_response_test.go @@ -0,0 +1,27 @@ +package api_test + +import ( + "bytes" + "io/ioutil" + "net/http" + "testing" + + "github.com/github/git-lfs/api" + "github.com/github/git-lfs/vendor/_nuts/github.com/technoweenie/assert" +) + +func TestWrappedHttpResponsesMatchInternal(t *testing.T) { + resp := &http.Response{ + Status: "200 OK", + StatusCode: 200, + Proto: "HTTP/1.1", + Body: ioutil.NopCloser(new(bytes.Buffer)), + } + wrapped := api.WrapHttpResponse(resp) + + assert.Equal(t, resp.Status, wrapped.Status()) + assert.Equal(t, resp.StatusCode, wrapped.StatusCode()) + assert.Equal(t, resp.Proto, wrapped.Proto()) + assert.Equal(t, resp.Body, wrapped.Body()) + assert.Equal(t, resp.Header, wrapped.Header()) +} diff --git a/api/lifecycle.go b/api/lifecycle.go index e75487ea..5d55ead6 100644 --- a/api/lifecycle.go +++ b/api/lifecycle.go @@ -1,11 +1,7 @@ // NOTE: Subject to change, do not rely on this package from outside git-lfs source package api -import ( - "net/http" - - "github.com/github/git-lfs/api" -) +import "net/http" // TODO(taylor): extract interface for *http.Request; update methods. @@ -18,17 +14,17 @@ import ( // lifecycle of a request in a platform-agnostic fashion. type Lifecycle interface { // Build creates a sendable request by using the given RequestSchema. - Build(req *api.RequestSchema) (*http.Request, error) + Build(req *RequestSchema) (*http.Request, error) // Execute transforms generated request into a wrapped repsonse, (and // optionally an error, if the request failed), and serializes the // response into the `into interface{}`, if one was provided. - Execute(req *http.Request, into interface{}) (api.Response, error) + Execute(req *http.Request, into interface{}) (Response, error) // Cleanup is called after the request has been completed and its // response has been processed. It is meant to preform any post-request // actions necessary, like closing or resetting the connection. If an // error was encountered in doin this operation, it should be returned // from this method, or otherwise nil. - Cleanup(resp api.Response) error + Cleanup(resp Response) error }