shift-proxy 0.9.7

Native Rust HTTP proxy for SHIFT — intercepts AI API requests, optimizes image payloads, and forwards to upstream providers
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
//! Integration tests for the SHIFT proxy routes.
//!
//! All tests use a local mock upstream server — no real API calls.
//! The mock echoes back request metadata (path, method, headers, body)
//! so we can verify the proxy routes, forwards, and transforms correctly.

use axum::body::Body;
use axum::extract::State as AxumState;
use axum::http::{HeaderMap, Request, StatusCode};
use axum::response::{IntoResponse, Json};
use axum::routing::{any, get, post};
use axum::Router;
use flate2::write::GzEncoder;
use flate2::Compression;
use http_body_util::BodyExt;
use shift_proxy::state::ProviderUrls;
use shift_proxy::{create_app, ProxyConfig};
use std::io::Write;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;
use tokio::net::TcpListener;
use tower::ServiceExt; // for `oneshot`

// ── Mock upstream server ─────────────────────────────────────────────

/// Shared state for the mock upstream — counts requests.
#[derive(Clone, Default)]
struct MockState {
    request_count: Arc<AtomicU64>,
}

/// Start a mock upstream HTTP server on a random port.
/// Returns the base URL (e.g., "http://127.0.0.1:12345").
async fn start_mock_upstream() -> (String, MockState) {
    let state = MockState::default();

    let app = Router::new()
        .route("/health", get(mock_health))
        .fallback(any(mock_echo))
        .with_state(state.clone());

    let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
    let addr = listener.local_addr().unwrap();
    let base_url = format!("http://{}", addr);

    tokio::spawn(async move {
        axum::serve(listener, app).await.unwrap();
    });

    (base_url, state)
}

/// Mock health endpoint.
async fn mock_health() -> Json<serde_json::Value> {
    Json(serde_json::json!({"status": "ok", "service": "mock-upstream"}))
}

/// Mock catch-all: echoes back request metadata as JSON.
/// This lets tests verify that the proxy forwarded correctly.
async fn mock_echo(
    AxumState(state): AxumState<MockState>,
    method: axum::http::Method,
    uri: axum::http::Uri,
    headers: HeaderMap,
    body: String,
) -> Json<serde_json::Value> {
    state.request_count.fetch_add(1, Ordering::Relaxed);

    // Collect headers into a map (skip pseudo-headers)
    let header_map: serde_json::Map<String, serde_json::Value> = headers
        .iter()
        .map(|(k, v)| {
            (
                k.as_str().to_string(),
                serde_json::Value::String(v.to_str().unwrap_or("").to_string()),
            )
        })
        .collect();

    Json(serde_json::json!({
        "method": method.as_str(),
        "path": uri.path(),
        "query": uri.query().unwrap_or(""),
        "headers": header_map,
        "body": body,
    }))
}

/// Create a ProxyConfig that points all providers at the mock upstream.
fn test_config_with_mock(mock_url: &str) -> ProxyConfig {
    ProxyConfig {
        port: 0,
        verbose: false,
        providers: ProviderUrls {
            anthropic: mock_url.to_string(),
            openai: mock_url.to_string(),
            google: mock_url.to_string(),
        },
        ..ProxyConfig::default()
    }
}

/// Helper: extract JSON body from an axum response.
async fn json_body(response: axum::response::Response) -> serde_json::Value {
    let body = response.into_body().collect().await.unwrap().to_bytes();
    serde_json::from_slice(&body).unwrap()
}

// ── Health endpoint ──────────────────────────────────────────────────

#[tokio::test]
async fn health_returns_ok_with_service_identity() {
    let app = create_app(ProxyConfig::default());

    let response = app
        .oneshot(
            Request::builder()
                .uri("/health")
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();

    assert_eq!(response.status(), StatusCode::OK);
    let json = json_body(response).await;

    assert_eq!(json["status"], "ok");
    assert_eq!(json["service"], "@shift-preflight/runtime proxy");
    assert!(json["version"].is_string());
    assert!(!json["version"].as_str().unwrap().is_empty());
}

// ── Stats endpoint ───────────────────────────────────────────────────

#[tokio::test]
async fn stats_returns_session_stats() {
    let app = create_app(ProxyConfig::default());

    let response = app
        .oneshot(
            Request::builder()
                .uri("/stats")
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();

    assert_eq!(response.status(), StatusCode::OK);
    let json = json_body(response).await;

    assert!(json["totalRequests"].is_number());
    assert!(json["totalImages"].is_number());
    assert!(json["totalImagesModified"].is_number());
    assert!(json["totalBytesSaved"].is_number());
    assert!(json["tokenSavings"].is_object());
}

// ── 404 for unknown routes ───────────────────────────────────────────

#[tokio::test]
async fn unknown_route_returns_not_found() {
    let app = create_app(ProxyConfig::default());

    let response = app
        .oneshot(
            Request::builder()
                .uri("/unknown/endpoint")
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();

    assert_eq!(response.status(), StatusCode::NOT_FOUND);
}

// ── Anthropic route — forwards to mock upstream ──────────────────────

#[tokio::test]
async fn anthropic_route_forwards_to_upstream() {
    let (mock_url, mock_state) = start_mock_upstream().await;
    let app = create_app(test_config_with_mock(&mock_url));

    let payload = serde_json::json!({
        "model": "claude-sonnet-4-20250514",
        "max_tokens": 1,
        "messages": [{"role": "user", "content": "test"}]
    });

    let response = app
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/v1/messages")
                .header("content-type", "application/json")
                .header("x-api-key", "sk-ant-test123")
                .header("anthropic-version", "2023-06-01")
                .body(Body::from(serde_json::to_string(&payload).unwrap()))
                .unwrap(),
        )
        .await
        .unwrap();

    assert_eq!(response.status(), StatusCode::OK);
    let json = json_body(response).await;

    // Verify the mock received the request at the correct path
    assert_eq!(json["path"], "/v1/messages");
    assert_eq!(json["method"], "POST");

    // Verify auth headers were forwarded
    assert_eq!(json["headers"]["x-api-key"], "sk-ant-test123");
    assert_eq!(json["headers"]["anthropic-version"], "2023-06-01");

    // Verify the body was forwarded (text-only payload — no optimization needed)
    let forwarded_body: serde_json::Value =
        serde_json::from_str(json["body"].as_str().unwrap()).unwrap();
    assert_eq!(forwarded_body["model"], "claude-sonnet-4-20250514");
    assert_eq!(forwarded_body["messages"][0]["content"], "test");

    // Verify mock received exactly 1 request
    assert_eq!(mock_state.request_count.load(Ordering::Relaxed), 1);
}

// ── OpenAI route — forwards to mock upstream ─────────────────────────

#[tokio::test]
async fn openai_route_forwards_to_upstream() {
    let (mock_url, mock_state) = start_mock_upstream().await;
    let app = create_app(test_config_with_mock(&mock_url));

    let payload = serde_json::json!({
        "model": "gpt-4o",
        "messages": [{"role": "user", "content": "test"}]
    });

    let response = app
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/v1/chat/completions")
                .header("content-type", "application/json")
                .header("authorization", "Bearer sk-test456")
                .body(Body::from(serde_json::to_string(&payload).unwrap()))
                .unwrap(),
        )
        .await
        .unwrap();

    assert_eq!(response.status(), StatusCode::OK);
    let json = json_body(response).await;

    assert_eq!(json["path"], "/v1/chat/completions");
    assert_eq!(json["method"], "POST");
    assert_eq!(json["headers"]["authorization"], "Bearer sk-test456");

    let forwarded_body: serde_json::Value =
        serde_json::from_str(json["body"].as_str().unwrap()).unwrap();
    assert_eq!(forwarded_body["model"], "gpt-4o");

    assert_eq!(mock_state.request_count.load(Ordering::Relaxed), 1);
}

// ── Google route — forwards with query params preserved ──────────────

#[tokio::test]
async fn google_route_forwards_with_query_params() {
    let (mock_url, _) = start_mock_upstream().await;
    let app = create_app(test_config_with_mock(&mock_url));

    let response = app
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/v1beta/models/gemini-2.5-pro:generateContent?key=test-key-789")
                .header("content-type", "application/json")
                .body(Body::from(r#"{"contents": [{"parts": [{"text": "hi"}]}]}"#))
                .unwrap(),
        )
        .await
        .unwrap();

    assert_eq!(response.status(), StatusCode::OK);
    let json = json_body(response).await;

    // Verify path and query params are forwarded correctly
    assert_eq!(
        json["path"],
        "/v1beta/models/gemini-2.5-pro:generateContent"
    );
    assert_eq!(json["query"], "key=test-key-789");
}

// ── Passthrough — forwards to correct provider ───────────────────────

#[tokio::test]
async fn passthrough_forwards_anthropic_subpaths() {
    let (mock_url, mock_state) = start_mock_upstream().await;
    let app = create_app(test_config_with_mock(&mock_url));

    let response = app
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/v1/messages/batches")
                .header("content-type", "application/json")
                .body(Body::from(r#"{"test": true}"#))
                .unwrap(),
        )
        .await
        .unwrap();

    assert_eq!(response.status(), StatusCode::OK);
    let json = json_body(response).await;
    assert_eq!(json["path"], "/v1/messages/batches");
    assert_eq!(mock_state.request_count.load(Ordering::Relaxed), 1);
}

#[tokio::test]
async fn passthrough_returns_404_for_unknown_provider() {
    let (mock_url, _) = start_mock_upstream().await;
    let app = create_app(test_config_with_mock(&mock_url));

    let response = app
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/unknown/path")
                .header("content-type", "application/json")
                .body(Body::from(r#"{"test": true}"#))
                .unwrap(),
        )
        .await
        .unwrap();

    assert_eq!(response.status(), StatusCode::NOT_FOUND);
}

// ── GET method passthrough ───────────────────────────────────────────

#[tokio::test]
async fn get_request_forwarded_through_passthrough() {
    let (mock_url, _) = start_mock_upstream().await;
    let app = create_app(test_config_with_mock(&mock_url));

    // GET /v1/models should be forwarded to OpenAI (via passthrough)
    let response = app
        .oneshot(
            Request::builder()
                .method("GET")
                .uri("/v1/models")
                .header("authorization", "Bearer sk-test")
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();

    assert_eq!(response.status(), StatusCode::OK);
    let json = json_body(response).await;
    assert_eq!(json["path"], "/v1/models");
    assert_eq!(json["method"], "GET");
    assert_eq!(json["headers"]["authorization"], "Bearer sk-test");
}

// ── Auth headers NOT stripped ────────────────────────────────────────

#[tokio::test]
async fn auth_headers_forwarded_to_upstream() {
    let (mock_url, _) = start_mock_upstream().await;
    let app = create_app(test_config_with_mock(&mock_url));

    let response = app
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/v1/messages")
                .header("content-type", "application/json")
                .header("x-api-key", "sk-ant-secret")
                .header("anthropic-version", "2023-06-01")
                .header("authorization", "Bearer also-present")
                .body(Body::from(r#"{"model":"claude-sonnet-4-20250514","max_tokens":1,"messages":[{"role":"user","content":"hi"}]}"#))
                .unwrap(),
        )
        .await
        .unwrap();

    assert_eq!(response.status(), StatusCode::OK);
    let json = json_body(response).await;

    assert_eq!(json["headers"]["x-api-key"], "sk-ant-secret");
    assert_eq!(json["headers"]["anthropic-version"], "2023-06-01");
    assert_eq!(json["headers"]["authorization"], "Bearer also-present");
}

// ── Host header stripped ─────────────────────────────────────────────

#[tokio::test]
async fn host_header_not_forwarded() {
    let (mock_url, _) = start_mock_upstream().await;
    let app = create_app(test_config_with_mock(&mock_url));

    let response = app
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/v1/messages")
                .header("content-type", "application/json")
                .header("host", "evil.example.com")
                .body(Body::from(r#"{"model":"claude-sonnet-4-20250514","max_tokens":1,"messages":[{"role":"user","content":"hi"}]}"#))
                .unwrap(),
        )
        .await
        .unwrap();

    assert_eq!(response.status(), StatusCode::OK);
    let json = json_body(response).await;

    // The original "host: evil.example.com" should have been stripped.
    // reqwest sets its own Host header from the target URL.
    let host = json["headers"]["host"].as_str().unwrap_or("");
    assert!(
        !host.contains("evil"),
        "original host header should be stripped, got: {}",
        host
    );
}

// ── Health endpoint backward compatibility ────────────────────────────

#[tokio::test]
async fn health_backward_compatible_with_opencode_plugin() {
    let app = create_app(ProxyConfig::default());

    let response = app
        .oneshot(
            Request::builder()
                .uri("/health")
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();

    let json = json_body(response).await;

    // OpenCode plugin checks: body.service === "@shift-preflight/runtime proxy"
    assert_eq!(
        json["service"].as_str().unwrap(),
        "@shift-preflight/runtime proxy"
    );

    // OpenCode plugin checks: body.version exists
    assert!(json.get("version").is_some());
    assert!(!json["version"].as_str().unwrap().is_empty());
}

// ── Gzip SSE streaming ──────────────────────────────────────────────

/// Mock handler that serves a gzip-compressed SSE response with Content-Encoding: gzip.
/// This simulates what Anthropic returns when the client sends Accept-Encoding: gzip.
async fn mock_gzip_sse() -> impl IntoResponse {
    let sse_body = "event: message_start\ndata: {\"type\":\"message_start\"}\n\nevent: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"delta\":{\"text\":\"Hello\"}}\n\nevent: message_stop\ndata: {\"type\":\"message_stop\"}\n\n";

    let mut encoder = GzEncoder::new(Vec::new(), Compression::default());
    encoder.write_all(sse_body.as_bytes()).unwrap();
    let compressed = encoder.finish().unwrap();

    (
        StatusCode::OK,
        [
            ("content-type", "text/event-stream"),
            ("content-encoding", "gzip"),
        ],
        compressed,
    )
}

/// Start a mock upstream that serves gzip-compressed SSE on /v1/messages.
async fn start_gzip_mock_upstream() -> String {
    let app = Router::new()
        .route("/v1/messages", post(mock_gzip_sse))
        .fallback(any(mock_gzip_sse));

    let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
    let addr = listener.local_addr().unwrap();
    let base_url = format!("http://{}", addr);

    tokio::spawn(async move {
        axum::serve(listener, app).await.unwrap();
    });

    base_url
}

#[tokio::test]
async fn anthropic_gzip_response_is_decompressed() {
    // Start a mock that serves gzip-compressed SSE (simulating Anthropic's response
    // when the client sends Accept-Encoding: gzip).
    let mock_url = start_gzip_mock_upstream().await;
    let app = create_app(test_config_with_mock(&mock_url));

    let response = app
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/v1/messages")
                .header("content-type", "application/json")
                .header("accept-encoding", "gzip")
                .header("x-api-key", "sk-ant-test")
                .header("anthropic-version", "2023-06-01")
                .body(Body::from(
                    r#"{"model":"claude-sonnet-4-20250514","max_tokens":1,"messages":[{"role":"user","content":"hi"}]}"#,
                ))
                .unwrap(),
        )
        .await
        .unwrap();

    assert_eq!(response.status(), StatusCode::OK);

    // Content-Encoding must NOT be present — reqwest decompressed the body.
    assert!(
        response.headers().get("content-encoding").is_none(),
        "content-encoding should be stripped after decompression"
    );

    // The body must be readable SSE text, not binary gzip bytes.
    let body_bytes = response.into_body().collect().await.unwrap().to_bytes();
    let body_str = String::from_utf8(body_bytes.to_vec())
        .expect("response body should be valid UTF-8 (decompressed SSE)");

    assert!(
        body_str.contains("event: message_start"),
        "body should contain SSE events, got: {:?}",
        &body_str[..body_str.len().min(200)]
    );
    assert!(
        body_str.contains("Hello"),
        "body should contain the streamed text"
    );
}

#[tokio::test]
async fn go_client_accept_encoding_gzip_gets_readable_response() {
    // Go's net/http sets Accept-Encoding: gzip by default. This test verifies
    // that a request with that header gets back readable text, not binary gzip.
    let mock_url = start_gzip_mock_upstream().await;
    let app = create_app(test_config_with_mock(&mock_url));

    let response = app
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/v1/messages")
                .header("content-type", "application/json")
                .header("accept-encoding", "gzip")
                .body(Body::from(
                    r#"{"model":"claude-sonnet-4-20250514","max_tokens":1,"messages":[{"role":"user","content":"test"}]}"#,
                ))
                .unwrap(),
        )
        .await
        .unwrap();

    assert_eq!(response.status(), StatusCode::OK);

    let body_bytes = response.into_body().collect().await.unwrap().to_bytes();

    // Gzip magic bytes: 0x1f 0x8b. If we see these, the body was NOT decompressed.
    assert!(
        !(body_bytes.len() >= 2 && body_bytes[0] == 0x1f && body_bytes[1] == 0x8b),
        "response body starts with gzip magic bytes — decompression is NOT working"
    );

    // Must be valid UTF-8 text.
    let body_str = String::from_utf8(body_bytes.to_vec())
        .expect("response body should be valid UTF-8, not raw gzip bytes");

    assert!(
        body_str.contains("event:") || body_str.contains("data:"),
        "response should contain SSE event markers, got: {:?}",
        &body_str[..body_str.len().min(200)]
    );
}