|
| 1 | +// Copyright 2023-2024 Princess Beef Heavy Industries, LLC / Dave Shanley |
| 2 | +// https://pb33f.io |
| 3 | + |
| 4 | +package helpers |
| 5 | + |
| 6 | +import ( |
| 7 | + "fmt" |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | + "net/http" |
| 10 | + "net/http/httptest" |
| 11 | + "testing" |
| 12 | + "time" |
| 13 | +) |
| 14 | + |
| 15 | +// Test the Load function for a successful case |
| 16 | +func TestHTTPURLLoader_Load_Success(t *testing.T) { |
| 17 | + // Create a mock HTTP server that returns a 200 response |
| 18 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 19 | + fmt.Fprintln(w, `{"success": true}`) |
| 20 | + })) |
| 21 | + defer server.Close() |
| 22 | + |
| 23 | + loader := NewHTTPURLLoader(false) |
| 24 | + |
| 25 | + // Test the Load function |
| 26 | + _, err := loader.Load(server.URL) |
| 27 | + require.NoError(t, err) |
| 28 | +} |
| 29 | + |
| 30 | +// Test the Load function when the server returns a non-200 response |
| 31 | +func TestHTTPURLLoader_Load_NonOKStatus(t *testing.T) { |
| 32 | + // Create a mock HTTP server that returns a 404 response |
| 33 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 34 | + http.Error(w, "not found", http.StatusNotFound) |
| 35 | + })) |
| 36 | + defer server.Close() |
| 37 | + |
| 38 | + loader := NewHTTPURLLoader(false) |
| 39 | + |
| 40 | + // Test the Load function |
| 41 | + _, err := loader.Load(server.URL) |
| 42 | + require.Error(t, err) |
| 43 | + require.Contains(t, err.Error(), "returned status code 404") |
| 44 | +} |
| 45 | + |
| 46 | +// Test the Load function when the server returns an error |
| 47 | +func TestHTTPURLLoader_Load_Error(t *testing.T) { |
| 48 | + loader := NewHTTPURLLoader(false) |
| 49 | + |
| 50 | + // Test the Load function with an invalid URL |
| 51 | + _, err := loader.Load("http://invalid-url") |
| 52 | + require.Error(t, err) |
| 53 | +} |
| 54 | + |
| 55 | +// Test the Load function with an insecure TLS config |
| 56 | +func TestHTTPURLLoader_Load_Insecure(t *testing.T) { |
| 57 | + // Create a mock HTTPS server |
| 58 | + server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 59 | + fmt.Fprintln(w, `{"secure": true}`) |
| 60 | + })) |
| 61 | + defer server.Close() |
| 62 | + |
| 63 | + loader := NewHTTPURLLoader(true) |
| 64 | + |
| 65 | + // Test the Load function |
| 66 | + _, err := loader.Load(server.URL) |
| 67 | + require.NoError(t, err) |
| 68 | +} |
| 69 | + |
| 70 | +// Test the NewHTTPURLLoader function with insecure set to false |
| 71 | +func TestNewHTTPURLLoader_Secure(t *testing.T) { |
| 72 | + loader := NewHTTPURLLoader(false) |
| 73 | + require.NotNil(t, loader) |
| 74 | + |
| 75 | + // Assert that the loader has the correct timeout and secure transport |
| 76 | + client := (*http.Client)(loader) |
| 77 | + require.Equal(t, 15*time.Second, client.Timeout) |
| 78 | + require.Nil(t, client.Transport) // Transport should be nil when secure |
| 79 | +} |
| 80 | + |
| 81 | +// Test the NewHTTPURLLoader function with insecure set to true |
| 82 | +func TestNewHTTPURLLoader_Insecure(t *testing.T) { |
| 83 | + loader := NewHTTPURLLoader(true) |
| 84 | + require.NotNil(t, loader) |
| 85 | + |
| 86 | + // Assert that the loader has an insecure transport configuration |
| 87 | + client := (*http.Client)(loader) |
| 88 | + transport, ok := client.Transport.(*http.Transport) |
| 89 | + require.True(t, ok) |
| 90 | + require.NotNil(t, transport.TLSClientConfig) |
| 91 | + require.True(t, transport.TLSClientConfig.InsecureSkipVerify) |
| 92 | +} |
| 93 | + |
| 94 | +// Test the NewCompilerLoader function |
| 95 | +func TestNewCompilerLoader(t *testing.T) { |
| 96 | + loader := NewCompilerLoader() |
| 97 | + require.NotNil(t, loader) |
| 98 | + |
| 99 | + // Assert that the loader contains the correct schemes |
| 100 | + require.NotNil(t, loader["http"]) |
| 101 | + require.NotNil(t, loader["https"]) |
| 102 | + require.NotNil(t, loader["file"]) |
| 103 | +} |
0 commit comments