@cryptotaxi247 / kubo / commits / 89600e62c

api: add wip api test

This commit was moved from ipfs/go-ipfs-http-client@2b48dd36ad170149bd4c62a4194587bf7c704f7e

Alex committed May 1, 2019 at 13:41 UTC 89600e62ce160cf5f3aa2a752b3806a273d1d142
1 file changed +46 -16
client/httpapi/api_test.go
+46 -16
@@ -3,11 +3,13 @@ package httpapi
3 import (
4 "context"
5 "io/ioutil"
6 + "net/http"
7 gohttp "net/http"
8 "os"
9 "strconv"
10 "sync"
11 "testing"
12 + "time"
13
14 iface "github.com/ipfs/interface-go-ipfs-core"
15 "github.com/ipfs/interface-go-ipfs-core/tests"
@@ -212,23 +214,51 @@ func TestHttpApi(t *testing.T) {
214 tests.TestApi(newNodeProvider(ctx))(t)
215 }
216
215 -func TestDirectAPI(t *testing.T) {
216 - type args struct {
217 - url, header, value string
217 +func Test_NewURLApiWithClient(t *testing.T) {
218 + t.Skip()
219 + var (
220 + url = "127.0.0.1:65501"
221 + headerToTest = "Test-Header"
222 + expectedHeaderValue = "thisisaheadertest"
223 + )
224 + server, err := testHTTPServer(url, headerToTest, expectedHeaderValue)
225 + if err != nil {
226 + t.Fatal(err)
227 }
219 - tests := []struct {
220 - name string
221 - args args
222 - }{
223 - {"Success", args{"http://127.0.0.1:5001/foo/bar/api/v0", "Authorization", "Bearer token"}},
228 + defer server.Close()
229 + go func() {
230 + server.ListenAndServe()
231 + }()
232 + time.Sleep(time.Second * 2)
233 + api, err := NewURLApiWithClient(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 }
225 - for _, tt := range tests {
226 - t.Run(tt.name, func(t *testing.T) {
227 - api, err := NewDirectAPIClient(tt.args.url)
228 - if err != nil {
229 - t.Fatal(err)
230 - }
231 - api = api.WithAuthorization(tt.args.header, tt.args.value)
232 - })
242 + api.Headers.Set(headerToTest, expectedHeaderValue)
243 + if _, err := api.Pin().Ls(context.Background()); err != nil {
244 + t.Fatal(err)
245 }
246 }
247 +
248 +/// testHTTPServer spins up a test go http server
249 +// used to check headers
250 +func testHTTPServer(url, headerToTest, expectedHeaderValue string) (*http.Server, error) {
251 + r := http.NewServeMux()
252 + r.HandleFunc("/api/v0/pin/ls", func(w http.ResponseWriter, r *http.Request) {
253 + val := r.Header.Get(headerToTest)
254 + if val == expectedHeaderValue {
255 + w.WriteHeader(400)
256 + return
257 + }
258 + w.WriteHeader(200)
259 + })
260 + return &http.Server{
261 + Handler: r,
262 + Addr: url,
263 + }, nil
264 +}