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

Commit b9c8a2f

Browse files
authored
Merge pull request #15 from RTradeLtd/client/auth
adds authenticated transport and non-standard api path connections
2 parents 6062f4d + 3201304 commit b9c8a2f

File tree

2 files changed

+54
-6
lines changed

2 files changed

+54
-6
lines changed

api.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"fmt"
66
"io/ioutil"
7+
"net/http"
78
gohttp "net/http"
89
"os"
910
"path"
@@ -32,9 +33,9 @@ var ErrApiNotFound = errors.New("ipfs api address could not be found")
3233
// For interface docs see
3334
// https://godoc.org/github.com/ipfs/interface-go-ipfs-core#CoreAPI
3435
type HttpApi struct {
35-
url string
36-
httpcli gohttp.Client
37-
36+
url string
37+
httpcli gohttp.Client
38+
Headers http.Header
3839
applyGlobal func(*RequestBuilder)
3940
}
4041

@@ -108,17 +109,21 @@ func NewApiWithClient(a ma.Multiaddr, c *gohttp.Client) (*HttpApi, error) {
108109
}
109110
}
110111

112+
return NewURLApiWithClient(url, c)
113+
}
114+
115+
func NewURLApiWithClient(url string, c *gohttp.Client) (*HttpApi, error) {
111116
api := &HttpApi{
112117
url: url,
113118
httpcli: *c,
119+
Headers: make(map[string][]string),
114120
applyGlobal: func(*RequestBuilder) {},
115121
}
116122

117123
// We don't support redirects.
118124
api.httpcli.CheckRedirect = func(_ *gohttp.Request, _ []*gohttp.Request) error {
119125
return fmt.Errorf("unexpected redirect")
120126
}
121-
122127
return api, nil
123128
}
124129

@@ -139,10 +144,17 @@ func (api *HttpApi) WithOptions(opts ...caopts.ApiOption) (iface.CoreAPI, error)
139144
}
140145

141146
func (api *HttpApi) Request(command string, args ...string) *RequestBuilder {
147+
headers := make(map[string]string)
148+
if api.Headers != nil {
149+
for k := range api.Headers {
150+
headers[k] = api.Headers.Get(k)
151+
}
152+
}
142153
return &RequestBuilder{
143154
command: command,
144155
args: args,
145156
shell: api,
157+
headers: headers,
146158
}
147159
}
148160

api_test.go

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,23 @@ package httpapi
33
import (
44
"context"
55
"io/ioutil"
6+
"net/http"
67
gohttp "net/http"
8+
"net/http/httptest"
79
"os"
810
"strconv"
11+
"strings"
912
"sync"
1013
"testing"
14+
"time"
1115

12-
"github.com/ipfs/interface-go-ipfs-core"
16+
iface "github.com/ipfs/interface-go-ipfs-core"
1317
"github.com/ipfs/interface-go-ipfs-core/path"
18+
1419
"github.com/ipfs/interface-go-ipfs-core/tests"
1520
local "github.com/ipfs/iptb-plugins/local"
1621
"github.com/ipfs/iptb/testbed"
17-
"github.com/ipfs/iptb/testbed/interfaces"
22+
testbedi "github.com/ipfs/iptb/testbed/interfaces"
1823
ma "github.com/multiformats/go-multiaddr"
1924
)
2025

@@ -208,3 +213,34 @@ func TestHttpApi(t *testing.T) {
208213

209214
tests.TestApi(newNodeProvider(ctx))(t)
210215
}
216+
217+
func Test_NewURLApiWithClient_With_Headers(t *testing.T) {
218+
var (
219+
headerToTest = "Test-Header"
220+
expectedHeaderValue = "thisisaheadertest"
221+
)
222+
ts := httptest.NewServer(
223+
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
224+
val := r.Header.Get(headerToTest)
225+
if val != expectedHeaderValue {
226+
w.WriteHeader(400)
227+
return
228+
}
229+
http.ServeContent(w, r, "", time.Now(), strings.NewReader("test"))
230+
}),
231+
)
232+
defer ts.Close()
233+
api, err := NewURLApiWithClient(ts.URL, &http.Client{
234+
Transport: &http.Transport{
235+
Proxy: http.ProxyFromEnvironment,
236+
DisableKeepAlives: true,
237+
},
238+
})
239+
if err != nil {
240+
t.Fatal(err)
241+
}
242+
api.Headers.Set(headerToTest, expectedHeaderValue)
243+
if err := api.Pin().Rm(context.Background(), path.New("/ipfs/QmS4ustL54uo8FzR9455qaxZwuMiUhyvMcX9Ba8nUH4uVv")); err != nil {
244+
t.Fatal(err)
245+
}
246+
}

0 commit comments

Comments
 (0)