-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
176 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* eslint-disable */ | ||
import Taro from '../index.js' | ||
Taro.initNativeApi(Taro) | ||
|
||
global.fetch = require('jest-fetch-mock') | ||
|
||
describe('request', () => { | ||
beforeEach(() => { | ||
fetch.resetMocks() | ||
}) | ||
|
||
test('should return fetch data', () => { | ||
fetch.once(JSON.stringify({ data: '12345' })) | ||
|
||
return Taro.request({ | ||
url: 'https://github.com', | ||
data: { | ||
x: 123, | ||
y: 'abc', | ||
z: [1,2,3] | ||
} | ||
}) | ||
.then(res => { | ||
expect(fetch.mock.calls[0][0]).toBe('https://github.com?x=123&y=abc&z=1%2C2%2C3') | ||
expect(res.statusCode).toBe(200) | ||
expect(res.data).toEqual({ | ||
data: '12345' | ||
}) | ||
}) | ||
}) | ||
|
||
test('should return fetch data when options is url string', () => { | ||
fetch.once(JSON.stringify({ data: '12345' })) | ||
|
||
return Taro.request('https://github.com') | ||
.then(res => { | ||
expect(fetch.mock.calls[0][0]).toBe('https://github.com?') | ||
expect(res.statusCode).toBe(200) | ||
expect(res.data).toEqual({ | ||
data: '12345' | ||
}) | ||
}) | ||
}) | ||
|
||
test('should set correct params', () => { | ||
fetch.once(JSON.stringify({ data: '12345' }), { status: 201 }) | ||
|
||
return Taro.request({ | ||
url: 'https://github.com', | ||
method: 'POST', | ||
data: { | ||
arg: 123 | ||
}, | ||
header: { | ||
'A': 'CCC' | ||
}, | ||
mode: 'cors', | ||
cache: 'no-cache', | ||
credentials: 'include' | ||
}) | ||
.then(res => { | ||
expect(fetch.mock.calls[0][0]).toBe('https://github.com') | ||
expect(fetch.mock.calls[0][1]).toEqual({ | ||
method: 'POST', | ||
body: { | ||
arg: 123 | ||
}, | ||
headers: { | ||
'A': 'CCC' | ||
}, | ||
mode: 'cors', | ||
cache: 'no-cache', | ||
credentials: 'include' | ||
}) | ||
expect(res.statusCode).toBe(201) | ||
expect(res.data).toEqual({ | ||
data: '12345' | ||
}) | ||
}) | ||
}) | ||
|
||
test('should catch error', () => { | ||
fetch.mockReject(new Error('fake error message')) | ||
|
||
return Taro.request('https://github.com') | ||
.catch(err => { | ||
expect(err.message).toBe('fake error message') | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import jsonpRetry from 'jsonp-retry' | ||
import { serializeParams } from '../utils' | ||
|
||
function generateRequestUrlWithParams (url, params) { | ||
params = typeof params === 'string' ? params : serializeParams(params) | ||
url += (~url.indexOf('?') ? '&' : '?') + `${params}` | ||
url = url.replace('?&', '?') | ||
return url | ||
} | ||
|
||
export default function request (options) { | ||
options = options || {} | ||
if (typeof options === 'string') { | ||
options = { | ||
url: options | ||
} | ||
} | ||
let url = options.url | ||
const params = {} | ||
const res = {} | ||
if (options.jsonp) { | ||
params.params = options.data | ||
params.cache = options.jsonpCache | ||
if (typeof options.jsonp === 'string') { | ||
params.name = options.jsonp | ||
} | ||
return jsonpRetry(url, params) | ||
.then(data => { | ||
res.statusCode = 200 | ||
res.data = data | ||
return res | ||
}) | ||
} | ||
params.method = options.method || 'GET' | ||
const methodUpper = params.method.toUpperCase() | ||
params.cache = options.cache || 'default' | ||
if (methodUpper === 'GET' || methodUpper === 'HEAD') { | ||
url = generateRequestUrlWithParams(url, options.data) | ||
} else { | ||
params.body = options.data | ||
} | ||
if (options.header) { | ||
params.headers = options.header | ||
} | ||
if (options.mode) { | ||
params.mode = options.mode | ||
} | ||
params.credentials = options.credentials | ||
return fetch(url, params) | ||
.then(response => { | ||
res.statusCode = response.status | ||
res.header = {} | ||
response.headers.forEach((val, key) => { | ||
res.header[key] = val | ||
}) | ||
if (options.responseType === 'arraybuffer') { | ||
return response.arrayBuffer() | ||
} | ||
if (options.dataType === 'json' || typeof options.dataType === 'undefined') { | ||
return response.json() | ||
} | ||
if (options.responseType === 'text') { | ||
return response.text() | ||
} | ||
return Promise.resolve(null) | ||
}) | ||
.then(data => { | ||
res.data = data | ||
return res | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters