forked from NativeScript/NativeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch-tests.ts
More file actions
148 lines (131 loc) · 5.45 KB
/
fetch-tests.ts
File metadata and controls
148 lines (131 loc) · 5.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/* tslint:disable:no-unused-variable */
import * as TKUnit from "../TKUnit";
import * as types from "tns-core-modules/utils/types";
export var test_fetch_defined = function () {
TKUnit.assert(types.isDefined((fetch)), "Method fetch() should be defined!");
};
export var test_fetch = function (done: (err: Error, res?: string) => void) {
// >> fetch-response
fetch("https://httpbin.org/get").then(function (r) {
// Argument (r) is Response!
// >> (hide)
TKUnit.assert(r instanceof Response, "Result from fetch() should be valid Response object! Actual result is: " + r);
done(null);
// << (hide)
}).catch(failOnError(done));
// << fetch-response
};
export var test_fetch_text = function (done: (err: Error, res?: string) => void) {
// >> fetch-string
fetch("https://httpbin.org/get").then(response => { return response.text(); }).then(function (r) {
// Argument (r) is string!
// >> (hide)
TKUnit.assert(types.isString(r), "Result from text() should be string! Actual result is: " + r);
done(null);
// << (hide)
}).catch(failOnError(done));
// << fetch-string
};
export var test_fetch_json = function (done: (err: Error, res?: string) => void) {
// >> fetch-json
fetch("https://httpbin.org/get").then(response => { return response.json(); }).then(function (r) {
// Argument (r) is JSON object!
// >> (hide)
TKUnit.assertNotNull(r, "Result from json() should be JSON object!");
done(null);
// << (hide)
}).catch(failOnError(done));
// << fetch-json
};
export var test_fetch_formData = function (done: (err: Error, res?: string) => void) {
// >> fetch-formdata
fetch("https://httpbin.org/get").then(response => { return response.formData(); }).then(function (r) {
// Argument (r) is FormData object!
// >> (hide)
TKUnit.assert(r instanceof FormData, "Result from formData() should be FormData object! Actual result is: " + r);
done(null);
// << (hide)
}).catch(failOnError(done));
// << fetch-formdata
};
export var test_fetch_fail_invalid_url = function (done) {
var completed: boolean;
var isReady = function () { return completed; }
fetch("hgfttp://httpbin.org/get").catch(function (e) {
completed = true;
done(null)
}).catch(failOnError(done));
};
export var test_fetch_invalid_url_fail_message = function (done) {
fetch("hgfttp://httpbin.org/get").catch(function (e: TypeError) {
TKUnit.assert(e.message.match(/Network request failed:.{2,}/), "Failure message should contain details on the failure. Actual message was: " + e.message);
done(null);
}).catch(failOnError(done));
};
export var test_fetch_response_status = function (done) {
// >> fetch-status-response
fetch("https://httpbin.org/get").then(function (response) {
// Argument (response) is Response!
var statusCode = response.status;
// >> (hide)
TKUnit.assert(types.isDefined(statusCode), "response.status should be defined! Actual result is: " + statusCode);
done(null);
// << (hide)
}).catch(failOnError(done));
// << fetch-status-response
};
export var test_fetch_response_headers = function (done) {
// >> fetch-headers-response
fetch("https://httpbin.org/get").then(function (response) {
// Argument (response) is Response!
// var all = response.headers.getAll();
// >> (hide)
TKUnit.assert(types.isDefined(response.headers), "response.headers should be defined! Actual result is: " + response.headers);
done(null);
// << (hide)
}).catch(failOnError(done));
// << fetch-headers-response
};
export var test_fetch_headers_sent = function (done) {
fetch("https://httpbin.org/get", {
method: "GET",
headers: { "Content-Type": "application/json" }
}).then(function (response) {
var result = response.headers;
TKUnit.assert(result.get("Content-Type") === "application/json", "Headers not sent/received properly! Actual result is: " + result);
done(null);
}).catch(failOnError(done));
};
export var test_fetch_post_form_data = function (done) {
var data = new FormData();
data.append("MyVariableOne", "ValueOne");
data.append("MyVariableTwo", "ValueTwo");
fetch("https://httpbin.org/post", {
method: "POST",
headers: { "Content-Type": "application/x-www-form-urlencoded" },
body: data
}).then(r => {
return r.formData();
}).then(function (r) {
TKUnit.assert(r instanceof FormData, "Content not sent/received properly! Actual result is: " + r);
done(null);
}).catch(failOnError(done));
};
export var test_fetch_post_json = function (done) {
// >> fetch-post-json
fetch("https://httpbin.org/post", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ MyVariableOne: "ValueOne", MyVariableTwo: "ValueTwo" })
}).then(r => { return r.json(); }).then(function (r) {
// >> (hide)
TKUnit.assert(r.json["MyVariableOne"] === "ValueOne" && r.json["MyVariableTwo"] === "ValueTwo", "Content not sent/received properly! Actual result is: " + r.json);
done(null);
// << (hide)
// console.log(result);
}).catch(failOnError(done));
// << fetch-post-json
};
const failOnError = function (done: (err: Error, res?: string) => void) {
return e => done(e);
}