Debian Codegraph @

schema_validation/openapi_schemas/load_schema_test.go

raw ◀ ▶
1 Debian patch touch this file
1 // Copyright 2023-2024 Princess Beef Heavy Industries, LLC / Dave Shanley
2 // https://pb33f.io
3
4 package openapi_schemas
5
6 import (
7 "io"
8 "net/http"
9 "net/http/httptest"
10 "testing"
11
12 "github.com/stretchr/testify/assert"
13 "github.com/stretchr/testify/require"
14 )
15
16 // Mock server to simulate fetching remote files
17 func mockServer(response string, statusCode int) *httptest.Server {
18 handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
19 w.WriteHeader(statusCode)
20 _, _ = io.WriteString(w, response)
21 })
22 return httptest.NewServer(handler)
23 }
24
25 // Test LoadSchema3_0 when schema is already cached
26 func TestLoadSchema3_0_Cached(t *testing.T) {
27 // Set the cached schema
28 schema30 = "cached schema 3.0"
29 result := LoadSchema3_0("local schema 3.0")
30 require.Equal(t, "cached schema 3.0", result)
31 }
32
33 // Test LoadSchema3_1 when schema is already cached
34 func TestLoadSchema3_1_Cached(t *testing.T) {
35 // Set the cached schema
36 schema31 = "cached schema 3.1"
37 result := LoadSchema3_1("local schema 3.1")
38 require.Equal(t, "cached schema 3.1", result)
39 }
40
41 // Test LoadSchema3_0 when the remote schema is different from the local schema
42 func TestLoadSchema3_0_RemoteDifferent(t *testing.T) {
•43 t.Skip("Skipping network test in Debian build")
44 // Clear the cached schema
45 schema30 = ""
46
47 // Mock server with different remote schema
48 remoteSchema := `{"title": "OpenAPI 3.0"}`
49 server := mockServer(remoteSchema, http.StatusOK)
50 defer server.Close()
51
52 // Override the remote spec URL in extractSchema function
53 result := LoadSchema3_0(`{"title": "Local Schema 3.0"}`)
54 require.NotEqual(t, remoteSchema, result)
55 }
56
57 // Test LoadSchema3_0 when the remote schema is the same as the local schema
58 func TestLoadSchema3_0_RemoteSame(t *testing.T) {
•59 t.Skip("Skipping network test in Debian build")
60 // Clear the cached schema
61 schema30 = ""
62
63 // Same local and remote schema
64 localSchema := `{"title": "OpenAPI 3.0"}`
65 remoteSchema := `{"title": "OpenAPI 3.0"}`
66
67 server := mockServer(remoteSchema, http.StatusOK)
68 defer server.Close()
69
70 result := LoadSchema3_0(localSchema)
71 require.NotEqual(t, localSchema, result)
72 }
73
74 // Test LoadSchema3_1 when the remote schema is different from the local schema
75 func TestLoadSchema3_1_RemoteDifferent(t *testing.T) {
•76 t.Skip("Skipping network test in Debian build")
77 // Clear the cached schema
78 schema31 = ""
79
80 // Mock server with different remote schema
81 remoteSchema := `{"title": "OpenAPI 3.1"}`
82 server := mockServer(remoteSchema, http.StatusOK)
83 defer server.Close()
84
85 // The result should be the remote schema because it differs from the local schema
86 result := LoadSchema3_1(`{"title": "Local Schema 3.1"}`)
87 require.NotEqual(t, remoteSchema, result)
88 }
89
90 // Test LoadSchema3_1 when the remote schema is the same as the local schema
91 func TestLoadSchema3_1_RemoteSame(t *testing.T) {
•92 t.Skip("Skipping network test in Debian build")
93 // Clear the cached schema
94 schema31 = ""
95
96 // Same local and remote schema
97 localSchema := `{"title": "OpenAPI 3.1"}`
98 remoteSchema := `{"title": "OpenAPI 3.1"}`
99
100 server := mockServer(remoteSchema, http.StatusOK)
101 defer server.Close()
102
103 // The result should be the local schema since the MD5 hashes are the same
104 result := LoadSchema3_1(localSchema)
105 require.NotEqual(t, localSchema, result)
106 }
107
108 // Test extractSchema when the remote schema differs from the local schema
109 func TestExtractSchema_RemoteDifferent(t *testing.T) {
•110 t.Skip("Skipping network test in Debian build")
111 // Mock remote schema
112 remoteSchema := `{"title": "Remote Schema"}`
113 server := mockServer(remoteSchema, http.StatusOK)
114 defer server.Close()
115
116 // Local schema is different from the remote schema
117 localSchema := `{"title": "Local Schema"}`
118
119 result := extractSchema(server.URL, localSchema)
120 require.Equal(t, remoteSchema, result)
121 }
122
123 // Test extractSchema when the remote schema matches the local schema
124 func TestExtractSchema_RemoteSame(t *testing.T) {
•125 t.Skip("Skipping network test in Debian build")
126 // Mock remote schema
127 localSchema := `{"title": "Same Schema"}`
128 remoteSchema := `{"title": "Same Schema"}`
129
130 server := mockServer(remoteSchema, http.StatusOK)
131 defer server.Close()
132
133 // Since the schemas match, the result should be the local schema
134 result := extractSchema(server.URL, localSchema)
135 require.Equal(t, localSchema, result)
136 }
137
138 // Test extractSchema when there is an error fetching the remote schema
139 func TestExtractSchema_Error(t *testing.T) {
•140 t.Skip("Skipping network test in Debian build")
141 // Mock server that returns an error status code
142 server := mockServer("", http.StatusInternalServerError)
143 defer server.Close()
144
145 // Local schema should be returned in case of an error
146 localSchema := `{"title": "Local Schema"}`
147 result := extractSchema(server.URL, localSchema)
148 require.NotEqual(t, localSchema, result)
149 }
150
151 func TestGetFile_Error(t *testing.T) {
152 // Mock server to return an error
153 local, err := getFile("htttttp://981374918273")
154 assert.Error(t, err)
155 assert.Nil(t, local)
156 }
157
158 func TestGetSchema_Error(t *testing.T) {
159 // Mock server to return an error
160 local := extractSchema("htttttp://981374918273", "pingo")
161 assert.Equal(t, "pingo", local)
162 }