(t *testing.T)
| 89 | } |
| 90 | |
| 91 | func TestNewHTTPRequest(t *testing.T) { |
| 92 | // Create a temporary file for bearer token test |
| 93 | tmpDir := t.TempDir() |
| 94 | bearerTokenFile := filepath.Join(tmpDir, "token") |
| 95 | err := os.WriteFile(bearerTokenFile, []byte("test-bearer-token"), 0644) |
| 96 | require.NoError(t, err) |
| 97 | |
| 98 | tests := map[string]struct { |
| 99 | req RequestConfig |
| 100 | validate func(t *testing.T, req *http.Request, cfg RequestConfig) |
| 101 | wantErr bool |
| 102 | errMsg string |
| 103 | }{ |
| 104 | "empty config": { |
| 105 | req: RequestConfig{}, |
| 106 | validate: func(t *testing.T, req *http.Request, cfg RequestConfig) { |
| 107 | assert.Equal(t, "GET", req.Method) |
| 108 | assert.Equal(t, "", req.URL.String()) |
| 109 | assert.NotEmpty(t, req.Header.Get("User-Agent")) |
| 110 | }, |
| 111 | }, |
| 112 | "full config": { |
| 113 | req: RequestConfig{ |
| 114 | URL: "http://127.0.0.1:19999/api/v1/info", |
| 115 | Method: "POST", |
| 116 | Body: "test body content", |
| 117 | Username: "user", |
| 118 | Password: "pass", |
| 119 | ProxyUsername: "proxy_user", |
| 120 | ProxyPassword: "proxy_pass", |
| 121 | Headers: map[string]string{ |
| 122 | "X-Custom-Header": "custom-value", |
| 123 | "Content-Type": "application/json", |
| 124 | }, |
| 125 | }, |
| 126 | validate: func(t *testing.T, req *http.Request, cfg RequestConfig) { |
| 127 | assert.Equal(t, cfg.URL, req.URL.String()) |
| 128 | assert.Equal(t, cfg.Method, req.Method) |
| 129 | |
| 130 | // Check body |
| 131 | body, err := io.ReadAll(req.Body) |
| 132 | require.NoError(t, err) |
| 133 | assert.Equal(t, cfg.Body, string(body)) |
| 134 | |
| 135 | // Check basic auth |
| 136 | user, pass, ok := req.BasicAuth() |
| 137 | assert.True(t, ok) |
| 138 | assert.Equal(t, cfg.Username, user) |
| 139 | assert.Equal(t, cfg.Password, pass) |
| 140 | |
| 141 | // Check proxy auth |
| 142 | proxyAuth := req.Header.Get("Proxy-Authorization") |
| 143 | assert.NotEmpty(t, proxyAuth) |
| 144 | proxyUser, proxyPass, ok := parseBasicAuth(proxyAuth) |
| 145 | assert.True(t, ok) |
| 146 | assert.Equal(t, cfg.ProxyUsername, proxyUser) |
| 147 | assert.Equal(t, cfg.ProxyPassword, proxyPass) |
| 148 |
nothing calls this directly
no test coverage detected
searching dependent graphs…