1package tests 2 3import ( 4 "context" 5 "encoding/json" 6 "io/ioutil" 7 "net/http" 8 "testing" 9 "time" 10 11 "github.com/cretz/bine/tor" 12 "golang.org/x/net/context/ctxhttp" 13) 14 15func TestDialerSimpleHTTP(t *testing.T) { 16 ctx := GlobalEnabledNetworkContext(t) 17 httpClient := httpClient(ctx, nil) 18 // IsTor check 19 byts := httpGet(ctx, httpClient, "https://check.torproject.org/api/ip") 20 jsn := map[string]interface{}{} 21 ctx.Require.NoError(json.Unmarshal(byts, &jsn)) 22 ctx.Require.True(jsn["IsTor"].(bool)) 23} 24 25func httpClient(ctx *TestContext, conf *tor.DialConf) *http.Client { 26 // 15 seconds max to dial 27 dialCtx, dialCancel := context.WithTimeout(ctx, 15*time.Second) 28 defer dialCancel() 29 // Make connection 30 dialer, err := ctx.Dialer(dialCtx, conf) 31 ctx.Require.NoError(err) 32 return &http.Client{Transport: &http.Transport{DialContext: dialer.DialContext}} 33} 34 35func httpGet(ctx *TestContext, client *http.Client, url string) []byte { 36 // We'll give it 30 seconds to respond 37 callCtx, callCancel := context.WithTimeout(ctx, 30*time.Second) 38 defer callCancel() 39 resp, err := ctxhttp.Get(callCtx, client, url) 40 ctx.Require.NoError(err) 41 defer resp.Body.Close() 42 respBytes, err := ioutil.ReadAll(resp.Body) 43 ctx.Require.NoError(err) 44 return respBytes 45} 46