Debian Codegraph @

helpers/url_loader_test.go

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