11import TKUnit = require( "Tests/TKUnit" ) ;
22import http = require( "http/http" ) ;
3- import http_request = require( "http/http-request" ) ;
43require ( "globals" ) ;
54
5+ // <snippet name="http">
6+ // # Http module
7+ // ``` JavaScript
8+ // var http = require("http");
9+ // ```
10+ // </snippet>
11+
612export var test_getString_isDefined = function ( ) {
713 TKUnit . assert ( typeof ( http . getString ) !== "undefined" , "Method http.getString() should be defined!" ) ;
814} ;
@@ -12,10 +18,18 @@ export var test_getString = function () {
1218 var completed : boolean ;
1319 var isReady = function ( ) { return completed ; }
1420
21+ // <snippet name="http">
22+ // ### Get string from URL
23+ // ``` JavaScript
1524 http . getString ( "http://httpbin.org/get" ) . then ( function ( r ) {
25+ //// Result (r) is string!
26+ // <hide>
1627 completed = true ;
1728 result = r ;
29+ // </hide>
1830 } ) . fail ( function ( e ) { console . log ( e ) ; } ) ;
31+ // ```
32+ // </snippet>
1933
2034 TKUnit . waitUntilReady ( isReady , 3 ) ;
2135 TKUnit . assert ( typeof ( result ) === "string" , "Result from getString() should be string!" ) ;
@@ -26,10 +40,18 @@ export var test_getString_fail = function () {
2640 var completed : boolean ;
2741 var isReady = function ( ) { return completed ; }
2842
43+ // <snippet name="http">
44+ // ### Get string from invalid URL
45+ // ``` JavaScript
2946 http . getString ( "hgfttp://httpbin.org/get" ) . fail ( function ( e ) {
47+ //// Result (e) is Error!
48+ // <hide>
3049 completed = true ;
3150 result = e ;
51+ // </hide>
3252 } ) ;
53+ // ```
54+ // </snippet>
3355
3456 TKUnit . waitUntilReady ( isReady , 3 ) ;
3557 TKUnit . assert ( result instanceof Error , "Result from getString().fail() should be Error!" ) ;
@@ -44,10 +66,18 @@ export var test_getJSON = function () {
4466 var completed : boolean ;
4567 var isReady = function ( ) { return completed ; }
4668
69+ // <snippet name="http">
70+ // ### Get JSON from URL
71+ // ``` JavaScript
4772 http . getJSON ( "http://httpbin.org/get" ) . then ( function ( r ) {
73+ //// Result (r) is JSON!
74+ // <hide>
4875 completed = true ;
4976 result = r ;
77+ // </hide>
5078 } ) . fail ( function ( e ) { console . log ( e ) ; } ) ;
79+ // ```
80+ // </snippet>
5181
5282 TKUnit . waitUntilReady ( isReady , 3 ) ;
5383 TKUnit . assert ( typeof ( JSON . stringify ( result ) ) === "string" , "Result from getJSON() should be valid JSON object!" ) ;
@@ -58,10 +88,18 @@ export var test_getJSON_fail = function () {
5888 var completed : boolean ;
5989 var isReady = function ( ) { return completed ; }
6090
91+ // <snippet name="http">
92+ // ### Get JSON from invalid URL
93+ // ``` JavaScript
6194 http . getJSON ( "hgfttp://httpbin.org/get" ) . fail ( function ( e ) {
95+ //// Result (e) is Error!
96+ // <hide>
6297 completed = true ;
6398 result = e ;
99+ // </hide>
64100 } ) ;
101+ // ```
102+ // </snippet>
65103
66104 TKUnit . waitUntilReady ( isReady , 3 ) ;
67105 TKUnit . assert ( result instanceof Error , "Result from getJSON().fail() should be Error!" ) ;
@@ -76,10 +114,18 @@ export var test_getImage = function () {
76114 var completed : boolean ;
77115 var isReady = function ( ) { return completed ; }
78116
117+ // <snippet name="http">
118+ // ### Get Image from URL
119+ // ``` JavaScript
79120 http . getImage ( "http://www.google.com/images/errors/logo_sm_2.png" ) . then ( function ( r ) {
121+ //// Result (r) is Image!
122+ // <hide>
80123 completed = true ;
81124 result = r ;
125+ // </hide>
82126 } ) ;
127+ // ```
128+ // </snippet>
83129
84130 TKUnit . waitUntilReady ( isReady , 3 ) ;
85131 TKUnit . assert ( result instanceof require ( "image" ) . Image , "Result from getImage() should be valid Image object!" ) ;
@@ -90,10 +136,18 @@ export var test_getImage_fail = function () {
90136 var completed : boolean ;
91137 var isReady = function ( ) { return completed ; }
92138
139+ // <snippet name="http">
140+ // ### Get Image from URL
141+ // ``` JavaScript
93142 http . getImage ( "htadvtp://www.google.com/images/errors/logo_sm_2.pngm" ) . fail ( function ( e ) {
143+ //// Result (e) is Error!
144+ // <hide>
94145 completed = true ;
95146 result = e ;
147+ // </hide>
96148 } ) ;
149+ // ```
150+ // </snippet>
97151
98152 TKUnit . waitUntilReady ( isReady , 3 ) ;
99153 TKUnit . assert ( result instanceof Error , "Result from getImage().fail() should be Error!" ) ;
@@ -108,7 +162,7 @@ export var test_request_shouldFailIfOptionsUrlIsNotDefined = function () {
108162 var completed : boolean ;
109163 var isReady = function ( ) { return completed ; }
110164
111- http_request . request ( { url : undefined , method : undefined } ) . fail ( function ( e ) {
165+ http . request ( { url : undefined , method : undefined } ) . fail ( function ( e ) {
112166 completed = true ;
113167 result = e ;
114168 } ) ;
@@ -118,35 +172,70 @@ export var test_request_shouldFailIfOptionsUrlIsNotDefined = function () {
118172} ;
119173
120174export var test_request_responseStatusCodeShouldBeDefined = function ( ) {
121- var result : http_request . HttpResponse ;
175+ var result : http . HttpResponse ;
122176 var completed : boolean ;
123177 var isReady = function ( ) { return completed ; }
124178
125- http_request . request ( { url : "http://httpbin.org/get" , method : "GET" } ) . then ( function ( response ) {
179+ // <snippet name="http">
180+ // ### Get response status code
181+ // ``` JavaScript
182+ http . request ( { url : "http://httpbin.org/get" , method : "GET" } ) . then ( function ( response ) {
183+ //// Result (response) is require("http/http-request").HttpResponse!
184+ //// You can get status code using response.statusCode (number)!
185+ // <hide>
126186 completed = true ;
127187 result = response ;
188+ // </hide>
128189 } ) ;
190+ // ```
191+ // </snippet>
129192
130193 TKUnit . waitUntilReady ( isReady , 3 ) ;
131194 TKUnit . assert ( typeof ( result . statusCode ) !== "undefined" , "response.statusCode should be defined!" ) ;
132195} ;
133196
134197export var test_request_responseHeadersShouldBeDefined = function ( ) {
135- var result : http_request . HttpResponse ;
198+ var result : http . HttpResponse ;
136199 var completed : boolean ;
137200 var isReady = function ( ) { return completed ; }
138201
139- http_request . request ( { url : "http://httpbin.org/get" , method : "GET" } ) . then ( function ( response ) {
202+ // <snippet name="http">
203+ // ### Get response headers
204+ // ``` JavaScript
205+ http . request ( { url : "http://httpbin.org/get" , method : "GET" } ) . then ( function ( response ) {
206+ //// Result (response) is require("http/http-request").HttpResponse!
207+ //// You can get response headers using response.headers (JSON)!
208+ // <hide>
140209 completed = true ;
141210 result = response ;
211+ // </hide>
142212 } ) ;
213+ // ```
214+ // </snippet>
143215
144216 TKUnit . waitUntilReady ( isReady , 3 ) ;
145217 TKUnit . assert ( typeof ( result . headers ) !== "undefined" , "response.headers should be defined!" ) ;
146218} ;
147219
148220export var test_request_responseContentShouldBeDefined = function ( ) {
149- http_request . request ( { url : "http://httpbin.org/get" , method : "GET" } ) . then ( function ( response ) {
150- TKUnit . assert ( typeof ( response . content ) !== "undefined" , "response.content should be defined!" ) ;
221+ var result : http . HttpResponse ;
222+ var completed : boolean ;
223+ var isReady = function ( ) { return completed ; }
224+
225+ // <snippet name="http">
226+ // ### Get response content
227+ // ``` JavaScript
228+ http . request ( { url : "http://httpbin.org/get" , method : "GET" } ) . then ( function ( response ) {
229+ //// Result (response) is require("http/http-request").HttpContent!
230+ //// You can get response content using response.content methods: toString(), toJSON and toImage()!
231+ // <hide>
232+ completed = true ;
233+ result = response ;
234+ // </hide>
151235 } ) ;
236+ // ```
237+ // </snippet>
238+
239+ TKUnit . waitUntilReady ( isReady , 3 ) ;
240+ TKUnit . assert ( typeof ( result . content ) !== "undefined" , "response.content should be defined!" ) ;
152241} ;
0 commit comments