@cryptotaxi247 / kubo / commits / fd5010bd2

api: cleanup header and NewURLApiWithClient test

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

Alex committed May 1, 2019 at 16:13 UTC fd5010bd2a852ea8fa3a2b08e199312fb3b71097
1 file changed +15 -31
client/httpapi/api_test.go
+15 -31
@@ -5,13 +5,14 @@ import (
5 "io/ioutil"
6 "net/http"
7 gohttp "net/http"
8 + "net/http/httptest"
9 "os"
10 "strconv"
11 "sync"
12 "testing"
13 "time"
14
14 - "github.com/ipfs/interface-go-ipfs-core"
15 + iface "github.com/ipfs/interface-go-ipfs-core"
16 "github.com/ipfs/interface-go-ipfs-core/path"
17
18 "github.com/ipfs/interface-go-ipfs-core/tests"
@@ -212,23 +213,24 @@ func TestHttpApi(t *testing.T) {
213 tests.TestApi(newNodeProvider(ctx))(t)
214 }
215
215 -func Test_NewURLApiWithClient(t *testing.T) {
216 - t.Skip()
216 +func Test_NewURLApiWithClient_With_Headers(t *testing.T) {
217 var (
218 - url = "127.0.0.1:65501"
218 headerToTest = "Test-Header"
219 expectedHeaderValue = "thisisaheadertest"
220 )
222 - server, err := testHTTPServer(url, headerToTest, expectedHeaderValue)
223 - if err != nil {
224 - t.Fatal(err)
225 - }
226 - defer server.Close()
227 - go func() {
228 - server.ListenAndServe()
229 - }()
221 + ts := httptest.NewServer(
222 + http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
223 + val := r.Header.Get(headerToTest)
224 + if val == expectedHeaderValue {
225 + w.WriteHeader(400)
226 + return
227 + }
228 + w.WriteHeader(200)
229 + }),
230 + )
231 + defer ts.Close()
232 time.Sleep(time.Second * 2)
231 - api, err := NewURLApiWithClient(url, &http.Client{
233 + api, err := NewURLApiWithClient(ts.URL, &http.Client{
234 Transport: &http.Transport{
235 Proxy: http.ProxyFromEnvironment,
236 DisableKeepAlives: true,
@@ -242,21 +244,3 @@ func Test_NewURLApiWithClient(t *testing.T) {
244 t.Fatal(err)
245 }
246 }
245 -
246 -/// testHTTPServer spins up a test go http server
247 -// used to check headers
248 -func testHTTPServer(url, headerToTest, expectedHeaderValue string) (*http.Server, error) {
249 - r := http.NewServeMux()
250 - r.HandleFunc("/api/v0/pin/ls", func(w http.ResponseWriter, r *http.Request) {
251 - val := r.Header.Get(headerToTest)
252 - if val == expectedHeaderValue {
253 - w.WriteHeader(400)
254 - return
255 - }
256 - w.WriteHeader(200)
257 - })
258 - return &http.Server{
259 - Handler: r,
260 - Addr: url,
261 - }, nil
262 -}