|
@@ -81,67 +81,25 @@ describe('slice test', (report, done) => {
|
81
|
81
|
})
|
82
|
82
|
})
|
83
|
83
|
|
|
84
|
+describe('Upload multipart/form-data', (report, done) => {
|
84
|
85
|
|
85
|
|
-describe('fs.slice test', (report, done) => {
|
|
86
|
+ RNFetchBlob.fetch('POST', `${TEST_SERVER_URL}/upload-form`, {
|
|
87
|
+ Authorization : "Bearer fsXcpmKPrHgAAAAAAAAAEGxFXwhejXM_E8fznZoXPhHbhbNhA-Lytbe6etp1Jznz",
|
|
88
|
+ 'Content-Type' : 'multipart/form-data',
|
|
89
|
+ }, [
|
|
90
|
+ // { name : 'test-img', filename : 'test-img.png', data: image},
|
|
91
|
+ // { name : 'test-text', filename : 'test-text.txt', data: RNFetchBlob.base64.encode('hello.txt')},
|
|
92
|
+ { name : 'field1', data : 'hello !!'},
|
|
93
|
+ { name : 'field2', data : 'hello2 !!'}
|
|
94
|
+ ])
|
|
95
|
+ .then((resp) => {
|
|
96
|
+ console.log(resp.json())
|
|
97
|
+ resp = resp.json()
|
86
|
98
|
|
87
|
|
- let source = null
|
88
|
|
- let parts = fs.dirs.DocumentDir + '/tmp-source-'
|
89
|
|
- let dests = []
|
90
|
|
- let combined = fs.dirs.DocumentDir + '/combined-' + Date.now() + '.jpg'
|
91
|
|
- let size = 0
|
92
|
|
-
|
93
|
|
- window.fetch = new RNFetchBlob.polyfill.Fetch({
|
94
|
|
- auto : true,
|
95
|
|
- binaryContentTypes : ['image/', 'video/', 'audio/']
|
96
|
|
- }).build()
|
97
|
|
-
|
98
|
|
- fetch(`${TEST_SERVER_URL}/public/github2.jpg`)
|
99
|
|
- .then((res) => res.rawResp())
|
100
|
|
- .then((res) => {
|
101
|
|
- source = res.path()
|
102
|
|
- return fs.stat(source)
|
103
|
|
- })
|
104
|
|
- // separate file into 4kb chunks
|
105
|
|
- .then((stat) => {
|
106
|
|
- size = stat.size
|
107
|
|
- let promise = Promise.resolve()
|
108
|
|
- let cursor = 0
|
109
|
|
- while(cursor < size) {
|
110
|
|
- promise = promise.then(function(start) {
|
111
|
|
- console.log('slicing part ', start , start + 40960)
|
112
|
|
- let offset = 0
|
113
|
|
- return fs.slice(source, parts + start, start + offset, start + 40960)
|
114
|
|
- .then((dest) => {
|
115
|
|
- console.log('slicing part ', start + offset, start + 40960, 'done')
|
116
|
|
- dests.push(dest)
|
117
|
|
- return Promise.resolve()
|
118
|
|
- })
|
119
|
|
- }.bind(this, cursor))
|
120
|
|
- cursor += 40960
|
121
|
|
- }
|
122
|
|
- console.log('loop end')
|
123
|
|
- return promise
|
124
|
|
- })
|
125
|
|
- // combine chunks and verify the result
|
126
|
|
- .then(() => {
|
127
|
|
- console.log('combinding files')
|
128
|
|
- let p = Promise.resolve()
|
129
|
|
- for(let d in dests) {
|
130
|
|
- p = p.then(function(chunk){
|
131
|
|
- return fs.appendFile(combined, chunk, 'uri').then((write) => {
|
132
|
|
- console.log(write, 'bytes write')
|
133
|
|
- })
|
134
|
|
- }.bind(this, dests[d]))
|
135
|
|
- }
|
136
|
|
- return p.then(() => fs.stat(combined))
|
137
|
|
- })
|
138
|
|
- .then((stat) => {
|
139
|
99
|
report(
|
140
|
|
- <Assert key="verify file size" expect={size} actual={stat.size}/>,
|
141
|
|
- <Info key="image viewer">
|
142
|
|
- <Image key="combined image" style={styles.image} source={{ uri : prefix + combined}}/>
|
143
|
|
- </Info>)
|
|
100
|
+ <Assert key="check posted form data #1" expect="hello !!" actual={resp.fields.field1}/>,
|
|
101
|
+ <Assert key="check posted form data #2" expect="hello2 !!" actual={resp.fields.field2}/>,
|
|
102
|
+ )
|
144
|
103
|
done()
|
145
|
104
|
})
|
146
|
|
-
|
147
|
105
|
})
|