1+ /* tslint:disable:no-unused-variable */
2+ import TKUnit = require( "./TKUnit" ) ;
3+ import fetchModule = require( "fetch" ) ;
4+ import types = require( "utils/types" ) ;
5+
6+ // <snippet module="fetch" title="fetch">
7+ // # Fetch module
8+ // Using fetch methods requires to load "fetch" module.
9+ // ``` JavaScript
10+ // var fetch = require("fetch");
11+ // ```
12+ // </snippet>
13+
14+ export var test_fetch_defined = function ( ) {
15+ TKUnit . assert ( types . isDefined ( ( fetchModule . fetch ) ) , "Method fetch() should be defined!" ) ;
16+ } ;
17+
18+ export var test_fetch = function ( done : ( err : Error , res ?: string ) => void ) {
19+ var result ;
20+ // <snippet module="fetch" title="fetch">
21+ // ### Get Response from URL
22+ // ``` JavaScript
23+ fetchModule . fetch ( "https://httpbin.org/get" ) . then ( function ( r ) {
24+ //// Argument (r) is Response!
25+ // <hide>
26+ TKUnit . assert ( r instanceof fetchModule . Response , "Result from fetch() should be valid Response object! Actual result is: " + result ) ;
27+ done ( null ) ;
28+ // </hide>
29+ } , function ( e ) {
30+ //// Argument (e) is Error!
31+ // <hide>
32+ done ( e ) ;
33+ // </hide>
34+ } ) ;
35+ // ```
36+ // </snippet>
37+ } ;
38+
39+ export var test_fetch_text = function ( done : ( err : Error , res ?: string ) => void ) {
40+ var result ;
41+
42+ // <snippet module="fetch" title="fetch">
43+ // ### Get string from URL
44+ // ``` JavaScript
45+ fetchModule . fetch ( "https://httpbin.org/get" ) . then ( response => { return response . text ( ) ; } ) . then ( function ( r ) {
46+ //// Argument (r) is string!
47+ // <hide>
48+ TKUnit . assert ( types . isString ( r ) , "Result from text() should be string! Actual result is: " + r ) ;
49+ done ( null ) ;
50+ // </hide>
51+ } , function ( e ) {
52+ //// Argument (e) is Error!
53+ // <hide>
54+ done ( e ) ;
55+ // </hide>
56+ } ) ;
57+ // ```
58+ // </snippet>
59+ } ;
60+
61+ export var test_fetch_json = function ( done : ( err : Error , res ?: string ) => void ) {
62+ var result ;
63+
64+ // <snippet module="fetch" title="fetch">
65+ // ### Get JSON from URL
66+ // ``` JavaScript
67+ fetchModule . fetch ( "https://httpbin.org/get" ) . then ( response => { return response . json ( ) ; } ) . then ( function ( r ) {
68+ //// Argument (r) is JSON object!
69+ // <hide>
70+ TKUnit . assert ( types . isString ( JSON . stringify ( r ) ) , "Result from json() should be JSON object! Actual result is: " + r ) ;
71+ done ( null ) ;
72+ // </hide>
73+ } , function ( e ) {
74+ //// Argument (e) is Error!
75+ // <hide>
76+ done ( e ) ;
77+ // </hide>
78+ } ) ;
79+ // ```
80+ // </snippet>
81+ } ;
82+ /*
83+ export var test_fetch_blob = function (done: (err: Error, res?: string) => void) {
84+ var result;
85+
86+ // <snippet module="fetch" title="fetch">
87+ // ### Get Blob from URL
88+ // ``` JavaScript
89+ fetchModule.fetch("https://httpbin.org/get").then(response => { return response.blob(); }).then(function (r) {
90+ //// Argument (r) is Blob object!
91+ // <hide>
92+ TKUnit.assert(r instanceof Blob, "Result from blob() should be Blob object! Actual result is: " + r);
93+ done(null);
94+ // </hide>
95+ }, function (e) {
96+ //// Argument (e) is Error!
97+ // <hide>
98+ done(e);
99+ // </hide>
100+ });
101+ // ```
102+ // </snippet>
103+ };
104+
105+ export var test_fetch_arrayBuffer = function (done: (err: Error, res?: string) => void) {
106+ var result;
107+
108+ // <snippet module="fetch" title="fetch">
109+ // ### Get ArrayBuffer from URL
110+ // ``` JavaScript
111+ fetchModule.fetch("https://httpbin.org/get").then(response => { return response.arrayBuffer(); }).then(function (r) {
112+ //// Argument (r) is ArrayBuffer object!
113+ // <hide>
114+ TKUnit.assert(r instanceof ArrayBuffer, "Result from arrayBuffer() should be ArrayBuffer object! Actual result is: " + r);
115+ done(null);
116+ // </hide>
117+ }, function (e) {
118+ //// Argument (e) is Error!
119+ // <hide>
120+ done(e);
121+ // </hide>
122+ });
123+ // ```
124+ // </snippet>
125+ };
126+
127+ export var test_fetch_formData = function (done: (err: Error, res?: string) => void) {
128+ var result;
129+
130+ // <snippet module="fetch" title="fetch">
131+ // ### Get FormData from URL
132+ // ``` JavaScript
133+ fetchModule.fetch("https://httpbin.org/get").then(response => { return response.formData(); }).then(function (r) {
134+ //// Argument (r) is FormData object!
135+ // <hide>
136+ TKUnit.assert(r instanceof FormData, "Result from formData() should be FormData object! Actual result is: " + r);
137+ done(null);
138+ // </hide>
139+ }, function (e) {
140+ //// Argument (e) is Error!
141+ // <hide>
142+ done(e);
143+ // </hide>
144+ });
145+ // ```
146+ // </snippet>
147+ };
148+ */
149+ export var test_fetch_fail_invalid_url = function ( done ) {
150+ var completed : boolean ;
151+ var isReady = function ( ) { return completed ; }
152+
153+ fetchModule . fetch ( "hgfttp://httpbin.org/get" ) . catch ( function ( e ) {
154+ completed = true ;
155+ done ( null )
156+ } ) ;
157+ } ;
158+
159+ export var test_fetch_response_status = function ( done ) {
160+
161+ // <snippet module="fetch" title="fetch">
162+ // ### Get Response status
163+ // ``` fetch
164+ fetchModule . fetch ( "https://httpbin.org/get" ) . then ( function ( response ) {
165+ //// Argument (response) is Response!
166+ var statusCode = response . status ;
167+ // <hide>
168+ try {
169+ TKUnit . assert ( types . isDefined ( statusCode ) , "response.status should be defined! Actual result is: " + statusCode ) ;
170+ done ( null ) ;
171+ }
172+ catch ( err ) {
173+ done ( err ) ;
174+ }
175+ // </hide>
176+ } , function ( e ) {
177+ //// Argument (e) is Error!
178+ // <hide>
179+ done ( e ) ;
180+ // </hide>
181+ } ) ;
182+ // ```
183+ // </snippet>
184+ } ;
185+
186+ export var test_fetch_response_headers = function ( done ) {
187+
188+ // <snippet module="fetch" title="fetch">
189+ // ### Get response headers
190+ // ``` JavaScript
191+ fetchModule . fetch ( "https://httpbin.org/get" ) . then ( function ( response ) {
192+ //// Argument (response) is Response!
193+ // var all = response.headers.getAll();
194+ // <hide>
195+ try {
196+ TKUnit . assert ( types . isDefined ( response . headers ) , "response.headers should be defined! Actual result is: " + response . headers ) ;
197+ done ( null ) ;
198+ }
199+ catch ( err ) {
200+ done ( err ) ;
201+ }
202+ // </hide>
203+ } , function ( e ) {
204+ //// Argument (e) is Error!
205+ // <hide>
206+ done ( e ) ;
207+ // </hide>
208+ } ) ;
209+ // ```
210+ // </snippet>
211+ } ;
212+
213+ export var test_fetch_headers_sent = function ( done ) {
214+ var result : fetchModule . Headers ;
215+
216+ fetchModule . fetch ( "https://httpbin.org/get" , {
217+ method : "GET" ,
218+ headers : { "Content-Type" : "application/json" }
219+ } ) . then ( function ( response ) {
220+ result = response . headers ;
221+ try {
222+ TKUnit . assert ( result . get ( "Content-Type" ) === "application/json" , "Headers not sent/received properly! Actual result is: " + result ) ;
223+ done ( null ) ;
224+ }
225+ catch ( err ) {
226+ done ( err ) ;
227+ }
228+ } , function ( e ) {
229+ done ( e ) ;
230+ } ) ;
231+ } ;
232+
233+ export var test_fetch_post_form_data = function ( done ) {
234+ fetchModule . fetch ( "https://httpbin.org/post" , {
235+ method : "POST" ,
236+ headers : { "Content-Type" : "application/x-www-form-urlencoded" } ,
237+ body : "MyVariableOne=ValueOne&MyVariableTwo=ValueTwo"
238+ } ) . then ( r => {
239+ // return r.formData(); Uncomment this when FormData is available!
240+ return r . json ( ) ;
241+ } ) . then ( function ( r ) {
242+ try {
243+ TKUnit . assert ( r . form [ "MyVariableOne" ] === "ValueOne" && r . form [ "MyVariableTwo" ] === "ValueTwo" , "Content not sent/received properly! Actual result is: " + r . form ) ;
244+ done ( null ) ;
245+ }
246+ catch ( err ) {
247+ done ( err ) ;
248+ }
249+ } , function ( e ) {
250+ done ( e ) ;
251+ } ) ;
252+ } ;
253+
254+ export var test_fetch_post_json = function ( done ) {
255+ // <snippet module="fetch" title="fetch">
256+ // ### Post JSON
257+ // ``` JavaScript
258+ fetchModule . fetch ( "https://httpbin.org/post" , {
259+ method : "POST" ,
260+ headers : { "Content-Type" : "application/json" } ,
261+ body : JSON . stringify ( { MyVariableOne : "ValueOne" , MyVariableTwo : "ValueTwo" } )
262+ } ) . then ( r => { return r . json ( ) ; } ) . then ( function ( r ) {
263+ // <hide>
264+ try {
265+ TKUnit . assert ( r . json [ "MyVariableOne" ] === "ValueOne" && r . json [ "MyVariableTwo" ] === "ValueTwo" , "Content not sent/received properly! Actual result is: " + r . json ) ;
266+ done ( null ) ;
267+ }
268+ catch ( err ) {
269+ done ( err ) ;
270+ }
271+ // </hide>
272+ // console.log(result);
273+ } , function ( e ) {
274+ // <hide>
275+ done ( e ) ;
276+ // </hide>
277+ // console.log("Error occurred " + e);
278+ } ) ;
279+ // ```
280+ // </snippet>
281+ } ;
0 commit comments