Skip to content
This repository was archived by the owner on Oct 5, 2023. It is now read-only.

adds authenticated transport and non-standard api path connections #15

Merged
merged 9 commits into from
May 2, 2019
20 changes: 16 additions & 4 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"io/ioutil"
"net/http"
gohttp "net/http"
"os"
"path"
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -108,17 +109,21 @@ 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) {},
}

// We don't support redirects.
api.httpcli.CheckRedirect = func(_ *gohttp.Request, _ []*gohttp.Request) error {
return fmt.Errorf("unexpected redirect")
}

return api, nil
}

Expand All @@ -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,
}
}

Expand Down
40 changes: 38 additions & 2 deletions api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
}
}