Merge pull request ipfs/go-ipfs-http-client#15 from RTradeLtd/client/auth

adds authenticated transport and non-standard api path connections

This commit was moved from ipfs/go-ipfs-http-client@b9c8a2ffa7
This commit is contained in:
Łukasz Magiera 2019-05-02 16:40:36 +02:00 committed by GitHub
commit fd9d62faf2
2 changed files with 54 additions and 6 deletions

View File

@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"io/ioutil"
"net/http"
gohttp "net/http"
"os"
"path"
@ -32,9 +33,9 @@ var ErrApiNotFound = errors.New("ipfs api address could not be found")
// For interface docs see
// https://godoc.org/github.com/ipfs/interface-go-ipfs-core#CoreAPI
type HttpApi struct {
url string
httpcli gohttp.Client
url string
httpcli gohttp.Client
Headers http.Header
applyGlobal func(*RequestBuilder)
}
@ -108,9 +109,14 @@ func NewApiWithClient(a ma.Multiaddr, c *gohttp.Client) (*HttpApi, error) {
}
}
return NewURLApiWithClient(url, c)
}
func NewURLApiWithClient(url string, c *gohttp.Client) (*HttpApi, error) {
api := &HttpApi{
url: url,
httpcli: *c,
Headers: make(map[string][]string),
applyGlobal: func(*RequestBuilder) {},
}
@ -118,7 +124,6 @@ func NewApiWithClient(a ma.Multiaddr, c *gohttp.Client) (*HttpApi, error) {
api.httpcli.CheckRedirect = func(_ *gohttp.Request, _ []*gohttp.Request) error {
return fmt.Errorf("unexpected redirect")
}
return api, nil
}
@ -139,10 +144,17 @@ func (api *HttpApi) WithOptions(opts ...caopts.ApiOption) (iface.CoreAPI, error)
}
func (api *HttpApi) Request(command string, args ...string) *RequestBuilder {
headers := make(map[string]string)
if api.Headers != nil {
for k := range api.Headers {
headers[k] = api.Headers.Get(k)
}
}
return &RequestBuilder{
command: command,
args: args,
shell: api,
headers: headers,
}
}

View File

@ -3,18 +3,23 @@ package httpapi
import (
"context"
"io/ioutil"
"net/http"
gohttp "net/http"
"net/http/httptest"
"os"
"strconv"
"strings"
"sync"
"testing"
"time"
"github.com/ipfs/interface-go-ipfs-core"
iface "github.com/ipfs/interface-go-ipfs-core"
"github.com/ipfs/interface-go-ipfs-core/path"
"github.com/ipfs/interface-go-ipfs-core/tests"
local "github.com/ipfs/iptb-plugins/local"
"github.com/ipfs/iptb/testbed"
"github.com/ipfs/iptb/testbed/interfaces"
testbedi "github.com/ipfs/iptb/testbed/interfaces"
ma "github.com/multiformats/go-multiaddr"
)
@ -208,3 +213,34 @@ func TestHttpApi(t *testing.T) {
tests.TestApi(newNodeProvider(ctx))(t)
}
func Test_NewURLApiWithClient_With_Headers(t *testing.T) {
var (
headerToTest = "Test-Header"
expectedHeaderValue = "thisisaheadertest"
)
ts := httptest.NewServer(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
val := r.Header.Get(headerToTest)
if val != expectedHeaderValue {
w.WriteHeader(400)
return
}
http.ServeContent(w, r, "", time.Now(), strings.NewReader("test"))
}),
)
defer ts.Close()
api, err := NewURLApiWithClient(ts.URL, &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
DisableKeepAlives: true,
},
})
if err != nil {
t.Fatal(err)
}
api.Headers.Set(headerToTest, expectedHeaderValue)
if err := api.Pin().Rm(context.Background(), path.New("/ipfs/QmS4ustL54uo8FzR9455qaxZwuMiUhyvMcX9Ba8nUH4uVv")); err != nil {
t.Fatal(err)
}
}