Debian Codegraph @

schema_validation/openapi_schemas/load_schema_test.go

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
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 // Clear the cached schema
44 schema30 = ""
45
46 // Mock server with different remote schema
47 remoteSchema := `{"title": "OpenAPI 3.0"}`
48 server := mockServer(remoteSchema, http.StatusOK)
49 defer server.Close()
50
51 // Override the remote spec URL in extractSchema function
52 result := LoadSchema3_0(`{"title": "Local Schema 3.0"}`)
53 require.NotEqual(t, remoteSchema, result)
54 }
55
56 // Test LoadSchema3_0 when the remote schema is the same as the local schema
57 func TestLoadSchema3_0_RemoteSame(t *testing.T) {
58 // Clear the cached schema
59 schema30 = ""
60
61 // Same local and remote schema
62 localSchema := `{"title": "OpenAPI 3.0"}`
63 remoteSchema := `{"title": "OpenAPI 3.0"}`
64
65 server := mockServer(remoteSchema, http.StatusOK)
66 defer server.Close()
67
68 result := LoadSchema3_0(localSchema)
69 require.NotEqual(t, localSchema, result)
70 }
71
72 // Test LoadSchema3_1 when the remote schema is different from the local schema
73 func TestLoadSchema3_1_RemoteDifferent(t *testing.T) {
74 // Clear the cached schema
75 schema31 = ""
76
77 // Mock server with different remote schema
78 remoteSchema := `{"title": "OpenAPI 3.1"}`
79 server := mockServer(remoteSchema, http.StatusOK)
80 defer server.Close()
81
82 // The result should be the remote schema because it differs from the local schema
83 result := LoadSchema3_1(`{"title": "Local Schema 3.1"}`)
84 require.NotEqual(t, remoteSchema, result)
85 }
86
87 // Test LoadSchema3_1 when the remote schema is the same as the local schema
88 func TestLoadSchema3_1_RemoteSame(t *testing.T) {
89 // Clear the cached schema
90 schema31 = ""
91
92 // Same local and remote schema
93 localSchema := `{"title": "OpenAPI 3.1"}`
94 remoteSchema := `{"title": "OpenAPI 3.1"}`
95
96 server := mockServer(remoteSchema, http.StatusOK)
97 defer server.Close()
98
99 // The result should be the local schema since the MD5 hashes are the same
100 result := LoadSchema3_1(localSchema)
101 require.NotEqual(t, localSchema, result)
102 }
103
104 // Test extractSchema when the remote schema differs from the local schema
105 func TestExtractSchema_RemoteDifferent(t *testing.T) {
106 // Mock remote schema
107 remoteSchema := `{"title": "Remote Schema"}`
108 server := mockServer(remoteSchema, http.StatusOK)
109 defer server.Close()
110
111 // Local schema is different from the remote schema
112 localSchema := `{"title": "Local Schema"}`
113
114 result := extractSchema(server.URL, localSchema)
115 require.Equal(t, remoteSchema, result)
116 }
117
118 // Test extractSchema when the remote schema matches the local schema
119 func TestExtractSchema_RemoteSame(t *testing.T) {
120 // Same local and remote schema
121 localSchema := `{"title": "Same Schema"}`
122 remoteSchema := `{"title": "Same Schema"}`
123
124 server := mockServer(remoteSchema, http.StatusOK)
125 defer server.Close()
126
127 // Since the schemas match, the result should be the local schema
128 result := extractSchema(server.URL, localSchema)
129 require.Equal(t, localSchema, result)
130 }
131
132 // Test extractSchema when there is an error fetching the remote schema
133 func TestExtractSchema_Error(t *testing.T) {
134 // Mock server to return an error
135 server := mockServer("", http.StatusInternalServerError)
136 defer server.Close()
137
138 // Local schema should be returned in case of an error
139 localSchema := `{"title": "Local Schema"}`
140 result := extractSchema(server.URL, localSchema)
141 require.NotEqual(t, localSchema, result)
142 }
143
144 func TestGetFile_Error(t *testing.T) {
145 // Mock server to return an error
146 local, err := getFile("htttttp://981374918273")
147 assert.Error(t, err)
148 assert.Nil(t, local)
149 }
150
151 func TestGetSchema_Error(t *testing.T) {
152 // Mock server to return an error
153 local := extractSchema("htttttp://981374918273", "pingo")
154 assert.Equal(t, "pingo", local)
155 }