MCPcopy
hub / github.com/snyk/cli / Test_proxyWithErrorHandler

Function Test_proxyWithErrorHandler

cliv2/pkg/basic_workflows/legacycli_test.go:46–101  ·  view source on GitHub ↗

Test_proxyWithErrorHandler tests if we correctly either set or do not set the "snyk-terminate" header, which is a signal to the legacy TypeScript CLI that it should stop trying to send HTTP requests.

(t *testing.T)

Source from the content-addressed store, hash-verified

44// Test_proxyWithErrorHandler tests if we correctly either set or do not set the "snyk-terminate" header,
45// which is a signal to the legacy TypeScript CLI that it should stop trying to send HTTP requests.
46func Test_proxyWithErrorHandler(t *testing.T) {
47 ctrl := gomock.NewController(t)
48 logger := zerolog.Nop()
49 config := configuration.NewWithOpts()
50
51 handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
52 w.WriteHeader(http.StatusUnauthorized)
53 })
54 server := httptest.NewServer(handler)
55 defer server.Close()
56
57 networkAccess := networking.NewNetworkAccess(config)
58 networkAccess.AddErrorHandler(func(err error, _ context.Context) error { return err })
59
60 invocationCtxMock := mocks.NewMockInvocationContext(ctrl)
61 invocationCtxMock.EXPECT().GetNetworkAccess().Return(networkAccess).AnyTimes()
62 invocationCtxMock.EXPECT().GetEnhancedLogger().Return(&logger).AnyTimes()
63
64 testCases := []struct {
65 name string
66 configureApiUrl string
67 expectedIntercept bool
68 }{
69 {
70 name: "intercepts traffic based on configuration",
71 configureApiUrl: server.URL,
72 expectedIntercept: true,
73 },
74 {
75 name: "does not intercept external traffic",
76 configureApiUrl: "http://api.snyk.io",
77 expectedIntercept: false,
78 },
79 }
80
81 for _, tc := range testCases {
82 t.Run(tc.name, func(t *testing.T) {
83 config.Set(configuration.API_URL, tc.configureApiUrl)
84 wp, err := createInternalProxy(config, &logger, invocationCtxMock)
85 assert.Nil(t, err)
86
87 defer wp.Close()
88
89 client := http.Client{Transport: &http.Transport{Proxy: func(*http.Request) (*url.URL, error) {
90 return &url.URL{Scheme: "http", Host: fmt.Sprintf("127.0.0.1:%d", wp.ProxyInfo().Port)}, nil
91 }}}
92
93 res, err := client.Get(server.URL)
94 assert.NotNil(t, res)
95 defer func() { _ = res.Body.Close() }()
96
97 assert.Equal(t, tc.expectedIntercept, res.Header.Get(proxy.HeaderSnykTerminate) == "true")
98 assert.Nil(t, err)
99 })
100 }
101}
102
103func Test_ValidateGlibcVersion_doesNotApplyOnNonLinux(t *testing.T) {

Callers

nothing calls this directly

Calls 3

createInternalProxyFunction · 0.85
CloseMethod · 0.80
ProxyInfoMethod · 0.80

Tested by

no test coverage detected