github.com/webx-top/com@v1.2.12/http_test.go (about) 1 package com 2 3 import ( 4 "context" 5 "errors" 6 "fmt" 7 "net" 8 "net/url" 9 "testing" 10 "time" 11 12 "github.com/stretchr/testify/assert" 13 ) 14 15 func TestParseRetryAfter(t *testing.T) { 16 now := time.Now().UTC() 17 tim := now.Format(`15:04:05`) 18 year := now.AddDate(1, 0, 0).Year() 19 retryAfter := fmt.Sprintf(`Wed, 21 Oct %d %s GMT`, year, tim) 20 d := ParseRetryAfter(retryAfter) 21 assert.Equal(t, fmt.Sprintf(`%d-10-21T%sZ`, year, now.Add(-time.Second).Format(`15:04:05`)), now.Add(d).UTC().Format(time.RFC3339)) 22 retryAfter = `300` 23 d = ParseRetryAfter(retryAfter) 24 assert.Equal(t, float64(5), d.Minutes()) 25 } 26 27 func TestIsNetworkOrHostDown(t *testing.T) { 28 r := IsNetworkOrHostDown(context.Canceled, false) 29 assert.False(t, r) 30 r = IsNetworkOrHostDown(context.DeadlineExceeded, false) 31 assert.True(t, r) 32 r = IsNetworkOrHostDown(context.DeadlineExceeded, true) 33 assert.False(t, r) 34 35 r = IsNetworkOrHostDown(&url.Error{Err: &net.DNSError{}}, true) 36 assert.True(t, r) 37 r = IsNetworkOrHostDown(&url.Error{Err: &net.OpError{}}, true) 38 assert.True(t, r) 39 r = IsNetworkOrHostDown(&url.Error{Err: net.UnknownNetworkError(`err`)}, true) 40 assert.True(t, r) 41 42 var netErr net.Error = &net.DNSError{IsTimeout: true, IsTemporary: true} 43 r = IsNetworkOrHostDown(netErr, true) 44 assert.True(t, r) 45 46 r = IsNetworkOrHostDown(errors.New(`connection refused`), true) 47 assert.True(t, r) 48 }