2016-05-18 19:19:20 +00:00
|
|
|
package api_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
2016-05-20 23:37:43 +00:00
|
|
|
"net/http"
|
2016-05-18 19:19:20 +00:00
|
|
|
"net/http/httptest"
|
2016-05-25 19:31:12 +00:00
|
|
|
"testing"
|
2016-05-18 19:19:20 +00:00
|
|
|
|
|
|
|
"github.com/github/git-lfs/api"
|
2016-05-25 19:31:12 +00:00
|
|
|
"github.com/github/git-lfs/config"
|
2016-05-25 19:38:39 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2016-05-18 19:19:20 +00:00
|
|
|
)
|
|
|
|
|
2016-05-25 19:31:12 +00:00
|
|
|
type NopEndpointSource struct {
|
|
|
|
Root string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *NopEndpointSource) Endpoint(op string) config.Endpoint {
|
|
|
|
return config.Endpoint{Url: e.Root}
|
|
|
|
}
|
|
|
|
|
2016-05-18 19:19:20 +00:00
|
|
|
var (
|
2016-05-25 19:31:12 +00:00
|
|
|
source = &NopEndpointSource{"https://example.com"}
|
2016-05-18 19:19:20 +00:00
|
|
|
)
|
|
|
|
|
2016-05-25 19:38:39 +00:00
|
|
|
func TestHttpLifecycleMakesRequestsAgainstAbsolutePath(t *testing.T) {
|
2016-05-23 19:34:02 +00:00
|
|
|
SetupTestCredentialsFunc()
|
2016-05-25 19:38:39 +00:00
|
|
|
defer RestoreCredentialsFunc()
|
2016-05-23 19:34:02 +00:00
|
|
|
|
2016-05-25 19:31:12 +00:00
|
|
|
l := api.NewHttpLifecycle(source)
|
2016-05-18 19:19:20 +00:00
|
|
|
req, err := l.Build(&api.RequestSchema{
|
2016-05-25 19:31:12 +00:00
|
|
|
Path: "/foo",
|
|
|
|
Operation: api.DownloadOperation,
|
2016-05-18 19:19:20 +00:00
|
|
|
})
|
|
|
|
|
2016-05-25 19:38:39 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, "https://example.com/foo", req.URL.String())
|
2016-05-18 19:19:20 +00:00
|
|
|
}
|
|
|
|
|
2016-05-25 19:38:39 +00:00
|
|
|
func TestHttpLifecycleAttachesQueryParameters(t *testing.T) {
|
|
|
|
SetupTestCredentialsFunc()
|
|
|
|
defer RestoreCredentialsFunc()
|
|
|
|
|
2016-05-25 19:31:12 +00:00
|
|
|
l := api.NewHttpLifecycle(source)
|
2016-05-18 19:19:20 +00:00
|
|
|
req, err := l.Build(&api.RequestSchema{
|
2016-05-25 19:31:12 +00:00
|
|
|
Path: "/foo",
|
|
|
|
Operation: api.DownloadOperation,
|
2016-05-18 19:19:20 +00:00
|
|
|
Query: map[string]string{
|
|
|
|
"a": "b",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2016-05-25 19:38:39 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, "https://example.com/foo?a=b", req.URL.String())
|
2016-05-18 19:19:20 +00:00
|
|
|
}
|
|
|
|
|
2016-05-25 19:38:39 +00:00
|
|
|
func TestHttpLifecycleAttachesBodyWhenPresent(t *testing.T) {
|
|
|
|
SetupTestCredentialsFunc()
|
|
|
|
defer RestoreCredentialsFunc()
|
|
|
|
|
2016-05-25 19:31:12 +00:00
|
|
|
l := api.NewHttpLifecycle(source)
|
2016-05-18 19:19:20 +00:00
|
|
|
req, err := l.Build(&api.RequestSchema{
|
2016-05-25 19:31:12 +00:00
|
|
|
Operation: api.DownloadOperation,
|
2016-05-18 19:19:20 +00:00
|
|
|
Body: struct {
|
|
|
|
Foo string `json:"foo"`
|
|
|
|
}{"bar"},
|
|
|
|
})
|
|
|
|
|
2016-05-25 19:38:39 +00:00
|
|
|
assert.Nil(t, err)
|
2016-05-18 19:19:20 +00:00
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(req.Body)
|
2016-05-25 19:38:39 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, "{\"foo\":\"bar\"}", string(body))
|
2016-05-18 19:19:20 +00:00
|
|
|
}
|
|
|
|
|
2016-05-25 19:38:39 +00:00
|
|
|
func TestHttpLifecycleDoesNotAttachBodyWhenEmpty(t *testing.T) {
|
|
|
|
SetupTestCredentialsFunc()
|
|
|
|
defer RestoreCredentialsFunc()
|
|
|
|
|
2016-05-25 19:31:12 +00:00
|
|
|
l := api.NewHttpLifecycle(source)
|
|
|
|
req, err := l.Build(&api.RequestSchema{
|
|
|
|
Operation: api.DownloadOperation,
|
|
|
|
})
|
2016-05-18 19:19:20 +00:00
|
|
|
|
2016-05-25 19:38:39 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Nil(t, req.Body)
|
2016-05-18 19:19:20 +00:00
|
|
|
}
|
|
|
|
|
2016-05-25 19:38:39 +00:00
|
|
|
func TestHttpLifecycleErrsWithoutOperation(t *testing.T) {
|
|
|
|
SetupTestCredentialsFunc()
|
|
|
|
defer RestoreCredentialsFunc()
|
|
|
|
|
2016-05-25 19:31:12 +00:00
|
|
|
l := api.NewHttpLifecycle(source)
|
|
|
|
req, err := l.Build(&api.RequestSchema{
|
|
|
|
Path: "/foo",
|
|
|
|
})
|
|
|
|
|
2016-05-25 19:38:39 +00:00
|
|
|
assert.Equal(t, api.ErrNoOperationGiven, err)
|
|
|
|
assert.Nil(t, req)
|
2016-05-25 19:31:12 +00:00
|
|
|
}
|
|
|
|
|
2016-05-25 19:38:39 +00:00
|
|
|
func TestHttpLifecycleExecutesRequestWithoutBody(t *testing.T) {
|
|
|
|
SetupTestCredentialsFunc()
|
|
|
|
defer RestoreCredentialsFunc()
|
|
|
|
|
2016-05-18 19:19:20 +00:00
|
|
|
var called bool
|
|
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
called = true
|
|
|
|
|
2016-05-25 19:38:39 +00:00
|
|
|
assert.Equal(t, "/path", r.URL.RequestURI())
|
2016-05-18 19:19:20 +00:00
|
|
|
}))
|
|
|
|
defer server.Close()
|
|
|
|
|
2016-05-24 15:34:51 +00:00
|
|
|
req, _ := http.NewRequest("GET", server.URL+"/path", nil)
|
2016-05-18 19:19:20 +00:00
|
|
|
|
2016-05-25 19:31:12 +00:00
|
|
|
l := api.NewHttpLifecycle(source)
|
2016-05-18 19:19:20 +00:00
|
|
|
_, err := l.Execute(req, nil)
|
|
|
|
|
2016-05-25 19:38:39 +00:00
|
|
|
assert.True(t, called)
|
|
|
|
assert.Nil(t, err)
|
2016-05-18 19:19:20 +00:00
|
|
|
}
|
|
|
|
|
2016-05-25 19:38:39 +00:00
|
|
|
func TestHttpLifecycleExecutesRequestWithBody(t *testing.T) {
|
|
|
|
SetupTestCredentialsFunc()
|
|
|
|
defer RestoreCredentialsFunc()
|
|
|
|
|
2016-05-18 19:19:20 +00:00
|
|
|
type Response struct {
|
|
|
|
Foo string `json:"foo"`
|
|
|
|
}
|
|
|
|
|
|
|
|
var called bool
|
|
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
called = true
|
|
|
|
|
|
|
|
w.Write([]byte("{\"foo\":\"bar\"}"))
|
|
|
|
}))
|
|
|
|
defer server.Close()
|
|
|
|
|
2016-05-24 15:34:51 +00:00
|
|
|
req, _ := http.NewRequest("GET", server.URL+"/path", nil)
|
2016-05-18 19:19:20 +00:00
|
|
|
|
2016-05-25 19:31:12 +00:00
|
|
|
l := api.NewHttpLifecycle(source)
|
2016-05-18 19:19:20 +00:00
|
|
|
resp := new(Response)
|
|
|
|
_, err := l.Execute(req, resp)
|
|
|
|
|
2016-05-25 19:38:39 +00:00
|
|
|
assert.True(t, called)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, "bar", resp.Foo)
|
2016-05-18 19:19:20 +00:00
|
|
|
}
|