|
| 1 | +import Foundation |
| 2 | +import Testing |
| 3 | +import URLRouting |
| 4 | +@testableimport VPNLib |
| 5 | + |
| 6 | +@MainActor |
| 7 | +@Suite(.timeLimit(.minutes(1))) |
| 8 | +structCoderRouterTests{ |
| 9 | +letrouter:CoderRouter |
| 10 | + |
| 11 | +init(){ |
| 12 | + router=CoderRouter() |
| 13 | +} |
| 14 | + |
| 15 | +structRouteTestCase:CustomStringConvertible,Sendable{ |
| 16 | +leturlString:String |
| 17 | +letexpectedRoute:CoderRoute? |
| 18 | +letdescription:String |
| 19 | +} |
| 20 | + |
| 21 | +@Test("RDP routes", arguments:[ |
| 22 | + // Valid routes |
| 23 | +RouteTestCase( |
| 24 | + urlString:"coder://coder.example.com/v0/open/ws/myworkspace/agent/dev/rdp?username=user&password=pass", |
| 25 | + expectedRoute:.open( |
| 26 | + workspace:"myworkspace", |
| 27 | + agent:"dev", |
| 28 | + route:.rdp(RDPCredentials(username:"user", password:"pass")) |
| 29 | +), |
| 30 | + description:"RDP with username and password" |
| 31 | +), |
| 32 | +RouteTestCase( |
| 33 | + urlString:"coder://coder.example.com/v0/open/ws/workspace-123/agent/agent-456/rdp", |
| 34 | + expectedRoute:.open( |
| 35 | + workspace:"workspace-123", |
| 36 | + agent:"agent-456", |
| 37 | + route:.rdp(RDPCredentials(username:nil, password:nil)) |
| 38 | +), |
| 39 | + description:"RDP without credentials" |
| 40 | +), |
| 41 | +RouteTestCase( |
| 42 | + urlString:"coder://coder.example.com/v0/open/ws/workspace-123/agent/agent-456/rdp?username=user", |
| 43 | + expectedRoute:.open( |
| 44 | + workspace:"workspace-123", |
| 45 | + agent:"agent-456", |
| 46 | + route:.rdp(RDPCredentials(username:"user", password:nil)) |
| 47 | +), |
| 48 | + description:"RDP with username only" |
| 49 | +), |
| 50 | +RouteTestCase( |
| 51 | + urlString:"coder://coder.example.com/v0/open/ws/workspace-123/agent/agent-456/rdp?password=pass", |
| 52 | + expectedRoute:.open( |
| 53 | + workspace:"workspace-123", |
| 54 | + agent:"agent-456", |
| 55 | + route:.rdp(RDPCredentials(username:nil, password:"pass")) |
| 56 | +), |
| 57 | + description:"RDP with password only" |
| 58 | +), |
| 59 | +RouteTestCase( |
| 60 | + urlString:"coder://coder.example.com/v0/open/ws/ws-special-chars/agent/agent-with-dashes/rdp", |
| 61 | + expectedRoute:.open( |
| 62 | + workspace:"ws-special-chars", |
| 63 | + agent:"agent-with-dashes", |
| 64 | + route:.rdp(RDPCredentials(username:nil, password:nil)) |
| 65 | +), |
| 66 | + description:"RDP with special characters in workspace and agent IDs" |
| 67 | +), |
| 68 | +
|
| 69 | + // Invalid routes |
| 70 | +RouteTestCase( |
| 71 | + urlString:"coder://coder.example.com/invalid/path", |
| 72 | + expectedRoute:nil, |
| 73 | + description:"Completely invalid path" |
| 74 | +), |
| 75 | +RouteTestCase( |
| 76 | + urlString:"coder://coder.example.com/v1/open/ws/workspace-123/agent/agent-456/rdp", |
| 77 | + expectedRoute:nil, |
| 78 | + description:"Invalid version prefix (v1 instead of v0)" |
| 79 | +), |
| 80 | +RouteTestCase( |
| 81 | + urlString:"coder://coder.example.com/v0/open/workspace-123/agent/agent-456/rdp", |
| 82 | + expectedRoute:nil, |
| 83 | + description:"Missing 'ws' segment" |
| 84 | +), |
| 85 | +RouteTestCase( |
| 86 | + urlString:"coder://coder.example.com/v0/open/ws/workspace-123/rdp", |
| 87 | + expectedRoute:nil, |
| 88 | + description:"Missing agent segment" |
| 89 | +), |
| 90 | +RouteTestCase( |
| 91 | + urlString:"http://coder.example.com/v0/open/ws/workspace-123/agent/agent-456", |
| 92 | + expectedRoute:nil, |
| 93 | + description:"Wrong scheme" |
| 94 | +), |
| 95 | +]) |
| 96 | +func testRdpRoutes(testCase:RouteTestCase)throws{ |
| 97 | +leturl=URL(string: testCase.urlString)! |
| 98 | + |
| 99 | +iflet expectedRoute= testCase.expectedRoute{ |
| 100 | +letroute=try router.match(url: url) |
| 101 | + #expect(route== expectedRoute) |
| 102 | +}else{ |
| 103 | + #expect(throws:(anyError).self){ |
| 104 | + _=try router.match(url: url) |
| 105 | +} |
| 106 | +} |
| 107 | +} |
| 108 | +} |