in Sources/PackageRegistryCompatibilityTestSuite/APITests/CreatePackageRelease.swift [34:105]
func run() async {
let randomString = randomAlphaNumericString(length: 6)
let randomScope = "test-\(randomString)"
let randomName = "package-\(randomString)"
for packageRelease in self.configuration.packageReleases {
let scope = packageRelease.package?.scope ?? randomScope
let name = packageRelease.package?.name ?? randomName
self.log.append(await TestCase(name: "Create package release \(scope).\(name)@\(packageRelease.version)") { testCase in
let response = try await self.createPackageRelease(packageRelease, scope: scope, name: name, for: &testCase)
testCase.mark("HTTP response status")
switch response.status {
// 4.6.3.1 Server must return 201 if publication is done synchronously
case .created:
// 3.5 Server must set "Content-Version" header
self.checkContentVersionHeader(response.headers, for: &testCase)
// 4.6.3.1 Server should set "Location" header
testCase.mark("\"Location\" response header")
let locationHeader = response.headers["Location"].first
if locationHeader == nil {
testCase.warning("\"Location\" header should be set")
}
// 4.6.3.2 Server must return 202 if publication is done asynchronously
case .accepted:
// 3.5 Server must set "Content-Version" header
self.checkContentVersionHeader(response.headers, for: &testCase)
// 4.6.3.2 Server must set "Location" header
testCase.mark("\"Location\" response header")
guard let locationHeader = response.headers["Location"].first else {
throw TestError("Missing \"Location\" header")
}
// Poll status until it finishes
testCase.mark("Poll \(locationHeader) until publication finishes")
try await Task.detached {
try await self.poll(url: locationHeader, after: self.getRetryTimestamp(headers: response.headers),
deadline: DispatchTime.now() + .seconds(self.configuration.maxProcessingTimeInSeconds))
}.value
default:
throw TestError("Expected HTTP status code 201 or 202 but got \(response.status.code)")
}
})
}
for packageRelease in self.configuration.packageReleases {
// Also test case-insensitivity
let scope = (packageRelease.package?.scope ?? randomScope).flipcased
let name = (packageRelease.package?.name ?? randomName).flipcased
self.log.append(await TestCase(name: "Publish duplicate package release \(scope).\(name)@\(packageRelease.version)") { testCase in
let response = try await self.createPackageRelease(packageRelease, scope: scope, name: name, for: &testCase)
// 4.6 Server should return 409 if package release already exists
testCase.mark("HTTP response status")
guard response.status == .conflict else {
throw TestError("Expected HTTP status code 409 but got \(response.status.code)")
}
// 3.3 Server should communicate errors using "problem details" object
testCase.mark("Response body")
if response.body == nil {
testCase.warning("Response should include problem details")
}
})
}
self.printLog()
}