11/* tslint:disable:no-unused-variable */
22import TKUnit = require( "./TKUnit" ) ;
3- import fetchModule = require( "fetch" ) ;
43import types = require( "utils/types" ) ;
54
65// <snippet module="fetch" title="fetch">
@@ -12,18 +11,18 @@ import types = require("utils/types");
1211// </snippet>
1312
1413export var test_fetch_defined = function ( ) {
15- TKUnit . assert ( types . isDefined ( ( fetchModule . fetch ) ) , "Method fetch() should be defined!" ) ;
14+ TKUnit . assert ( types . isDefined ( ( fetch ) ) , "Method fetch() should be defined!" ) ;
1615} ;
1716
1817export var test_fetch = function ( done : ( err : Error , res ?: string ) => void ) {
1918 var result ;
2019 // <snippet module="fetch" title="fetch">
2120 // ### Get Response from URL
2221 // ``` JavaScript
23- fetchModule . fetch ( "https://httpbin.org/get" ) . then ( function ( r ) {
22+ fetch ( "https://httpbin.org/get" ) . then ( function ( r ) {
2423 //// Argument (r) is Response!
2524 // <hide>
26- TKUnit . assert ( r instanceof fetchModule . Response , "Result from fetch() should be valid Response object! Actual result is: " + result ) ;
25+ TKUnit . assert ( r instanceof Response , "Result from fetch() should be valid Response object! Actual result is: " + result ) ;
2726 done ( null ) ;
2827 // </hide>
2928 } , function ( e ) {
@@ -42,7 +41,7 @@ export var test_fetch_text = function (done: (err: Error, res?: string) => void)
4241 // <snippet module="fetch" title="fetch">
4342 // ### Get string from URL
4443 // ``` JavaScript
45- fetchModule . fetch ( "https://httpbin.org/get" ) . then ( response => { return response . text ( ) ; } ) . then ( function ( r ) {
44+ fetch ( "https://httpbin.org/get" ) . then ( response => { return response . text ( ) ; } ) . then ( function ( r ) {
4645 //// Argument (r) is string!
4746 // <hide>
4847 TKUnit . assert ( types . isString ( r ) , "Result from text() should be string! Actual result is: " + r ) ;
@@ -64,7 +63,7 @@ export var test_fetch_json = function (done: (err: Error, res?: string) => void)
6463 // <snippet module="fetch" title="fetch">
6564 // ### Get JSON from URL
6665 // ``` JavaScript
67- fetchModule . fetch ( "https://httpbin.org/get" ) . then ( response => { return response . json ( ) ; } ) . then ( function ( r ) {
66+ fetch ( "https://httpbin.org/get" ) . then ( response => { return response . json ( ) ; } ) . then ( function ( r ) {
6867 //// Argument (r) is JSON object!
6968 // <hide>
7069 TKUnit . assert ( types . isString ( JSON . stringify ( r ) ) , "Result from json() should be JSON object! Actual result is: " + r ) ;
@@ -86,7 +85,7 @@ export var test_fetch_blob = function (done: (err: Error, res?: string) => void)
8685 // <snippet module="fetch" title="fetch">
8786 // ### Get Blob from URL
8887 // ``` JavaScript
89- fetchModule. fetch("https://httpbin.org/get").then(response => { return response.blob(); }).then(function (r) {
88+ fetch("https://httpbin.org/get").then(response => { return response.blob(); }).then(function (r) {
9089 //// Argument (r) is Blob object!
9190 // <hide>
9291 TKUnit.assert(r instanceof Blob, "Result from blob() should be Blob object! Actual result is: " + r);
@@ -108,7 +107,7 @@ export var test_fetch_arrayBuffer = function (done: (err: Error, res?: string) =
108107 // <snippet module="fetch" title="fetch">
109108 // ### Get ArrayBuffer from URL
110109 // ``` JavaScript
111- fetchModule. fetch("https://httpbin.org/get").then(response => { return response.arrayBuffer(); }).then(function (r) {
110+ fetch("https://httpbin.org/get").then(response => { return response.arrayBuffer(); }).then(function (r) {
112111 //// Argument (r) is ArrayBuffer object!
113112 // <hide>
114113 TKUnit.assert(r instanceof ArrayBuffer, "Result from arrayBuffer() should be ArrayBuffer object! Actual result is: " + r);
@@ -123,14 +122,15 @@ export var test_fetch_arrayBuffer = function (done: (err: Error, res?: string) =
123122 // ```
124123 // </snippet>
125124};
125+ */
126126
127127export var test_fetch_formData = function ( done : ( err : Error , res ?: string ) => void ) {
128128 var result ;
129129
130130 // <snippet module="fetch" title="fetch">
131131 // ### Get FormData from URL
132132 // ``` JavaScript
133- fetchModule. fetch("https://httpbin.org/get").then(response => { return response.formData(); }).then(function (r) {
133+ fetch ( "https://httpbin.org/get" ) . then ( response => { return response . formData ( ) ; } ) . then ( function ( r ) {
134134 //// Argument (r) is FormData object!
135135 // <hide>
136136 TKUnit . assert ( r instanceof FormData , "Result from formData() should be FormData object! Actual result is: " + r ) ;
@@ -145,12 +145,12 @@ export var test_fetch_formData = function (done: (err: Error, res?: string) => v
145145 // ```
146146 // </snippet>
147147} ;
148- */
148+
149149export var test_fetch_fail_invalid_url = function ( done ) {
150150 var completed : boolean ;
151151 var isReady = function ( ) { return completed ; }
152152
153- fetchModule . fetch ( "hgfttp://httpbin.org/get" ) . catch ( function ( e ) {
153+ fetch ( "hgfttp://httpbin.org/get" ) . catch ( function ( e ) {
154154 completed = true ;
155155 done ( null )
156156 } ) ;
@@ -161,7 +161,7 @@ export var test_fetch_response_status = function (done) {
161161 // <snippet module="fetch" title="fetch">
162162 // ### Get Response status
163163 // ``` fetch
164- fetchModule . fetch ( "https://httpbin.org/get" ) . then ( function ( response ) {
164+ fetch ( "https://httpbin.org/get" ) . then ( function ( response ) {
165165 //// Argument (response) is Response!
166166 var statusCode = response . status ;
167167 // <hide>
@@ -188,7 +188,7 @@ export var test_fetch_response_headers = function (done) {
188188 // <snippet module="fetch" title="fetch">
189189 // ### Get response headers
190190 // ``` JavaScript
191- fetchModule . fetch ( "https://httpbin.org/get" ) . then ( function ( response ) {
191+ fetch ( "https://httpbin.org/get" ) . then ( function ( response ) {
192192 //// Argument (response) is Response!
193193 // var all = response.headers.getAll();
194194 // <hide>
@@ -211,9 +211,9 @@ export var test_fetch_response_headers = function (done) {
211211} ;
212212
213213export var test_fetch_headers_sent = function ( done ) {
214- var result : fetchModule . Headers ;
214+ var result : Headers ;
215215
216- fetchModule . fetch ( "https://httpbin.org/get" , {
216+ fetch ( "https://httpbin.org/get" , {
217217 method : "GET" ,
218218 headers : { "Content-Type" : "application/json" }
219219 } ) . then ( function ( response ) {
@@ -231,16 +231,19 @@ export var test_fetch_headers_sent = function (done) {
231231} ;
232232
233233export var test_fetch_post_form_data = function ( done ) {
234- fetchModule . fetch ( "https://httpbin.org/post" , {
234+ var data = new FormData ( ) ;
235+ data . append ( "MyVariableOne" , "ValueOne" ) ;
236+ data . append ( "MyVariableTwo" , "ValueTwo" ) ;
237+
238+ fetch ( "https://httpbin.org/post" , {
235239 method : "POST" ,
236240 headers : { "Content-Type" : "application/x-www-form-urlencoded" } ,
237- body : "MyVariableOne=ValueOne&MyVariableTwo=ValueTwo"
241+ body : data
238242 } ) . then ( r => {
239- // return r.formData(); Uncomment this when FormData is available!
240- return r . json ( ) ;
243+ return r . formData ( ) ;
241244 } ) . then ( function ( r ) {
242245 try {
243- TKUnit . assert ( r . form [ "MyVariableOne" ] === "ValueOne" && r . form [ "MyVariableTwo" ] === "ValueTwo" , "Content not sent/received properly! Actual result is: " + r . form ) ;
246+ TKUnit . assert ( r instanceof FormData , "Content not sent/received properly! Actual result is: " + r ) ;
244247 done ( null ) ;
245248 }
246249 catch ( err ) {
@@ -255,7 +258,7 @@ export var test_fetch_post_json = function (done) {
255258 // <snippet module="fetch" title="fetch">
256259 // ### Post JSON
257260 // ``` JavaScript
258- fetchModule . fetch ( "https://httpbin.org/post" , {
261+ fetch ( "https://httpbin.org/post" , {
259262 method : "POST" ,
260263 headers : { "Content-Type" : "application/json" } ,
261264 body : JSON . stringify ( { MyVariableOne : "ValueOne" , MyVariableTwo : "ValueTwo" } )
0 commit comments