mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-16 11:42:04 +08:00
http module improved
This commit is contained in:
@ -1,8 +1,14 @@
|
||||
import TKUnit = require("Tests/TKUnit");
|
||||
import http = require("http/http");
|
||||
import http_request = require("http/http-request");
|
||||
require("globals");
|
||||
|
||||
// <snippet name="http">
|
||||
// # Http module
|
||||
// ``` JavaScript
|
||||
// var http = require("http");
|
||||
// ```
|
||||
// </snippet>
|
||||
|
||||
export var test_getString_isDefined = function () {
|
||||
TKUnit.assert(typeof (http.getString) !== "undefined", "Method http.getString() should be defined!");
|
||||
};
|
||||
@ -12,10 +18,18 @@ export var test_getString = function () {
|
||||
var completed: boolean;
|
||||
var isReady = function () { return completed; }
|
||||
|
||||
// <snippet name="http">
|
||||
// ### Get string from URL
|
||||
// ``` JavaScript
|
||||
http.getString("http://httpbin.org/get").then(function (r) {
|
||||
//// Result (r) is string!
|
||||
// <hide>
|
||||
completed = true;
|
||||
result = r;
|
||||
// </hide>
|
||||
}).fail(function (e) { console.log(e); });
|
||||
// ```
|
||||
// </snippet>
|
||||
|
||||
TKUnit.waitUntilReady(isReady, 3);
|
||||
TKUnit.assert(typeof (result) === "string", "Result from getString() should be string!");
|
||||
@ -26,10 +40,18 @@ export var test_getString_fail = function () {
|
||||
var completed: boolean;
|
||||
var isReady = function () { return completed; }
|
||||
|
||||
// <snippet name="http">
|
||||
// ### Get string from invalid URL
|
||||
// ``` JavaScript
|
||||
http.getString("hgfttp://httpbin.org/get").fail(function (e) {
|
||||
//// Result (e) is Error!
|
||||
// <hide>
|
||||
completed = true;
|
||||
result = e;
|
||||
// </hide>
|
||||
});
|
||||
// ```
|
||||
// </snippet>
|
||||
|
||||
TKUnit.waitUntilReady(isReady, 3);
|
||||
TKUnit.assert(result instanceof Error, "Result from getString().fail() should be Error!");
|
||||
@ -44,10 +66,18 @@ export var test_getJSON = function () {
|
||||
var completed: boolean;
|
||||
var isReady = function () { return completed; }
|
||||
|
||||
// <snippet name="http">
|
||||
// ### Get JSON from URL
|
||||
// ``` JavaScript
|
||||
http.getJSON("http://httpbin.org/get").then(function (r) {
|
||||
//// Result (r) is JSON!
|
||||
// <hide>
|
||||
completed = true;
|
||||
result = r;
|
||||
// </hide>
|
||||
}).fail(function (e) { console.log(e); });
|
||||
// ```
|
||||
// </snippet>
|
||||
|
||||
TKUnit.waitUntilReady(isReady, 3);
|
||||
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 () {
|
||||
var completed: boolean;
|
||||
var isReady = function () { return completed; }
|
||||
|
||||
// <snippet name="http">
|
||||
// ### Get JSON from invalid URL
|
||||
// ``` JavaScript
|
||||
http.getJSON("hgfttp://httpbin.org/get").fail(function (e) {
|
||||
//// Result (e) is Error!
|
||||
// <hide>
|
||||
completed = true;
|
||||
result = e;
|
||||
// </hide>
|
||||
});
|
||||
// ```
|
||||
// </snippet>
|
||||
|
||||
TKUnit.waitUntilReady(isReady, 3);
|
||||
TKUnit.assert(result instanceof Error, "Result from getJSON().fail() should be Error!");
|
||||
@ -76,10 +114,18 @@ export var test_getImage = function () {
|
||||
var completed: boolean;
|
||||
var isReady = function () { return completed; }
|
||||
|
||||
// <snippet name="http">
|
||||
// ### Get Image from URL
|
||||
// ``` JavaScript
|
||||
http.getImage("http://www.google.com/images/errors/logo_sm_2.png").then(function (r) {
|
||||
//// Result (r) is Image!
|
||||
// <hide>
|
||||
completed = true;
|
||||
result = r;
|
||||
// </hide>
|
||||
});
|
||||
// ```
|
||||
// </snippet>
|
||||
|
||||
TKUnit.waitUntilReady(isReady, 3);
|
||||
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 () {
|
||||
var completed: boolean;
|
||||
var isReady = function () { return completed; }
|
||||
|
||||
// <snippet name="http">
|
||||
// ### Get Image from URL
|
||||
// ``` JavaScript
|
||||
http.getImage("htadvtp://www.google.com/images/errors/logo_sm_2.pngm").fail(function (e) {
|
||||
//// Result (e) is Error!
|
||||
// <hide>
|
||||
completed = true;
|
||||
result = e;
|
||||
// </hide>
|
||||
});
|
||||
// ```
|
||||
// </snippet>
|
||||
|
||||
TKUnit.waitUntilReady(isReady, 3);
|
||||
TKUnit.assert(result instanceof Error, "Result from getImage().fail() should be Error!");
|
||||
@ -108,7 +162,7 @@ export var test_request_shouldFailIfOptionsUrlIsNotDefined = function () {
|
||||
var completed: boolean;
|
||||
var isReady = function () { return completed; }
|
||||
|
||||
http_request.request({ url: undefined, method: undefined }).fail(function (e) {
|
||||
http.request({ url: undefined, method: undefined }).fail(function (e) {
|
||||
completed = true;
|
||||
result = e;
|
||||
});
|
||||
@ -118,35 +172,70 @@ export var test_request_shouldFailIfOptionsUrlIsNotDefined = function () {
|
||||
};
|
||||
|
||||
export var test_request_responseStatusCodeShouldBeDefined = function () {
|
||||
var result : http_request.HttpResponse;
|
||||
var result: http.HttpResponse;
|
||||
var completed: boolean;
|
||||
var isReady = function () { return completed; }
|
||||
|
||||
http_request.request({ url: "http://httpbin.org/get", method: "GET" }).then(function (response) {
|
||||
// <snippet name="http">
|
||||
// ### Get response status code
|
||||
// ``` JavaScript
|
||||
http.request({ url: "http://httpbin.org/get", method: "GET" }).then(function (response) {
|
||||
//// Result (response) is require("http/http-request").HttpResponse!
|
||||
//// You can get status code using response.statusCode (number)!
|
||||
// <hide>
|
||||
completed = true;
|
||||
result = response;
|
||||
// </hide>
|
||||
});
|
||||
// ```
|
||||
// </snippet>
|
||||
|
||||
TKUnit.waitUntilReady(isReady, 3);
|
||||
TKUnit.assert(typeof (result.statusCode) !== "undefined", "response.statusCode should be defined!");
|
||||
};
|
||||
|
||||
export var test_request_responseHeadersShouldBeDefined = function () {
|
||||
var result: http_request.HttpResponse;
|
||||
var result: http.HttpResponse;
|
||||
var completed: boolean;
|
||||
var isReady = function () { return completed; }
|
||||
|
||||
http_request.request({ url: "http://httpbin.org/get", method: "GET" }).then(function (response) {
|
||||
// <snippet name="http">
|
||||
// ### Get response headers
|
||||
// ``` JavaScript
|
||||
http.request({ url: "http://httpbin.org/get", method: "GET" }).then(function (response) {
|
||||
//// Result (response) is require("http/http-request").HttpResponse!
|
||||
//// You can get response headers using response.headers (JSON)!
|
||||
// <hide>
|
||||
completed = true;
|
||||
result = response;
|
||||
// </hide>
|
||||
});
|
||||
// ```
|
||||
// </snippet>
|
||||
|
||||
TKUnit.waitUntilReady(isReady, 3);
|
||||
TKUnit.assert(typeof (result.headers) !== "undefined", "response.headers should be defined!");
|
||||
};
|
||||
|
||||
export var test_request_responseContentShouldBeDefined = function () {
|
||||
http_request.request({ url: "http://httpbin.org/get", method: "GET" }).then(function (response) {
|
||||
TKUnit.assert(typeof (response.content) !== "undefined", "response.content should be defined!");
|
||||
var result: http.HttpResponse;
|
||||
var completed: boolean;
|
||||
var isReady = function () { return completed; }
|
||||
|
||||
// <snippet name="http">
|
||||
// ### Get response content
|
||||
// ``` JavaScript
|
||||
http.request({ url: "http://httpbin.org/get", method: "GET" }).then(function (response) {
|
||||
//// Result (response) is require("http/http-request").HttpContent!
|
||||
//// You can get response content using response.content methods: toString(), toJSON and toImage()!
|
||||
// <hide>
|
||||
completed = true;
|
||||
result = response;
|
||||
// </hide>
|
||||
});
|
||||
// ```
|
||||
// </snippet>
|
||||
|
||||
TKUnit.waitUntilReady(isReady, 3);
|
||||
TKUnit.assert(typeof (result.content) !== "undefined", "response.content should be defined!");
|
||||
};
|
@ -3,8 +3,10 @@ import promises = require("promises/promises");
|
||||
import http = require("http/http-request");
|
||||
|
||||
// merge request
|
||||
declare var exports;
|
||||
require("utils/module-merge").merge(http, exports);
|
||||
export var request = http.request;
|
||||
export interface HttpResponse extends http.HttpResponse { };
|
||||
export interface HttpRequestOptions extends http.HttpRequestOptions { };
|
||||
export interface HttpContent extends http.HttpContent { };
|
||||
|
||||
/**
|
||||
* Gets string from url.
|
||||
|
Reference in New Issue
Block a user