api: cleanup header and NewURLApiWithClient test

This commit was moved from ipfs/go-ipfs-http-client@b8b55cb89a
This commit is contained in:
Alex 2019-05-01 16:13:47 -07:00
parent efe3b6054e
commit fd5010bd2a

View File

@ -5,13 +5,14 @@ import (
"io/ioutil"
"net/http"
gohttp "net/http"
"net/http/httptest"
"os"
"strconv"
"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"
@ -212,23 +213,24 @@ func TestHttpApi(t *testing.T) {
tests.TestApi(newNodeProvider(ctx))(t)
}
func Test_NewURLApiWithClient(t *testing.T) {
t.Skip()
func Test_NewURLApiWithClient_With_Headers(t *testing.T) {
var (
url = "127.0.0.1:65501"
headerToTest = "Test-Header"
expectedHeaderValue = "thisisaheadertest"
)
server, err := testHTTPServer(url, headerToTest, expectedHeaderValue)
if err != nil {
t.Fatal(err)
}
defer server.Close()
go func() {
server.ListenAndServe()
}()
ts := httptest.NewServer(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
val := r.Header.Get(headerToTest)
if val == expectedHeaderValue {
w.WriteHeader(400)
return
}
w.WriteHeader(200)
}),
)
defer ts.Close()
time.Sleep(time.Second * 2)
api, err := NewURLApiWithClient(url, &http.Client{
api, err := NewURLApiWithClient(ts.URL, &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
DisableKeepAlives: true,
@ -242,21 +244,3 @@ func Test_NewURLApiWithClient(t *testing.T) {
t.Fatal(err)
}
}
/// testHTTPServer spins up a test go http server
// used to check headers
func testHTTPServer(url, headerToTest, expectedHeaderValue string) (*http.Server, error) {
r := http.NewServeMux()
r.HandleFunc("/api/v0/pin/ls", func(w http.ResponseWriter, r *http.Request) {
val := r.Header.Get(headerToTest)
if val == expectedHeaderValue {
w.WriteHeader(400)
return
}
w.WriteHeader(200)
})
return &http.Server{
Handler: r,
Addr: url,
}, nil
}