-
Notifications
You must be signed in to change notification settings - Fork 103
/
env_test.go
57 lines (45 loc) · 1.74 KB
/
env_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package httpmock_test
import (
"net/http"
"os"
"testing"
"github.com/maxatome/go-testdeep/td"
"github.com/jarcoal/httpmock"
)
const envVarName = "GONOMOCKS"
func TestEnv(t *testing.T) {
require := td.Require(t)
httpmock.DeactivateAndReset()
defer func(orig string) {
require.CmpNoError(os.Setenv(envVarName, orig))
}(os.Getenv(envVarName))
// put it in an enabled state
require.CmpNoError(os.Setenv(envVarName, ""))
require.False(httpmock.Disabled(), "expected not to be disabled")
client1 := &http.Client{Transport: &http.Transport{}}
client2 := &http.Client{Transport: &http.Transport{}}
// make sure an activation works
httpmock.Activate()
httpmock.ActivateNonDefault(client1)
httpmock.ActivateNonDefault(client2)
require.Cmp(http.DefaultTransport, httpmock.DefaultTransport,
"expected http.DefaultTransport to be our DefaultTransport")
require.Cmp(client1.Transport, httpmock.DefaultTransport,
"expected client1.Transport to be our DefaultTransport")
require.Cmp(client2.Transport, httpmock.DefaultTransport,
"expected client2.Transport to be our DefaultTransport")
httpmock.Deactivate()
require.CmpNoError(os.Setenv(envVarName, "1"))
require.True(httpmock.Disabled(), "expected to be disabled")
// make sure activation doesn't work
httpmock.Activate()
httpmock.ActivateNonDefault(client1)
httpmock.ActivateNonDefault(client2)
require.Not(http.DefaultTransport, httpmock.DefaultTransport,
"expected http.DefaultTransport to not be our DefaultTransport")
require.Not(client1.Transport, httpmock.DefaultTransport,
"expected client1.Transport to not be our DefaultTransport")
require.Not(client2.Transport, httpmock.DefaultTransport,
"expected client2.Transport to not be our DefaultTransport")
httpmock.Deactivate()
}