api/response: HttpResponse tests

This commit is contained in:
Taylor Blau 2016-05-18 11:43:56 -06:00
parent 91c1b2d360
commit ff2a96b09d
4 changed files with 39 additions and 22 deletions

@ -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)
}

@ -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
}

27
api/http_response_test.go Normal file

@ -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())
}

@ -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
}