Skip to content

Commit cec3ad7

Browse files
fix: server overriding (#24)
* fix: server overriding * fix: fixed wrong condition in handling servers * test: add server override functionality test --------- Co-authored-by: Leonardo Vicentini <[email protected]>
1 parent bd51161 commit cec3ad7

File tree

3 files changed

+70
-1
lines changed

3 files changed

+70
-1
lines changed

internal/client/restclient.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,18 @@ func (u *UnstructuredClient) Call(ctx context.Context, cli *http.Client, path st
2121
return nil, fmt.Errorf("path not found: %s", path)
2222
}
2323
httpMethod := string(opts.Method)
24+
ops := pathItem.GetOperations()
25+
if ops != nil {
26+
op, ok := ops.Get(strings.ToLower(httpMethod))
27+
if !ok {
28+
return nil, fmt.Errorf("operation not found for method %s at path %s", httpMethod, path)
29+
}
30+
31+
if len(op.Servers) > 0 {
32+
server := op.Servers[0]
33+
uri = buildPath(server.URL, path, opts.Parameters, opts.Query)
34+
}
35+
}
2436

2537
err := u.ValidateRequest(httpMethod, path, opts.Parameters, opts.Query)
2638
if err != nil {

internal/client/restclient_test.go

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ func TestCallWithRecorder(t *testing.T) {
4242
clientSetup func(*UnstructuredClient)
4343
expected interface{}
4444
expectedError string
45+
expectedURL string
4546
}{
4647
{
4748
name: "path with slash in path parameter",
@@ -179,6 +180,26 @@ func TestCallWithRecorder(t *testing.T) {
179180
},
180181
expectedError: "unexpected status: 401: invalid status code: 401",
181182
},
183+
{
184+
name: "server override in operation",
185+
handler: func(w http.ResponseWriter, r *http.Request) {
186+
// Verify that the request is using the override server URL
187+
// The mock transport doesn't actually change the host, but we can verify
188+
// the path and that the request was made
189+
assert.Equal(t, "GET", r.Method)
190+
assert.Equal(t, "/api/override", r.URL.Path)
191+
192+
w.Header().Set("Content-Type", "application/json")
193+
w.WriteHeader(http.StatusOK)
194+
json.NewEncoder(w).Encode(map[string]interface{}{"message": "override success"})
195+
},
196+
path: "/api/override",
197+
opts: &RequestConfiguration{
198+
Method: "GET",
199+
},
200+
expected: map[string]interface{}{"message": "override success"},
201+
expectedURL: "http://override.example.com/api/override",
202+
},
182203
}
183204

184205
for _, tt := range tests {
@@ -188,6 +209,11 @@ func TestCallWithRecorder(t *testing.T) {
188209
handler: tt.handler,
189210
}
190211

212+
// If we need to verify the URL (we set the field in the test case), capture it
213+
if tt.expectedURL != "" {
214+
mockTransport.capturedURL = ""
215+
}
216+
191217
// Create test client
192218
client := createTestClient(t)
193219
if tt.clientSetup != nil {
@@ -200,6 +226,11 @@ func TestCallWithRecorder(t *testing.T) {
200226
// Call the method under test
201227
result, err := client.Call(context.Background(), testClient, tt.path, tt.opts)
202228

229+
// Verify the URL if expected, if we set the field in the test case
230+
if tt.expectedURL != "" {
231+
assert.Equal(t, tt.expectedURL, mockTransport.capturedURL)
232+
}
233+
203234
if tt.expectedError != "" {
204235
require.Error(t, err)
205236
assert.Contains(t, err.Error(), tt.expectedError)
@@ -227,10 +258,16 @@ func TestCallWithRecorder(t *testing.T) {
227258

228259
// mockTransport implements http.RoundTripper using a ResponseRecorder
229260
type mockTransport struct {
230-
handler http.HandlerFunc
261+
handler http.HandlerFunc
262+
capturedURL string
231263
}
232264

233265
func (m *mockTransport) RoundTrip(req *http.Request) (*http.Response, error) {
266+
// Capture the full URL for verification
267+
if m.capturedURL == "" {
268+
m.capturedURL = req.URL.String()
269+
}
270+
234271
// Create a ResponseRecorder
235272
rr := httptest.NewRecorder()
236273

internal/client/testdata/openapi.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,26 @@ paths:
141141
items:
142142
$ref: '#/components/schemas/Item'
143143

144+
/api/override:
145+
get:
146+
summary: Test endpoint with server override
147+
operationId: getOverride
148+
servers:
149+
- url: http://override.example.com
150+
description: Override server
151+
responses:
152+
'200':
153+
description: Successful operation
154+
content:
155+
application/json:
156+
schema:
157+
type: object
158+
properties:
159+
message:
160+
type: string
161+
'404':
162+
description: Not found
163+
144164
components:
145165
schemas:
146166
User:

0 commit comments

Comments
 (0)