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