Skip to content

Commit 705dd42

Browse files
committed
test: demo app show async, cancel, progress
1 parent 91b1772 commit 705dd42

File tree

2 files changed

+157
-75
lines changed

2 files changed

+157
-75
lines changed

demo/app/main-page.ts

Lines changed: 130 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,94 +1,165 @@
1-
import * as Https from 'nativescript-https';
2-
import * as Observable from 'tns-core-modules/data/observable';
3-
import * as fs from 'tns-core-modules/file-system';
4-
import * as dialogs from 'tns-core-modules/ui/dialogs';
5-
import * as Page from 'tns-core-modules/ui/page';
1+
import * as Https from "nativescript-https";
2+
import * as Observable from "tns-core-modules/data/observable";
3+
import * as fs from "tns-core-modules/file-system";
4+
import * as dialogs from "tns-core-modules/ui/dialogs";
5+
import * as Page from "tns-core-modules/ui/page";
66

7-
export function onNavigatingTo(args: Page.NavigatedData) {
8-
let page = args.object as Page.Page;
9-
page.bindingContext = Observable.fromObject({enabled: false});
7+
let page;
8+
let viewModel;
9+
export function pageLoaded(args: Page.NavigatedData) {
10+
page = args.object as Page.Page;
11+
viewModel = Observable.fromObject({
12+
enabled: false,
13+
progress: 0,
14+
currentRequest: null,
15+
});
16+
page.bindingContext = viewModel;
1017
}
1118

12-
function getRequest(url: string, allowLargeResponse = false) {
13-
Https.request(
14-
{
19+
function createRequest(
20+
url: string,
21+
options?: Partial<Https.HttpsRequestOptions>
22+
) {
23+
return Https.createRequest({
24+
useLegacy: true,
1525
url,
16-
method: 'GET',
26+
method: "GET",
1727
timeout: 1,
18-
allowLargeResponse
19-
})
20-
.then(response => console.log('Https.request response', response))
21-
.catch(error => {
22-
console.error('Https.request error', error);
23-
dialogs.alert(error);
24-
});
28+
...options,
29+
});
2530
}
2631

27-
function postRequest(url: string, body: any) {
28-
Https.request(
29-
{
32+
function onError(error) {
33+
console.error("Https.request error", error, error.stack);
34+
dialogs.alert(error.toString());
35+
page.bindingContext.currentRequest = null;
36+
page.bindingContext.progress = 0;
37+
return Promise.reject(error);
38+
}
39+
function getRequest(url: string, options?: Partial<Https.HttpsRequestOptions>) {
40+
return Https.request({
41+
useLegacy: true,
3042
url,
31-
method: 'POST',
32-
body
33-
})
34-
.then(response => console.log('Https.request response', response))
35-
.catch(error => {
36-
console.error('Https.request error', error);
37-
dialogs.alert(error);
38-
});
43+
method: "GET",
44+
timeout: 1,
45+
...options,
46+
})
47+
.then((response) => {
48+
page.bindingContext.currentRequest = null;
49+
page.bindingContext.progress = 0;
50+
console.log("Https.request response", response);
51+
return response;
52+
})
53+
.catch(onError);
54+
}
55+
56+
function postRequest(
57+
url: string,
58+
options?: Partial<Https.HttpsRequestOptions>
59+
) {
60+
return Https.request({
61+
useLegacy: true,
62+
url,
63+
method: "POST",
64+
...options,
65+
})
66+
.then((response) => console.log("Https.request response", response))
67+
.catch((error) => {
68+
console.error("Https.request error", error);
69+
dialogs.alert(error);
70+
});
3971
}
4072

4173
export function postHttpbin() {
42-
postRequest('https://httpbin.org/post', {"foo": "bar", "baz": undefined, "plaz": null});
74+
postRequest("https://httpbin.org/post", {
75+
body: { foo: "bar", baz: undefined, plaz: null },
76+
});
4377
}
4478

4579
export function postHttpbinWithUTF8() {
46-
Https.request(
47-
{
48-
url: 'https://httpbin.org/post',
49-
method: 'POST',
50-
body: {"foo": "bar", "baz": undefined, "plaz": null},
80+
Https.request({
81+
url: "https://httpbin.org/post",
82+
method: "POST",
83+
body: { foo: "bar", baz: undefined, plaz: null },
5184
headers: {
52-
'Content-Type': "application/json; charset=utf-8",
53-
'X-testing': "ok"
54-
}
55-
})
56-
.then(response => console.log('Https.request response', response))
57-
.catch(error => {
58-
console.error('Https.request error', error);
85+
"Content-Type": "application/json; charset=utf-8",
86+
"X-testing": "ok",
87+
},
88+
}).catch((error) => {
89+
console.error("Https.request error", error);
5990
dialogs.alert(error);
60-
});
91+
});
6192
}
6293

6394
export function getHttpbin() {
64-
getRequest('https://httpbin.org/get');
95+
getRequest("https://httpbin.org/get");
96+
}
97+
98+
export function cancelCurrentRequest() {
99+
if (page.bindingContext.currentRequest) {
100+
page.bindingContext.currentRequest.cancel();
101+
}
102+
}
103+
export function getBigFile() {
104+
const request = createRequest(
105+
"http://ipv4.download.thinkbroadband.com/200MB.zip",
106+
{
107+
onProgress: (current, total) => {
108+
page.bindingContext.progress = (current / total) * 100;
109+
},
110+
}
111+
);
112+
page.bindingContext.currentRequest = request;
113+
114+
return new Promise<any>((resolve, reject) => {
115+
request.run(resolve, reject);
116+
})
117+
.then((response) => {
118+
console.log("did get response");
119+
let dir = fs.knownFolders.temp().getFile("200MB.zip");
120+
return (response.content as Https.HttpsResponseLegacy).toFile(
121+
dir.path
122+
);
123+
// console.log("did get response done");
124+
})
125+
.then(() => {
126+
page.bindingContext.currentPromise = null;
127+
console.log("did get response done");
128+
})
129+
.catch(onError);
65130
}
66131

67132
export function getHttpbinLargeResponse() {
68-
getRequest('https://httpbin.org/bytes/100000', true);
133+
getRequest("https://httpbin.org/bytes/100000");
69134
}
70135

71136
export function getMockbin() {
72-
getRequest('https://mockbin.com/request');
137+
getRequest("https://mockbin.com/request");
138+
}
139+
140+
export function get404() {
141+
getRequest("https://mockbin.com/reque2st");
73142
}
74143

75144
export function enableSSLPinning(args: Observable.EventData) {
76-
let dir = fs.knownFolders.currentApp().getFolder('assets');
77-
let certificate = dir.getFile('httpbin.org.cer').path;
78-
Https.enableSSLPinning({host: 'httpbin.org', commonName: "httpbin.org", certificate});
79-
console.log('enabled');
145+
let dir = fs.knownFolders.currentApp().getFolder("assets");
146+
let certificate = dir.getFile("httpbin.org.cer").path;
147+
Https.enableSSLPinning({
148+
host: "httpbin.org",
149+
commonName: "httpbin.org",
150+
certificate,
151+
});
152+
console.log("enabled");
80153
}
81154

82155
export function enableSSLPinningExpired(args: Observable.EventData) {
83-
let dir = fs.knownFolders.currentApp().getFolder('assets');
84-
let certificate = dir.getFile('httpbin.org.expired.cer').path;
85-
Https.enableSSLPinning({host: 'httpbin.org', certificate});
86-
console.log('enabled');
156+
let dir = fs.knownFolders.currentApp().getFolder("assets");
157+
let certificate = dir.getFile("httpbin.org.expired.cer").path;
158+
Https.enableSSLPinning({ host: "httpbin.org", certificate });
159+
console.log("enabled");
87160
}
88161

89162
export function disableSSLPinning(args: Observable.EventData) {
90-
Https.disableSSLPinning();
91-
console.log('disabled');
163+
Https.disableSSLPinning();
164+
console.log("disabled");
92165
}
93-
94-

demo/app/main-page.xml

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,31 @@
1-
<Page class="page" navigatingTo="onNavigatingTo">
1+
<Page class="page" loaded="pageLoaded"
2+
xmlns="http://schemas.nativescript.org/tns.xsd">
23
<Page.actionBar>
34
<ActionBar title="HTTPS Playground" class="action-bar"></ActionBar>
45
</Page.actionBar>
5-
6-
<StackLayout class="p-20">
7-
<Label class="h1 text-center" color="{{ enabled ? 'green' : 'red' }}" text="{{ enabled ? 'Httpbin SSL Pinning Enabled' : 'SSL Pinning Disabled' }}"/>
8-
<Button text="GET Mockbin" tap="getMockbin" class="t-20 btn btn-primary btn-active"/>
9-
<Button text="GET Httpbin" tap="getHttpbin" class="t-20 btn btn-primary btn-active"/>
10-
<Button text="GET Httpbin (large response)" tap="getHttpbinLargeResponse" class="t-20 btn btn-primary btn-active"/>
11-
<Button text="POST Httpbin " tap="postHttpbin" class="t-20 btn btn-primary btn-active"/>
12-
<Button text="POST Httpbin UTF-8" tap="postHttpbinWithUTF8" class="t-20 btn btn-primary btn-active"/>
13-
<Button text="Httpbin Pinning ON" tap="enableSSLPinning" class="t-20 btn btn-primary btn-active"/>
14-
<Button text="Httpbin Pinning ON, expired cert" tap="enableSSLPinningExpired" class="t-20 btn btn-primary btn-active"/>
15-
<Button text="Httpbin Pinning OFF" tap="disableSSLPinning" class="t-20 btn btn-primary btn-active"/>
16-
<Label text="Here's a spinner to show the main thread is not blocked by network IO" textWrap="true" class="m-20"/>
17-
<ActivityIndicator busy="true"/>
18-
</StackLayout>
19-
6+
<GridLayout class="p-20" rows="auto,auto,auto,*">
7+
<Label class="h2 text-center" color="{{ enabled ? 'green' : 'red' }}" text="{{ enabled ? 'Httpbin SSL Pinning Enabled' : 'SSL Pinning Disabled' }}"/>
8+
<GridLayout row="1" rows="auto" columns="*,auto">
9+
<Label fontSize="13" text="Here's a spinner to show the main thread is not blocked by network IO" textWrap="true" class="m-20"/>
10+
<ActivityIndicator col="1" busy="true"/>
11+
</GridLayout>
12+
<GridLayout row="2" rows="auto" columns="*,auto" visibility="{{ currentRequest ? 'visible' : 'collapsed'}}">
13+
<Progress minValue="0" maxValue="100" value="{{ progress}}"/>
14+
<Button col="1" text="cancel" tap="cancelCurrentRequest"/>
15+
</GridLayout>
16+
<ScrollView row="3">
17+
<StackLayout>
18+
<Button text="GET Bigfile" tap="getBigFile" class="t-20 btn btn-primary btn-active"/>
19+
<Button text="GET 404" tap="get404" class="t-20 btn btn-primary btn-active"/>
20+
<Button text="GET Mockbin" tap="getMockbin" class="t-20 btn btn-primary btn-active"/>
21+
<Button text="GET Httpbin" tap="getHttpbin" class="t-20 btn btn-primary btn-active"/>
22+
<Button text="GET Httpbin (large response)" tap="getHttpbinLargeResponse" class="t-20 btn btn-primary btn-active"/>
23+
<Button text="POST Httpbin " tap="postHttpbin" class="t-20 btn btn-primary btn-active"/>
24+
<Button text="POST Httpbin UTF-8" tap="postHttpbinWithUTF8" class="t-20 btn btn-primary btn-active"/>
25+
<Button text="Httpbin Pinning ON" tap="enableSSLPinning" class="t-20 btn btn-primary btn-active"/>
26+
<Button text="Httpbin Pinning ON, expired cert" tap="enableSSLPinningExpired" class="t-20 btn btn-primary btn-active"/>
27+
<Button text="Httpbin Pinning OFF" tap="disableSSLPinning" class="t-20 btn btn-primary btn-active"/>
28+
</StackLayout>
29+
</ScrollView>
30+
</GridLayout>
2031
</Page>

0 commit comments

Comments
 (0)