File: env_test.go

package info (click to toggle)
golang-github-jarcoal-httpmock 1.0.8-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid
  • size: 220 kB
  • sloc: makefile: 2
file content (63 lines) | stat: -rw-r--r-- 1,644 bytes parent folder | download | duplicates (2)
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
58
59
60
61
62
63
package httpmock

import (
	"net/http"
	"os"
	"testing"
)

func TestEnv(t *testing.T) {
	DeactivateAndReset()

	orig := os.Getenv(envVarName)

	// put it in an enabled state
	if err := os.Setenv(envVarName, ""); err != nil {
		t.Fatal(err)
	} else if Disabled() {
		t.Fatal("expected not to be disabled")
	}

	client1 := &http.Client{Transport: &http.Transport{}}
	client2 := &http.Client{Transport: &http.Transport{}}

	// make sure an activation works
	Activate()
	ActivateNonDefault(client1)
	ActivateNonDefault(client2)
	if http.DefaultTransport != DefaultTransport {
		t.Fatal("expected http.DefaultTransport to be our DefaultTransport")
	}
	if client1.Transport != DefaultTransport {
		t.Fatal("expected client1.Transport to be our DefaultTransport")
	}
	if client2.Transport != DefaultTransport {
		t.Fatal("expected client2.Transport to be our DefaultTransport")
	}
	Deactivate()

	if err := os.Setenv(envVarName, "1"); err != nil {
		t.Fatal(err)
	} else if !Disabled() {
		t.Fatal("expected to be disabled")
	}

	// make sure activation doesn't work
	Activate()
	ActivateNonDefault(client1)
	ActivateNonDefault(client2)
	if http.DefaultTransport == DefaultTransport {
		t.Fatal("expected http.DefaultTransport to not be our DefaultTransport")
	}
	if client1.Transport == DefaultTransport {
		t.Fatal("expected client1.Transport to not be our DefaultTransport")
	}
	if client2.Transport == DefaultTransport {
		t.Fatal("expected client2.Transport to not be our DefaultTransport")
	}
	Deactivate()

	if err := os.Setenv(envVarName, orig); err != nil {
		t.Fatalf("could not reset %s to it's original value '%s'", envVarName, orig)
	}
}