@@ -3,85 +3,158 @@ import Foundation
33import Testing
44
55struct BodyMakerTests {
6- private typealias Parameter = BodyMaker . Parameter
6+ @Test
7+ func parameterInitializationFromDictionaryElement( ) {
8+ let element = ( " testKey " , " testValue " )
9+ let parameter = BodyMaker . Parameter ( from: element)
10+ #expect( parameter. key == " testKey " )
11+ #expect( parameter. value == " testValue " )
12+ }
13+
14+ // MARK: - makeBody
715
816 @Test
9- func makeBody_noParameters( ) {
10- let parameters = [ Parameter < TestKey > ] ( )
11- let result = BodyMaker . makeBody ( with: parameters)
17+ func makeBodyWithNoParametersReturnsNil( ) {
18+ let result = BodyMaker . makeBody ( with: [ ] )
1219 #expect( result == nil )
1320 }
1421
1522 @Test
16- func makeBody_validParameters( ) throws {
17- let parameters : [ Parameter < TestKey > ] = [
18- . init( key: TestKey . name. rawValue, value: " John " ) ,
19- . init( key: TestKey . age. rawValue, value: " 30 " )
23+ func makeBodyWithSingleParameter( ) throws {
24+ let parameter = BodyMaker . Parameter ( key: " name " , value: " John " )
25+ let result = try #require( BodyMaker . makeBody ( with: [ parameter] ) )
26+ let expectedString = " name=John "
27+ #expect( String ( data: result, encoding: . utf8) == expectedString)
28+ }
29+
30+ @Test
31+ func makeBodyWithMultipleParameters( ) throws {
32+ let params = [
33+ BodyMaker . Parameter ( key: " a " , value: " 1 " ) ,
34+ BodyMaker . Parameter ( key: " b " , value: " 2 " )
2035 ]
21- let expectedData = try #require( " name=John&age=30 " . data ( using : . utf8 ) )
22- let result = try #require ( BodyMaker . makeBody ( with : parameters ) )
23- #expect( result == expectedData )
36+ let result = try #require( BodyMaker . makeBody ( with : params ) )
37+ let expectedString = " a=1&b=2 "
38+ #expect( String ( data : result, encoding : . utf8 ) == expectedString )
2439 }
2540
41+ // MARK: - makeBodyWithMultipartForm
42+
2643 @Test
27- func makeBodyWithMultipartForm_noParameters_noMedia( ) {
28- let parameters = [ Parameter < TestKey > ] ( )
29- let result = BodyMaker . makeBodyWithMultipartForm ( with: parameters, and: nil )
44+ func multipartFormWithNoContentReturnsNil( ) {
45+ let result = BodyMaker . makeBodyWithMultipartForm (
46+ parameters: [ ] ,
47+ media: nil ,
48+ boundary: " BOUNDARY "
49+ )
3050 #expect( result == nil )
3151 }
3252
3353 @Test
34- func makeBodyWithMultipartForm_onlyDictionary( ) throws {
35- let parameters : [ Parameter < TestKey > ] = [
36- . init( key: TestKey . name. rawValue, value: " John " ) ,
37- . init( key: TestKey . age. rawValue, value: " 30 " )
54+ func multipartFormWithParametersOnly( ) throws {
55+ let params = [ BodyMaker . Parameter ( key: " text " , value: " Hello " ) ]
56+ let boundary = " TESTBOUNDARY "
57+ let result = try #require( BodyMaker . makeBodyWithMultipartForm (
58+ parameters: params,
59+ media: nil ,
60+ boundary: boundary
61+ ) )
62+ let string = try #require( String ( data: result, encoding: . utf8) )
63+ let expectedPatterns = [
64+ " --TESTBOUNDARY \r \n " ,
65+ " Content-Disposition: form-data; name= \" text \" \r \n \r \n " ,
66+ " Hello \r \n " ,
67+ " --TESTBOUNDARY-- \r \n "
3868 ]
39- let expectedData = try #require(
40- " --FFF \r \n Content-Disposition: form-data; name= \" name \" \r \n \r \n John \r \n --FFF \r \n Content-Disposition: form-data; name= \" age \" \r \n \r \n 30 \r \n --FFF-- \r \n "
41- . data ( using: . utf8)
42- )
43- let result = try #require( BodyMaker . makeBodyWithMultipartForm ( with: parameters, and: nil ) )
44- #expect( result == expectedData)
69+ for pattern in expectedPatterns {
70+ #expect( string. contains ( pattern) )
71+ }
4572 }
4673
4774 @Test
48- func makeBodyWithMultipartForm_onlyMedia( ) throws {
49- let parameters = [ Parameter < TestKey > ] ( )
50- let mediaFile = BodyMaker . MediaFile (
51- key: " file " ,
52- filename: " test.png " ,
53- data: Data ( " Test image content " . utf8) ,
54- mimeType: " image/png "
55- )
56- let expectedData = try #require(
57- " --FFF \r \n Content-Disposition: form-data; name= \" file \" ; filename= \" test.png \" \r \n Content-Type: image/png \r \n \r \n Test image content \r \n --FFF-- \r \n "
58- . data ( using: . utf8)
59- )
60- let result = try #require( BodyMaker . makeBodyWithMultipartForm ( with: parameters, and: [ mediaFile] ) )
61- #expect( result == expectedData)
75+ func multipartFormWithMediaOnly( ) throws {
76+ let media = [
77+ BodyMaker . MediaFile (
78+ key: " file " ,
79+ filename: " test.txt " ,
80+ data: Data ( " file content " . utf8) ,
81+ mimeType: " text/plain "
82+ )
83+ ]
84+ let boundary = " MEDIA_BOUNDARY "
85+ let result = try #require( BodyMaker . makeBodyWithMultipartForm (
86+ parameters: [ ] ,
87+ media: media,
88+ boundary: boundary
89+ ) )
90+ let string = try #require( String ( data: result, encoding: . utf8) )
91+ let expectedPatterns = [
92+ " --MEDIA_BOUNDARY \r \n " ,
93+ " Content-Disposition: form-data; name= \" file \" ; filename= \" test.txt \" \r \n " ,
94+ " Content-Type: text/plain \r \n \r \n " ,
95+ " file content \r \n " ,
96+ " --MEDIA_BOUNDARY-- \r \n "
97+ ]
98+ for pattern in expectedPatterns {
99+ #expect( string. contains ( pattern) )
100+ }
62101 }
63102
64103 @Test
65- func makeBodyWithMultipartForm_dictionaryAndMedia( ) throws {
66- let parameters : [ Parameter < TestKey > ] = [ . init( key: TestKey . description. rawValue, value: " A test image " ) ]
67- let mediaFile = BodyMaker . MediaFile (
68- key: " file " ,
69- filename: " test.png " ,
70- data: Data ( " Test image content " . utf8) ,
71- mimeType: " image/png "
72- )
73- let expectedData = try #require(
74- " --FFF \r \n Content-Disposition: form-data; name= \" description \" \r \n \r \n A test image \r \n --FFF \r \n Content-Disposition: form-data; name= \" file \" ; filename= \" test.png \" \r \n Content-Type: image/png \r \n \r \n Test image content \r \n --FFF-- \r \n "
75- . data ( using: . utf8)
76- )
77- let result = try #require( BodyMaker . makeBodyWithMultipartForm ( with: parameters, and: [ mediaFile] ) )
78- #expect( result == expectedData)
104+ func multipartFormWithMixedContent( ) throws {
105+ let params = [ BodyMaker . Parameter ( key: " title " , value: " Document " ) ]
106+ let media = [
107+ BodyMaker . MediaFile (
108+ key: " doc " ,
109+ filename: " doc.pdf " ,
110+ data: Data ( " pdf content " . utf8) ,
111+ mimeType: " application/pdf "
112+ )
113+ ]
114+ let boundary = " MIXEDBOUNDARY "
115+ let result = try #require( BodyMaker . makeBodyWithMultipartForm (
116+ parameters: params,
117+ media: media,
118+ boundary: boundary
119+ ) )
120+
121+ let string = try #require( String ( data: result, encoding: . utf8) )
122+
123+ // Проверяем порядок: сначала параметры, потом медиа
124+ let paramSection = """
125+ --MIXEDBOUNDARY \r \n \
126+ Content-Disposition: form-data; name= " title " \r \n \r \n \
127+ Document \r \n
128+ """
129+
130+ let mediaSection = """
131+ --MIXEDBOUNDARY \r \n \
132+ Content-Disposition: form-data; name= " doc " ; filename= " doc.pdf " \r \n \
133+ Content-Type: application/pdf \r \n \r \n \
134+ pdf content \r \n
135+ """
136+
137+ let closing = " --MIXEDBOUNDARY-- \r \n "
138+
139+ #expect( string. contains ( paramSection) )
140+ #expect( string. contains ( mediaSection) )
141+ #expect( string. contains ( closing) )
79142 }
80- }
81143
82- /// Пример ключа для тестирования
83- private enum TestKey : String {
84- case name
85- case age
86- case description
144+ // MARK: - MediaFile Tests
145+
146+ @Test
147+ func mediaFileInitialization( ) {
148+ let data = Data ( " test " . utf8)
149+ let media = BodyMaker . MediaFile (
150+ key: " avatar " ,
151+ filename: " image.jpg " ,
152+ data: data,
153+ mimeType: " image/jpeg "
154+ )
155+ #expect( media. key == " avatar " )
156+ #expect( media. filename == " image.jpg " )
157+ #expect( media. data == data)
158+ #expect( media. mimeType == " image/jpeg " )
159+ }
87160}
0 commit comments