2016-05-18 19:19:20 +00:00
|
|
|
package api_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/github/git-lfs/api"
|
2016-05-24 15:29:58 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2016-05-18 19:19:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestClientUsesLifecycleToExecuteSchemas(t *testing.T) {
|
|
|
|
schema := new(api.RequestSchema)
|
|
|
|
req := new(http.Request)
|
|
|
|
resp := new(api.HttpResponse)
|
|
|
|
|
|
|
|
lifecycle := new(MockLifecycle)
|
|
|
|
lifecycle.On("Build", schema).Return(req, nil).Once()
|
|
|
|
lifecycle.On("Execute", req, schema.Into).Return(resp, nil).Once()
|
|
|
|
lifecycle.On("Cleanup", resp).Return(nil).Once()
|
|
|
|
|
2016-05-25 19:31:12 +00:00
|
|
|
client := api.NewClient(lifecycle)
|
2016-05-18 19:19:20 +00:00
|
|
|
r1, err := client.Do(schema)
|
|
|
|
|
|
|
|
assert.Equal(t, resp, r1)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
lifecycle.AssertExpectations(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClientHaltsIfSchemaCannotBeBuilt(t *testing.T) {
|
|
|
|
schema := new(api.RequestSchema)
|
|
|
|
|
|
|
|
lifecycle := new(MockLifecycle)
|
|
|
|
lifecycle.On("Build", schema).Return(nil, errors.New("uh-oh!")).Once()
|
|
|
|
|
2016-05-25 19:31:12 +00:00
|
|
|
client := api.NewClient(lifecycle)
|
2016-05-18 19:19:20 +00:00
|
|
|
resp, err := client.Do(schema)
|
|
|
|
|
|
|
|
lifecycle.AssertExpectations(t)
|
|
|
|
assert.Nil(t, resp)
|
|
|
|
assert.Equal(t, "uh-oh!", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClientHaltsIfSchemaCannotBeExecuted(t *testing.T) {
|
|
|
|
schema := new(api.RequestSchema)
|
|
|
|
req := new(http.Request)
|
|
|
|
|
|
|
|
lifecycle := new(MockLifecycle)
|
|
|
|
lifecycle.On("Build", schema).Return(req, nil).Once()
|
|
|
|
lifecycle.On("Execute", req, schema.Into).Return(nil, errors.New("uh-oh!")).Once()
|
|
|
|
|
2016-05-25 19:31:12 +00:00
|
|
|
client := api.NewClient(lifecycle)
|
2016-05-18 19:19:20 +00:00
|
|
|
resp, err := client.Do(schema)
|
|
|
|
|
|
|
|
lifecycle.AssertExpectations(t)
|
|
|
|
assert.Nil(t, resp)
|
|
|
|
assert.Equal(t, "uh-oh!", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClientReturnsCleanupErrors(t *testing.T) {
|
|
|
|
schema := new(api.RequestSchema)
|
|
|
|
req := new(http.Request)
|
|
|
|
resp := new(api.HttpResponse)
|
|
|
|
|
|
|
|
lifecycle := new(MockLifecycle)
|
|
|
|
lifecycle.On("Build", schema).Return(req, nil).Once()
|
|
|
|
lifecycle.On("Execute", req, schema.Into).Return(resp, nil).Once()
|
|
|
|
lifecycle.On("Cleanup", resp).Return(errors.New("uh-oh!")).Once()
|
|
|
|
|
2016-05-25 19:31:12 +00:00
|
|
|
client := api.NewClient(lifecycle)
|
2016-05-18 19:19:20 +00:00
|
|
|
r1, err := client.Do(schema)
|
|
|
|
|
|
|
|
lifecycle.AssertExpectations(t)
|
|
|
|
assert.Nil(t, r1)
|
|
|
|
assert.Equal(t, "uh-oh!", err.Error())
|
|
|
|
}
|