Pārlūkot izejas kodu

Add fetch replacement test cases #70

Ben Hsieh 8 gadus atpakaļ
vecāks
revīzija
969daaae11
1 mainītis faili ar 83 papildinājumiem un 6 dzēšanām
  1. 83
    6
      test/test-fetch.js

+ 83
- 6
test/test-fetch.js Parādīt failu

@@ -31,13 +31,14 @@ const dirs = RNFetchBlob.fs.dirs
31 31
 
32 32
 let prefix = ((Platform.OS === 'android') ? 'file://' : '')
33 33
 
34
-describe('GET request test : text -> any', (report, done) => {
34
+describe('GET request test : unicode text -> any', (report, done) => {
35 35
 
36 36
   function get(fn1, fn2) {
37 37
     return fetch(`${TEST_SERVER_URL}/unicode`, { method : 'GET'})
38 38
     .then((res) => fn1(res))
39 39
     .then((data) => fn2(data))
40 40
   }
41
+
41 42
   let promises =
42 43
   [
43 44
     get((res) => res.json(), (json) => {
@@ -47,9 +48,7 @@ describe('GET request test : text -> any', (report, done) => {
47 48
       report(<Assert key="text data correct" expect={'你好!'} actual={JSON.parse(text).data}/>)
48 49
     }),
49 50
     get((res) => res.blob(), (blob) => {
50
-      let path = blob.getRNFetchBlobRef()
51
-      return fs.readFile(path, 'utf8').then((text) => {
52
-        console.log(text)
51
+      return blob.readBlob('utf8').then((text) => {
53 52
         report(<Assert key="blob data correct" expect={'你好!'} actual={JSON.parse(text).data}/>)
54 53
       })
55 54
     }),
@@ -64,10 +63,88 @@ describe('GET request test : text -> any', (report, done) => {
64 63
 
65 64
 })
66 65
 
67
-describe('GET request which has json response', (report, done) => {
66
+describe('GET request test : path -> any', (report, done) => {
67
+
68
+  function get(fn1, fn2, fn3) {
69
+    fetch(`${TEST_SERVER_URL}/public/github.png`, { method : 'GET'})
70
+      .then((res) => fn1(res))
71
+      .then((data) => fn2(data))
72
+      .catch((err) => fn3(err))
73
+  }
74
+  let contentLength = 0
75
+  let promises = [
76
+    get((res) => res.json(), (data) => {
77
+      report(<Assert key="should not convert blob to JSON" expect={true} actual={false} />)
78
+    }, (err) => {
79
+      report(<Assert key="should not convert blob to JSON" expect={true} actual={true} />)
80
+    }),
81
+    get((res) => {
82
+      contentLength = res.headers['Content-Length']
83
+      return res.text()
84
+    }, (data) => {
85
+      report(
86
+        <Assert key="should convert blob to text" expect={true} actual={true} />,
87
+        <Assert key="content length should correct" expect={Math.floor(contentLength)} actual={data.length} />)
88
+    }, (err) => {
89
+      console.warn(err, err.stack)
90
+      report(<Assert key="should convert blob to text" expect={true} actual={false} />)
91
+    }),
92
+    get((res) => {
93
+      contentLength = res.headers['Content-Length']
94
+      return res.blob()
95
+    }, (blob) => {
96
+      return fs.stat(blob.getRNFetchBlobRef()).then((stat) => {
97
+        report(<Assert key="stored file size correct" expect={contentLength} actual={stat.size} />)
98
+        return blob.readBlob('base64')
99
+      })
100
+      .then((b64) => {
101
+        report(<Info key="stored image">
102
+          <Image style={styles.image} source={{uri : 'data:image/png;BASE64 ,' + b64}}/>
103
+        </Info>)
104
+      })
105
+
106
+    }, (err) => {
107
+      console.warn(err, err.stack)
108
+      report(<Assert key="should convert blob to blob" expect={true} actual={false} />)
109
+    })
110
+  ]
111
+  Promise.all(promises).then( () => done() )
68 112
 
69 113
 })
70 114
 
71
-describe('GET request which has blob response', (report, done) => {
115
+describe('POST base64 body auto strategy', (report, done) => {
116
+
117
+  let image = RNTest.prop('image')
118
+  let tmpPath = dirs.DocumentDir + '/tmp-' + Date.now()
119
+
120
+  function upload(desc, method, pBody) {
121
+    let name = `fetch-replacement-${Platform.OS}-${Date.now()}.png`
122
+    return pBody.then((body) =>
123
+      fetch('https://content.dropboxapi.com/2/files/upload', {
124
+        method : method,
125
+        headers : {
126
+          Authorization : `Bearer ${DROPBOX_TOKEN}`,
127
+          'Dropbox-API-Arg': '{\"path\": \"/rn-upload/'+name+'\",\"mode\": \"add\",\"autorename\": true,\"mute\": false}',
128
+          'Content-Type' : 'application/octet-stream'
129
+        },
130
+        body : body
131
+      })
132
+    )
133
+    .then((res) => {
134
+      return res.json()
135
+    })
136
+    .then((info) => {
137
+      report(<Assert key={desc} expect={name} actual={info.name}/>)
138
+    })
139
+  }
140
+
141
+  let tests = [
142
+    upload('upload base64 encoded body', 'post', Promise.resolve(image)),
143
+    upload('upload Blob body', 'post', Blob.build(image, 'image/png;BASE64')),
144
+    upload('upload file path body', 'post', fs.writeFile(tmpPath, image, 'base64').then(() => Promise.resolve(RNFetchBlob.wrap(tmpPath))))
145
+  ]
146
+
147
+  Promise.all(tests).then(() => done())
148
+
72 149
 
73 150
 })