@@ -9,28 +9,36 @@ import XCTest
99@testable import MultipartFormData
1010
1111final class ContentDispositionTests : XCTestCase {
12- func testPercentEncodingError ( ) throws {
12+ func testUncheckedInitValid ( ) {
1313 XCTAssertNoThrow ( try ContentDisposition ( uncheckedName: " a " , uncheckedFilename: " a " ) )
14-
14+ }
15+
16+ func testUncheckedInitInvalid( ) throws {
1517 // https://stackoverflow.com/questions/33558933/why-is-the-return-value-of-string-addingpercentencoding-optional
16- // does not work on Linux, can still encoding there
17- let nonPercentEncodableString = try XCTUnwrap ( String ( bytes : [ 0xD8 , 0x00 ] as [ UInt8 ] , encoding : . utf16BigEndian ) )
18- if nonPercentEncodableString . addingPercentEncoding ( withAllowedCharacters : . urlPathAllowed ) == nil {
19- XCTAssertThrowsError ( try ContentDisposition ( uncheckedName : nonPercentEncodableString , uncheckedFilename : nil ) )
20- XCTAssertThrowsError ( try ContentDisposition ( uncheckedName : " " , uncheckedFilename : nonPercentEncodableString ) )
18+ let bytes : [ UInt8 ] = [ 0xD8 , 0x00 ]
19+
20+ // Ensure the non-encodable string can be created, e.g. on iOS 18 it no longer works.
21+ guard let nonPercentEncodableString = String ( bytes : bytes , encoding : . utf16BigEndian ) else {
22+ throw XCTSkip ( " UTF16 byte encoding failed " )
2123 }
24+
25+ // Ensure the percent-encoding fails on the current platform, e.g. on Linux it encodes.
26+ guard nonPercentEncodableString. addingPercentEncoding ( withAllowedCharacters: . urlPathAllowed) == nil else {
27+ throw XCTSkip ( " percent encoding didn't fail " )
28+ }
29+
30+ XCTAssertThrowsError ( try ContentDisposition ( uncheckedName: nonPercentEncodableString, uncheckedFilename: nil ) )
31+ XCTAssertThrowsError ( try ContentDisposition ( uncheckedName: " " , uncheckedFilename: nonPercentEncodableString) )
2232 }
2333
2434 func testParameters( ) throws {
2535 let contentDisposition = ContentDisposition ( name: " a " , filename: " a " )
26-
2736 XCTAssertEqual ( contentDisposition. parameters [ 0 ] , HTTPHeaderParameter ( " name " , value: " a " ) )
2837 XCTAssertEqual ( contentDisposition. parameters [ 1 ] , HTTPHeaderParameter ( " filename " , value: " a " ) )
2938 }
3039
3140 func testData( ) throws {
3241 let contentDisposition = ContentDisposition ( name: " a " , filename: " a " )
33-
3442 XCTAssertEqual ( contentDisposition. _data, Data ( " Content-Disposition: form-data; name= \" a \" ; filename= \" a \" " . utf8) )
3543 }
3644}
0 commit comments