http_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package util
  2. import (
  3. "net/http"
  4. "testing"
  5. )
  6. // TestHttpWithTLS_NilTransport tests the scenario where DefaultHTTPClient.Transport is nil
  7. func TestHttpWithTLS_NilTransport(t *testing.T) {
  8. // Save original transport
  9. originalTransport := DefaultHTTPClient.Transport
  10. defer func() {
  11. DefaultHTTPClient.Transport = originalTransport
  12. }()
  13. // Set Transport to nil to simulate the bug scenario
  14. DefaultHTTPClient.Transport = nil
  15. // This should not panic after the fix
  16. // Note: This will fail due to invalid cert path, but shouldn't panic on type assertion
  17. _, err := httpWithTLS("./testdata/invalid_cert.p12", "password")
  18. // We expect an error (cert file not found), but NOT a panic
  19. if err == nil {
  20. t.Error("Expected error due to invalid cert path, but got nil")
  21. }
  22. }
  23. // TestHttpWithTLS_CustomTransport tests the scenario where DefaultHTTPClient has a custom Transport
  24. func TestHttpWithTLS_CustomTransport(t *testing.T) {
  25. // Save original transport
  26. originalTransport := DefaultHTTPClient.Transport
  27. defer func() {
  28. DefaultHTTPClient.Transport = originalTransport
  29. }()
  30. // Set a custom http.Transport
  31. customTransport := &http.Transport{
  32. MaxIdleConns: 100,
  33. }
  34. DefaultHTTPClient.Transport = customTransport
  35. // This should not panic
  36. _, err := httpWithTLS("./testdata/invalid_cert.p12", "password")
  37. // We expect an error (cert file not found), but NOT a panic
  38. if err == nil {
  39. t.Error("Expected error due to invalid cert path, but got nil")
  40. }
  41. }
  42. // CustomRoundTripper is a custom implementation of http.RoundTripper
  43. type CustomRoundTripper struct{}
  44. func (c *CustomRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
  45. return http.DefaultTransport.RoundTrip(req)
  46. }
  47. // TestHttpWithTLS_CustomRoundTripper tests the edge case where DefaultHTTPClient has a custom RoundTripper
  48. // that is NOT *http.Transport
  49. func TestHttpWithTLS_CustomRoundTripper(t *testing.T) {
  50. // Save original transport
  51. originalTransport := DefaultHTTPClient.Transport
  52. defer func() {
  53. DefaultHTTPClient.Transport = originalTransport
  54. }()
  55. // Set a custom RoundTripper that is NOT *http.Transport
  56. customRoundTripper := &CustomRoundTripper{}
  57. DefaultHTTPClient.Transport = customRoundTripper
  58. // Create a recovery handler to catch potential panic
  59. defer func() {
  60. if r := recover(); r != nil {
  61. t.Errorf("httpWithTLS panicked with custom RoundTripper: %v", r)
  62. }
  63. }()
  64. // This might panic if the code doesn't handle non-*http.Transport RoundTripper properly
  65. _, _ = httpWithTLS("./testdata/invalid_cert.p12", "password")
  66. }