import RNTest from './react-native-testkit/'
import React from 'react'
import RNFetchBlob from 'react-native-fetch-blob'
import Timer from 'react-timer-mixin'
import {
StyleSheet,
Text,
View,
ScrollView,
CameraRoll,
Platform,
Dimensions,
Image,
} from 'react-native';
const fs = RNFetchBlob.fs
const Blob = RNFetchBlob.polyfill.Blob
window.XMLHttpRequest = RNFetchBlob.polyfill.XMLHttpRequest
window.Blob = Blob
window.ProgressEvent = RNFetchBlob.polyfill.ProgressEvent
const { Assert, Comparer, Info, prop } = RNTest
const describe = RNTest.config({
group : 'XMLHttpRequest',
run : true,
expand : false,
timeout : 20000,
})
const { TEST_SERVER_URL, TEST_SERVER_URL_SSL, DROPBOX_TOKEN, styles } = prop()
const dirs = RNFetchBlob.fs.dirs
let prefix = ((Platform.OS === 'android') ? 'file://' : '')
let file = RNTest.prop('image')
/**
* Test cases are based on W3C github repository.
* {@link https://github.com/w3c/web-platform-tests/blob/master/XMLHttpRequest/}
*/
describe('unsent state test', (report, done) => {
let xhr = new XMLHttpRequest()
try {
xhr.setRequestHeader('x-test', 'test')
} catch(err) {
report(
)
}
try {
let xhr = new XMLHttpRequest()
xhr.send(null)
} catch(err) {
report(
)
}
try {
report(
,
,
,
,
)
done()
} catch(err) {
console.log(err.stack)
}
})
describe('HTTP error should not throw error event', (report, done) => {
onError('GET', 200)
onError('GET', 400)
onError('GET', 401)
onError('GET', 404)
onError('GET', 410)
onError('GET', 500)
onError('GET', 699)
onError('HEAD', 200)
onError('HEAD', 404)
onError('HEAD', 500)
onError('HEAD', 699)
onError('POST', 200)
onError('POST', 404)
onError('POST', 500)
onError('POST', 699)
onError('PUT', 200)
onError('PUT', 404)
onError('PUT', 500)
onError('PUT', 699)
done()
let count = 0
function onError(method, code) {
let xhr = new XMLHttpRequest()
xhr.open(method, `${TEST_SERVER_URL}/xhr-code/${code}`)
xhr.onreadystatechange = function() {
count++
report(
,
)
}
xhr.onerror = function() {
report(
)
}
xhr.send()
}
})
describe('request headers records should be cleared by open()', (report, done) => {
let xhr = new XMLHttpRequest()
xhr.open('GET', `${TEST_SERVER_URL}/xhr-header`)
xhr.setRequestHeader('value', '100')
xhr.open('GET', `${TEST_SERVER_URL}/xhr-header`)
xhr.setRequestHeader('value', '200')
xhr.send()
xhr.onreadystatechange = function() {
if(this.readyState == 4) {
report()
done()
}
}
})
/**
* {@link https://github.com/w3c/web-platform-tests/blob/master/XMLHttpRequest/setrequestheader-bogus-name.htm}
*/
describe('invalid characters should not exists in header field', (report, done) => {
function try_name(name) {
try {
let client = new XMLHttpRequest()
client.open("GET", `${TEST_SERVER_URL}/public/github.png`)
client.setRequestHeader(name, '123')
} catch(err) {
report(
)
}
}
function try_byte_string(name) {
try {
let client = new XMLHttpRequest()
client.open("GET", `${TEST_SERVER_URL}/public/github.png`)
client.setRequestHeader(name, '123')
} catch(err) {
report(
)
}
}
var invalid_headers = ["(", ")", "<", ">", "@", ",", ";", ":", "\\",
"\"", "/", "[", "]", "?", "=", "{", "}", " ",
"\u007f", "", "t\rt", "t\nt", "t: t", "t:t",
"t {
try {
let xhr = new XMLHttpRequest()
xhr.setRequestHeader('foo', 'bar')
} catch(err) {
report(
{err}
,
)
done()
}
})
describe('upload progress event test', (report, done) => {
let xhr = new XMLHttpRequest()
let time = Date.now()
let msg = `time=${time}`
xhr.upload.onprogress = function(e) {
report(
)
}
xhr.onreadystatechange = function() {
if(this.readyState == XMLHttpRequest.DONE) {
report(
,
)
done()
}
}
xhr.open('POST', `${TEST_SERVER_URL}/upload`)
xhr.overrideMimeType('application/x-www-form-urlencoded')
xhr.send(msg)
})
describe('timeout event catchable', (report, done) => {
let xhr = new XMLHttpRequest()
let count = 0
xhr.timeout = 1
xhr.ontimeout = function() {
report(
{count}
,
)
done()
}
xhr.open('GET', `${TEST_SERVER_URL}/timeout/`)
xhr.send()
})
describe('upload progress event should not be triggered when body is empty', (report, done) => {
let xhr = new XMLHttpRequest()
let count = 0
xhr.upload.onloadstart = function() {
report(
)
}
xhr.upload.onprogress = function() {
report(
)
}
xhr.onreadystatechange = function() {
if(this.readyState == XMLHttpRequest.DONE) {
count++
report(
,
)
done()
}
}
xhr.open('GET', `${TEST_SERVER_URL}/pulbic/github.png`)
xhr.send()
})