Skip to main content

socket_patch_core/
constants.rs

1/// Default path for the patch manifest file relative to the project root.
2pub const DEFAULT_PATCH_MANIFEST_PATH: &str = ".socket/manifest.json";
3
4/// Default public patch API URL for free patches (no auth required).
5pub const DEFAULT_PATCH_API_PROXY_URL: &str = "https://patches-api.socket.dev";
6
7/// Default Socket API URL for authenticated access.
8pub const DEFAULT_SOCKET_API_URL: &str = "https://api.socket.dev";
9
10/// User-Agent header value for API requests.
11///
12/// The version segment is derived from the crate version at compile time so it
13/// tracks the published release (currently `3.x`) instead of drifting from a
14/// hardcoded literal. Server-side analytics and any minimum-version gating rely
15/// on this reporting the real version.
16pub const USER_AGENT: &str = concat!("SocketPatchCLI/", env!("CARGO_PKG_VERSION"));
17
18#[cfg(test)]
19mod tests {
20    use super::*;
21
22    #[test]
23    fn user_agent_reports_real_crate_version() {
24        // Regression: USER_AGENT was pinned to "SocketPatchCLI/1.0" while the
25        // crate shipped 3.x, so every API request / telemetry beacon misreported
26        // the version. It must carry the actual compiled crate version.
27        let expected = format!("SocketPatchCLI/{}", env!("CARGO_PKG_VERSION"));
28        assert_eq!(USER_AGENT, expected);
29        assert!(USER_AGENT.starts_with("SocketPatchCLI/"));
30        assert!(
31            !USER_AGENT.ends_with("/1.0"),
32            "USER_AGENT must not be stuck at the stale 1.0 version"
33        );
34        // The version segment must be non-empty.
35        let version = USER_AGENT.trim_start_matches("SocketPatchCLI/");
36        assert!(!version.is_empty(), "version segment must not be empty");
37    }
38
39    #[test]
40    fn api_urls_are_https_without_trailing_slash() {
41        for url in [DEFAULT_PATCH_API_PROXY_URL, DEFAULT_SOCKET_API_URL] {
42            assert!(url.starts_with("https://"), "{url} must use https");
43            assert!(
44                !url.ends_with('/'),
45                "{url} must not end with a trailing slash"
46            );
47        }
48        // The proxy and authenticated API are distinct hosts; swapping them
49        // would silently send authed traffic to the public proxy (or vice versa).
50        assert_ne!(DEFAULT_PATCH_API_PROXY_URL, DEFAULT_SOCKET_API_URL);
51        assert_eq!(
52            DEFAULT_PATCH_API_PROXY_URL,
53            "https://patches-api.socket.dev"
54        );
55        assert_eq!(DEFAULT_SOCKET_API_URL, "https://api.socket.dev");
56    }
57
58    #[test]
59    fn manifest_path_is_under_dot_socket() {
60        assert_eq!(DEFAULT_PATCH_MANIFEST_PATH, ".socket/manifest.json");
61    }
62}