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.FormData = RNFetchBlob.polyfill.FormData const { Assert, Comparer, Info, prop } = RNTest const describe = RNTest.config({ group : 'XMLHttpRequest', run : true, expand : true, 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() function onError(method, code) { let xhr = new XMLHttpRequest() xhr.open(method, `${TEST_SERVER_URL}/xhr-code/${code}`) xhr.onreadystatechange = function() { 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('header-test', '100') xhr.open('GET', `${TEST_SERVER_URL}/xhr-header`) xhr.setRequestHeader('header-test', '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 = ["(", ")", "<", ">", "@", ",", ";", ":", "\\", "\"", "/", "[", "]", "?", "=", "{", "}", " ", /* HT already tested in the loop below */ "\u007f", "", "t\rt", "t\nt", "t: t", "t:t", "t { try { new XMLHttpRequest().setRequestHeader('foo', 'bar') } catch(err) { report() done() } })