Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f602fa1

Browse files
committedJan 8, 2025·
http test server -> mocked requests
1 parent 48b35f0 commit f602fa1

File tree

3 files changed

+35
-122
lines changed

3 files changed

+35
-122
lines changed
 

‎Coder Desktop/Coder Desktop.xcodeproj/project.pbxproj

+10-10
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
AA3B3DC12D2D23AB0099996A /* SwiftProtobufPluginLibrary in Frameworks */ = {isa = PBXBuildFile; productRef = AA3B3DC02D2D23AB0099996A /* SwiftProtobufPluginLibrary */; };
1919
AA3B3DCD2D2D249F0099996A /* VPNLib.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = AA3B3DA12D2D23860099996A /* VPNLib.framework */; };
2020
AA3B3DCE2D2D249F0099996A /* VPNLib.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = AA3B3DA12D2D23860099996A /* VPNLib.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
21-
AA3B3DD22D2D26700099996A /* Swifter in Frameworks */ = {isa = PBXBuildFile; productRef = AA3B3DD12D2D26700099996A /* Swifter */; };
21+
AA3B3E8E2D2E0FF40099996A /* Mocker in Frameworks */ = {isa = PBXBuildFile; productRef = AA3B3E8D2D2E0FF40099996A /* Mocker */; };
2222
AA8BC3392D0060A900E1ABAA /* ViewInspector in Frameworks */ = {isa = PBXBuildFile; productRef = AA8BC3382D0060A900E1ABAA /* ViewInspector */; };
2323
AA8BC33F2D0061F200E1ABAA /* FluidMenuBarExtra in Frameworks */ = {isa = PBXBuildFile; productRef = AA8BC33E2D0061F200E1ABAA /* FluidMenuBarExtra */; };
2424
AA8BC4CF2D00A4B700E1ABAA /* KeychainAccess in Frameworks */ = {isa = PBXBuildFile; productRef = AA8BC4CE2D00A4B700E1ABAA /* KeychainAccess */; };
@@ -234,7 +234,7 @@
234234
buildActionMask = 2147483647;
235235
files = (
236236
AA3B3DA92D2D23860099996A /* VPNLib.framework in Frameworks */,
237-
AA3B3DD22D2D26700099996A /* Swifter in Frameworks */,
237+
AA3B3E8E2D2E0FF40099996A /* Mocker in Frameworks */,
238238
);
239239
runOnlyForDeploymentPostprocessing = 0;
240240
};
@@ -436,7 +436,7 @@
436436
);
437437
name = VPNLibTests;
438438
packageProductDependencies = (
439-
AA3B3DD12D2D26700099996A /* Swifter */,
439+
AA3B3E8D2D2E0FF40099996A /* Mocker */,
440440
);
441441
productName = VPNLibTests;
442442
productReference = AA3B3DA82D2D23860099996A /* VPNLibTests.xctest */;
@@ -491,7 +491,7 @@
491491
AA8BC4CD2D00A4B700E1ABAA /* XCRemoteSwiftPackageReference "KeychainAccess" */,
492492
AAD720CE2D0816B200F6304D /* XCRemoteSwiftPackageReference "Alamofire" */,
493493
961679512CFF207900B2B6DF /* XCRemoteSwiftPackageReference "swift-protobuf" */,
494-
AA03806B2D2CD853002C2285 /* XCRemoteSwiftPackageReference "swifter" */,
494+
AA3B3E8A2D2E0FE10099996A /* XCRemoteSwiftPackageReference "Mocker" */,
495495
);
496496
preferredProjectObjectVersion = 77;
497497
productRefGroup = 961678FD2CFF100D00B2B6DF /* Products */;
@@ -1142,12 +1142,12 @@
11421142
version = 1.28.2;
11431143
};
11441144
};
1145-
AA03806B2D2CD853002C2285 /* XCRemoteSwiftPackageReference "swifter" */ = {
1145+
AA3B3E8A2D2E0FE10099996A /* XCRemoteSwiftPackageReference "Mocker" */ = {
11461146
isa = XCRemoteSwiftPackageReference;
1147-
repositoryURL = "https://github.com/httpswift/swifter";
1147+
repositoryURL = "https://github.com/WeTransfer/Mocker";
11481148
requirement = {
11491149
kind = upToNextMajorVersion;
1150-
minimumVersion = 1.5.0;
1150+
minimumVersion = 3.0.2;
11511151
};
11521152
};
11531153
AA8BC3372D00609700E1ABAA /* XCRemoteSwiftPackageReference "ViewInspector" */ = {
@@ -1213,10 +1213,10 @@
12131213
package = 961679512CFF207900B2B6DF /* XCRemoteSwiftPackageReference "swift-protobuf" */;
12141214
productName = SwiftProtobufPluginLibrary;
12151215
};
1216-
AA3B3DD12D2D26700099996A /* Swifter */ = {
1216+
AA3B3E8D2D2E0FF40099996A /* Mocker */ = {
12171217
isa = XCSwiftPackageProductDependency;
1218-
package = AA03806B2D2CD853002C2285 /* XCRemoteSwiftPackageReference "swifter" */;
1219-
productName = Swifter;
1218+
package = AA3B3E8A2D2E0FE10099996A /* XCRemoteSwiftPackageReference "Mocker" */;
1219+
productName = Mocker;
12201220
};
12211221
AA8BC3382D0060A900E1ABAA /* ViewInspector */ = {
12221222
isa = XCSwiftPackageProductDependency;

‎Coder Desktop/Coder Desktop.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"originHash" : "0dee63153808d20a73f823faee354b1c4e55e5f9258a2831fa6074ed154cd0bc",
2+
"originHash" : "1cd4f7368eeddbaa35ef829e13093bc7081a4e6d3da9492d22db0757464ad473",
33
"pins" : [
44
{
55
"identity" : "alamofire",
@@ -28,21 +28,21 @@
2828
}
2929
},
3030
{
31-
"identity" : "swift-protobuf",
31+
"identity" : "mocker",
3232
"kind" : "remoteSourceControl",
33-
"location" : "https://github.com/apple/swift-protobuf.git",
33+
"location" : "https://github.com/WeTransfer/Mocker",
3434
"state" : {
35-
"revision" : "ebc7251dd5b37f627c93698e4374084d98409633",
36-
"version" : "1.28.2"
35+
"revision" : "95fa785c751f6bc40c49e112d433c3acf8417a97",
36+
"version" : "3.0.2"
3737
}
3838
},
3939
{
40-
"identity" : "swifter",
40+
"identity" : "swift-protobuf",
4141
"kind" : "remoteSourceControl",
42-
"location" : "https://github.com/httpswift/swifter",
42+
"location" : "https://github.com/apple/swift-protobuf.git",
4343
"state" : {
44-
"revision" : "9483a5d459b45c3ffd059f7b55f9638e268632fd",
45-
"version" : "1.5.0"
44+
"revision" : "ebc7251dd5b37f627c93698e4374084d98409633",
45+
"version" : "1.28.2"
4646
}
4747
},
4848
{
+16-103
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Foundation
2-
import Swifter
2+
import Mocker
33
import Testing
44
@testable import VPNLib
55

@@ -14,18 +14,11 @@ struct DownloaderTests {
1414
@Test
1515
func downloadFile() async throws {
1616
let destinationURL = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString)
17-
18-
let server = HttpServer()
1917
let testData = Data("foo".utf8)
2018

21-
server["/test.txt"] = { _ in
22-
HttpResponse.ok(.data(testData))
23-
}
24-
25-
try server.start(freePort())
26-
defer { server.stop() }
19+
let fileURL = URL(string: "http://example.com/test1.txt")!
20+
Mock(url: fileURL, contentType: .html, statusCode: 200, data: [.get: testData]).register()
2721

28-
let fileURL = try URL(string: "http://localhost:\(server.port())/test.txt")!
2922
try await downloader.download(src: fileURL, dest: destinationURL)
3023

3124
try #require(FileManager.default.fileExists(atPath: destinationURL.path))
@@ -37,126 +30,46 @@ struct DownloaderTests {
3730

3831
@Test
3932
func fileNotModified() async throws {
40-
let server = HttpServer()
4133
let testData = Data("foo bar".utf8)
34+
let fileURL = URL(string: "http://example.com/test2.txt")!
4235

4336
let destinationURL = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString)
37+
defer { try? FileManager.default.removeItem(at: destinationURL) }
4438

45-
var notModifiedSent = false
46-
server["/test.txt"] = { req in
47-
let etag = etag(data: testData)
48-
if let ifNoneMatch = req.headers["if-none-match"], ifNoneMatch == etag {
49-
notModifiedSent = true
50-
return .raw(304, "Not Modified", nil, nil)
51-
} else {
52-
return .ok(.data(testData))
53-
}
54-
}
55-
56-
try server.start(freePort())
57-
defer { server.stop() }
58-
59-
let fileURL = try URL(string: "http://localhost:\(server.port())/test.txt")!
60-
try await downloader.download(src: fileURL, dest: destinationURL)
39+
Mock(url: fileURL, contentType: .html, statusCode: 200, data: [.get: testData]).register()
6140

41+
try await downloader.download(src: fileURL, dest: destinationURL)
6242
try #require(FileManager.default.fileExists(atPath: destinationURL.path))
63-
defer { try? FileManager.default.removeItem(at: destinationURL) }
6443
let downloadedData = try Data(contentsOf: destinationURL)
6544
#expect(downloadedData == testData)
6645

46+
Mock(url: fileURL, contentType: .html, statusCode: 304, data: [.get: Data()]).register()
47+
6748
try await downloader.download(src: fileURL, dest: destinationURL)
68-
#expect(downloadedData == testData)
69-
#expect(notModifiedSent)
49+
let unchangedData = try Data(contentsOf: destinationURL)
50+
#expect(unchangedData == testData)
7051
}
7152

7253
@Test
7354
func fileUpdated() async throws {
74-
let server = HttpServer()
7555
let ogData = Data("foo bar".utf8)
7656
let newData = Data("foo bar qux".utf8)
7757

58+
let fileURL = URL(string: "http://example.com/test3.txt")!
7859
let destinationURL = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString)
60+
defer { try? FileManager.default.removeItem(at: destinationURL) }
7961

80-
server["/test.txt"] = { _ in
81-
.ok(.data(ogData))
82-
}
83-
84-
try server.start(freePort())
85-
defer { server.stop() }
86-
87-
let fileURL = try URL(string: "http://localhost:\(server.port())/test.txt")!
62+
Mock(url: fileURL, contentType: .html, statusCode: 200, data: [.get: ogData]).register()
8863

8964
try await downloader.download(src: fileURL, dest: destinationURL)
9065
try #require(FileManager.default.fileExists(atPath: destinationURL.path))
91-
defer { try? FileManager.default.removeItem(at: destinationURL) }
9266
var downloadedData = try Data(contentsOf: destinationURL)
9367
#expect(downloadedData == ogData)
9468

95-
server["/test.txt"] = { _ in
96-
.ok(.data(newData))
97-
}
69+
Mock(url: fileURL, contentType: .html, statusCode: 200, data: [.get: newData]).register()
70+
9871
try await downloader.download(src: fileURL, dest: destinationURL)
9972
downloadedData = try Data(contentsOf: destinationURL)
10073
#expect(downloadedData == newData)
10174
}
10275
}
103-
104-
// From https://stackoverflow.com/questions/65670932/how-to-find-a-free-local-port-using-swift
105-
func freePort() -> UInt16 {
106-
var port: UInt16 = 8000
107-
108-
let socketFD = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)
109-
if socketFD == -1 {
110-
return port
111-
}
112-
113-
var hints = addrinfo(
114-
ai_flags: AI_PASSIVE,
115-
ai_family: AF_INET,
116-
ai_socktype: SOCK_STREAM,
117-
ai_protocol: 0,
118-
ai_addrlen: 0,
119-
ai_canonname: nil,
120-
ai_addr: nil,
121-
ai_next: nil
122-
)
123-
124-
var addressInfo: UnsafeMutablePointer<addrinfo>?
125-
var result = getaddrinfo(nil, "0", &hints, &addressInfo)
126-
if result != 0 {
127-
close(socketFD)
128-
return port
129-
}
130-
131-
result = Darwin.bind(socketFD, addressInfo!.pointee.ai_addr, socklen_t(addressInfo!.pointee.ai_addrlen))
132-
if result == -1 {
133-
close(socketFD)
134-
return port
135-
}
136-
137-
result = Darwin.listen(socketFD, 1)
138-
if result == -1 {
139-
close(socketFD)
140-
return port
141-
}
142-
143-
var addr_in = sockaddr_in()
144-
addr_in.sin_len = UInt8(MemoryLayout.size(ofValue: addr_in))
145-
addr_in.sin_family = sa_family_t(AF_INET)
146-
147-
var len = socklen_t(addr_in.sin_len)
148-
result = withUnsafeMutablePointer(to: &addr_in) {
149-
$0.withMemoryRebound(to: sockaddr.self, capacity: 1) {
150-
Darwin.getsockname(socketFD, $0, &len)
151-
}
152-
}
153-
154-
if result == 0 {
155-
port = addr_in.sin_port
156-
}
157-
158-
Darwin.shutdown(socketFD, SHUT_RDWR)
159-
close(socketFD)
160-
161-
return port
162-
}

0 commit comments

Comments
 (0)
Please sign in to comment.