lfsapi: teach Client how to build a request with an endpoint.
This commit is contained in:
parent
e1677db1ec
commit
56067e3830
@ -15,6 +15,30 @@ import (
|
||||
|
||||
var UserAgent = "git-lfs"
|
||||
|
||||
func (c *Client) NewRequest(method string, e Endpoint, suffix string, body interface{}) (*http.Request, error) {
|
||||
req, err := http.NewRequest(method, joinURL(e.Url, suffix), nil)
|
||||
if err != nil {
|
||||
return req, err
|
||||
}
|
||||
|
||||
if body != nil {
|
||||
if merr := MarshalToRequest(req, body); merr != nil {
|
||||
return req, merr
|
||||
}
|
||||
}
|
||||
|
||||
return req, err
|
||||
}
|
||||
|
||||
const slash = "/"
|
||||
|
||||
func joinURL(prefix, suffix string) string {
|
||||
if strings.HasSuffix(prefix, slash) {
|
||||
return prefix + suffix
|
||||
}
|
||||
return prefix + slash + suffix
|
||||
}
|
||||
|
||||
func (c *Client) Do(req *http.Request) (*http.Response, error) {
|
||||
req.Header.Set("User-Agent", UserAgent)
|
||||
|
||||
|
@ -2,6 +2,7 @@ package lfsapi
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"sync/atomic"
|
||||
@ -159,3 +160,24 @@ func TestNewClientWithOSSSLVerify(t *testing.T) {
|
||||
assert.True(t, c.SkipSSLVerify)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewRequest(t *testing.T) {
|
||||
tests := [][]string{
|
||||
{"https://example.com", "a", "https://example.com/a"},
|
||||
{"https://example.com/", "a", "https://example.com/a"},
|
||||
{"https://example.com/a", "b", "https://example.com/a/b"},
|
||||
{"https://example.com/a/", "b", "https://example.com/a/b"},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
c, err := NewClient(nil, testEnv(map[string]string{
|
||||
"lfs.url": test[0],
|
||||
}))
|
||||
require.Nil(t, err)
|
||||
|
||||
req, err := c.NewRequest("POST", c.Endpoints.Endpoint("", ""), test[1], nil)
|
||||
require.Nil(t, err)
|
||||
assert.Equal(t, "POST", req.Method)
|
||||
assert.Equal(t, test[2], req.URL.String(), fmt.Sprintf("endpoint: %s, suffix: %s, expected: %s", test[0], test[1], test[2]))
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user