| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -158,9 +158,9 @@ export var test_getJSON_fail_when_result_is_not_JSONP = function (done) { | |||
| 158 | 158 | export var test_gzip_request_explicit = function(done) { | |
| 159 | 159 | var result; | |
| 160 | 160 | ||
| 161 | - http.request({ | ||
| 162 | - url: "https://postman-echo.com/gzip", | ||
| 163 | - method: "GET", | ||
| 161 | + http.request({ | ||
| 162 | + url: "https://postman-echo.com/gzip", | ||
| 163 | + method: "GET", | ||
| 164 | 164 | headers: { | |
| 165 | 165 | "Accept-Encoding": "gzip" | |
| 166 | 166 | }}).then(function (r) { | |
@@ -180,8 +180,8 @@ export var test_gzip_request_explicit = function(done) { | |||
| 180 | 180 | export var test_gzip_request_implicit = function(done) { | |
| 181 | 181 | var result; | |
| 182 | 182 | ||
| 183 | - http.request({ | ||
| 184 | - url: "https://postman-echo.com/gzip", | ||
| 183 | + http.request({ | ||
| 184 | + url: "https://postman-echo.com/gzip", | ||
| 185 | 185 | method: "GET"}).then(function (r) { | |
| 186 | 186 | result = r; | |
| 187 | 187 | try { | |
@@ -524,6 +524,24 @@ export var test_request_responseContentToFileFromUrlShouldReturnCorrectFile = fu | |||
| 524 | 524 | done(e); | |
| 525 | 525 | }); | |
| 526 | 526 | }; | |
| 527 | + export var test_request_responseContentToFileFromUrlShouldReturnCorrectFileAndCreateDirPathIfNecesary = function (done) { | ||
| 528 | + var result; | ||
| 529 | + | ||
| 530 | + http.request({ url: "https://raw.githubusercontent.com/NativeScript/NativeScript/master/tests/app/logo.png", method: "GET" }).then(function (response) { | ||
| 531 | + const filePath = fs.path.join(fs.knownFolders.temp().path, "test", "some", "path", "logo.png"); | ||
| 532 | + result = response.content.toFile(filePath); | ||
| 533 | + try { | ||
| 534 | + TKUnit.assert(result instanceof fs.File, "Result from toFile() should be valid File object!"); | ||
| 535 | + TKUnit.assert(result.size > 0, "result from to file should be greater than 0 in size"); | ||
| 536 | + done(null); | ||
| 537 | + } | ||
| 538 | + catch (err) { | ||
| 539 | + done(err); | ||
| 540 | + } | ||
| 541 | + }, function (e) { | ||
| 542 | + done(e); | ||
| 543 | + }); | ||
| 544 | + }; | ||
| 527 | 545 | ||
| 528 | 546 | export var test_request_responseContentToFileFromContentShouldReturnCorrectFile = function (done) { | |
| 529 | 547 | var result; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -6,10 +6,10 @@ | |||
| 6 | 6 | "nativescript": { | |
| 7 | 7 | "id": "org.nativescript.UnitTestApp", | |
| 8 | 8 | "tns-ios": { | |
| 9 | - "version": "5.1.0" | ||
| 9 | + "version": "5.2.0" | ||
| 10 | 10 | }, | |
| 11 | 11 | "tns-android": { | |
| 12 | - "version": "5.1.0" | ||
| 12 | + "version": "5.2.1" | ||
| 13 | 13 | } | |
| 14 | 14 | }, | |
| 15 | 15 | "dependencies": { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,9 +1,9 @@ | |||
| 1 | 1 | /** | |
| 2 | 2 | * Android specific http request implementation. | |
| 3 | 3 | */ | |
| 4 | - import * as imageSourceModule from "../../image-source"; | ||
| 5 | - import * as platformModule from "../../platform"; | ||
| 6 | - import * as fsModule from "../../file-system"; | ||
| 4 | + import { fromNativeSource } from "../../image-source"; | ||
| 5 | + import { screen } from "../../platform"; | ||
| 6 | + import { File } from "../../file-system"; | ||
| 7 | 7 | import { getFilenameFromUrl } from "./http-request-common"; | |
| 8 | 8 | ||
| 9 | 9 | // this is imported for definition purposes only | |
@@ -17,32 +17,18 @@ export enum HttpResponseEncoding { | |||
| 17 | 17 | } | |
| 18 | 18 | ||
| 19 | 19 | function parseJSON(source: string): any { | |
| 20 | - var src = source.trim(); | ||
| 20 | + const src = source.trim(); | ||
| 21 | 21 | if (src.lastIndexOf(")") === src.length - 1) { | |
| 22 | 22 | return JSON.parse(src.substring(src.indexOf("(") + 1, src.lastIndexOf(")"))); | |
| 23 | 23 | } | |
| 24 | 24 | ||
| 25 | 25 | return JSON.parse(src); | |
| 26 | 26 | } | |
| 27 | 27 | ||
| 28 | - var requestIdCounter = 0; | ||
| 29 | - var pendingRequests = {}; | ||
| 28 | + let requestIdCounter = 0; | ||
| 29 | + const pendingRequests = {}; | ||
| 30 | 30 | ||
| 31 | - var imageSource: typeof imageSourceModule; | ||
| 32 | - function ensureImageSource() { | ||
| 33 | - if (!imageSource) { | ||
| 34 | - imageSource = require("image-source"); | ||
| 35 | - } | ||
| 36 | - } | ||
| 37 | - | ||
| 38 | - var platform: typeof platformModule; | ||
| 39 | - function ensurePlatform() { | ||
| 40 | - if (!platform) { | ||
| 41 | - platform = require("platform"); | ||
| 42 | - } | ||
| 43 | - } | ||
| 44 | - | ||
| 45 | - var completeCallback: org.nativescript.widgets.Async.CompleteCallback; | ||
| 31 | + let completeCallback: org.nativescript.widgets.Async.CompleteCallback; | ||
| 46 | 32 | function ensureCompleteCallback() { | |
| 47 | 33 | if (completeCallback) { | |
| 48 | 34 | return; | |
@@ -60,7 +46,7 @@ function ensureCompleteCallback() { | |||
| 60 | 46 | } | |
| 61 | 47 | ||
| 62 | 48 | function onRequestComplete(requestId: number, result: org.nativescript.widgets.Async.Http.RequestResult) { | |
| 63 | - var callbacks = pendingRequests[requestId]; | ||
| 49 | + const callbacks = pendingRequests[requestId]; | ||
| 64 | 50 | delete pendingRequests[requestId]; | |
| 65 | 51 | ||
| 66 | 52 | if (result.error) { | |
@@ -69,13 +55,12 @@ function onRequestComplete(requestId: number, result: org.nativescript.widgets.A | |||
| 69 | 55 | } | |
| 70 | 56 | ||
| 71 | 57 | // read the headers | |
| 72 | - var headers: http.Headers = {}; | ||
| 58 | + const headers: http.Headers = {}; | ||
| 73 | 59 | if (result.headers) { | |
| 74 | - var jHeaders = result.headers; | ||
| 75 | - var length = jHeaders.size(); | ||
| 76 | - var i; | ||
| 77 | - var pair: org.nativescript.widgets.Async.Http.KeyValuePair; | ||
| 78 | - for (i = 0; i < length; i++) { | ||
| 60 | + const jHeaders = result.headers; | ||
| 61 | + const length = jHeaders.size(); | ||
| 62 | + let pair: org.nativescript.widgets.Async.Http.KeyValuePair; | ||
| 63 | + for (let i = 0; i < length; i++) { | ||
| 79 | 64 | pair = jHeaders.get(i); | |
| 80 | 65 | addHeader(headers, pair.key, pair.value); | |
| 81 | 66 | } | |
@@ -112,29 +97,29 @@ function onRequestComplete(requestId: number, result: org.nativescript.widgets.A | |||
| 112 | 97 | return parseJSON(str); | |
| 113 | 98 | }, | |
| 114 | 99 | toImage: () => { | |
| 115 | - ensureImageSource(); | ||
| 116 | - | ||
| 117 | 100 | return new Promise<any>((resolveImage, rejectImage) => { | |
| 118 | 101 | if (result.responseAsImage != null) { | |
| 119 | - resolveImage(imageSource.fromNativeSource(result.responseAsImage)); | ||
| 102 | + resolveImage(fromNativeSource(result.responseAsImage)); | ||
| 120 | 103 | } | |
| 121 | 104 | else { | |
| 122 | 105 | rejectImage(new Error("Response content may not be converted to an Image")); | |
| 123 | 106 | } | |
| 124 | 107 | }); | |
| 125 | 108 | }, | |
| 126 | 109 | toFile: (destinationFilePath: string) => { | |
| 127 | - var fs: typeof fsModule = require("file-system"); | ||
| 128 | - | ||
| 129 | 110 | if (!destinationFilePath) { | |
| 130 | 111 | destinationFilePath = getFilenameFromUrl(callbacks.url); | |
| 131 | 112 | } | |
| 132 | - var stream: java.io.FileOutputStream; | ||
| 113 | + let stream: java.io.FileOutputStream; | ||
| 133 | 114 | try { | |
| 134 | - var javaFile = new java.io.File(destinationFilePath); | ||
| 115 | + // ensure destination path exists by creating any missing parent directories | ||
| 116 | + const file = File.fromPath(destinationFilePath); | ||
| 117 | + | ||
| 118 | + const javaFile = new java.io.File(destinationFilePath); | ||
| 135 | 119 | stream = new java.io.FileOutputStream(javaFile); | |
| 136 | 120 | stream.write(result.raw.toByteArray()); | |
| 137 | - return fs.File.fromPath(destinationFilePath); | ||
| 121 | + | ||
| 122 | + return file; | ||
| 138 | 123 | } | |
| 139 | 124 | catch (exception) { | |
| 140 | 125 | throw new Error(`Cannot save file with path: ${destinationFilePath}.`); | |
@@ -152,7 +137,7 @@ function onRequestComplete(requestId: number, result: org.nativescript.widgets.A | |||
| 152 | 137 | } | |
| 153 | 138 | ||
| 154 | 139 | function onRequestError(error: string, requestId: number) { | |
| 155 | - var callbacks = pendingRequests[requestId]; | ||
| 140 | + const callbacks = pendingRequests[requestId]; | ||
| 156 | 141 | delete pendingRequests[requestId]; | |
| 157 | 142 | if (callbacks) { | |
| 158 | 143 | callbacks.rejectCallback(new Error(error)); | |
@@ -164,7 +149,7 @@ function buildJavaOptions(options: http.HttpRequestOptions) { | |||
| 164 | 149 | throw new Error("Http request must provide a valid url."); | |
| 165 | 150 | } | |
| 166 | 151 | ||
| 167 | - var javaOptions = new org.nativescript.widgets.Async.Http.RequestOptions(); | ||
| 152 | + const javaOptions = new org.nativescript.widgets.Async.Http.RequestOptions(); | ||
| 168 | 153 | ||
| 169 | 154 | javaOptions.url = options.url; | |
| 170 | 155 | ||
@@ -182,22 +167,19 @@ function buildJavaOptions(options: http.HttpRequestOptions) { | |||
| 182 | 167 | } | |
| 183 | 168 | ||
| 184 | 169 | if (options.headers) { | |
| 185 | - var arrayList = new java.util.ArrayList<org.nativescript.widgets.Async.Http.KeyValuePair>(); | ||
| 186 | - var pair = org.nativescript.widgets.Async.Http.KeyValuePair; | ||
| 170 | + const arrayList = new java.util.ArrayList<org.nativescript.widgets.Async.Http.KeyValuePair>(); | ||
| 171 | + const pair = org.nativescript.widgets.Async.Http.KeyValuePair; | ||
| 187 | 172 | ||
| 188 | - for (var key in options.headers) { | ||
| 173 | + for (let key in options.headers) { | ||
| 189 | 174 | arrayList.add(new pair(key, options.headers[key] + "")); | |
| 190 | 175 | } | |
| 191 | 176 | ||
| 192 | 177 | javaOptions.headers = arrayList; | |
| 193 | 178 | } | |
| 194 | 179 | ||
| 195 | - ensurePlatform(); | ||
| 196 | - | ||
| 197 | 180 | // pass the maximum available image size to the request options in case we need a bitmap conversion | |
| 198 | - var screen = platform.screen.mainScreen; | ||
| 199 | - javaOptions.screenWidth = screen.widthPixels; | ||
| 200 | - javaOptions.screenHeight = screen.heightPixels; | ||
| 181 | + javaOptions.screenWidth = screen.mainScreen.widthPixels; | ||
| 182 | + javaOptions.screenHeight = screen.mainScreen.heightPixels; | ||
| 201 | 183 | ||
| 202 | 184 | return javaOptions; | |
| 203 | 185 | } | |
@@ -211,15 +193,15 @@ export function request(options: http.HttpRequestOptions): Promise<http.HttpResp | |||
| 211 | 193 | return new Promise<http.HttpResponse>((resolve, reject) => { | |
| 212 | 194 | try { | |
| 213 | 195 | // initialize the options | |
| 214 | - var javaOptions = buildJavaOptions(options); | ||
| 196 | + const javaOptions = buildJavaOptions(options); | ||
| 215 | 197 | ||
| 216 | 198 | // send request data to network debugger | |
| 217 | 199 | if (global.__inspector && global.__inspector.isConnected) { | |
| 218 | 200 | NetworkAgent.requestWillBeSent(requestIdCounter, options); | |
| 219 | 201 | } | |
| 220 | 202 | ||
| 221 | 203 | // remember the callbacks so that we can use them when the CompleteCallback is called | |
| 222 | - var callbacks = { | ||
| 204 | + const callbacks = { | ||
| 223 | 205 | url: options.url, | |
| 224 | 206 | resolveCallback: resolve, | |
| 225 | 207 | rejectCallback: reject | |
@@ -252,7 +234,7 @@ export function addHeader(headers: http.Headers, key: string, value: string): vo | |||
| 252 | 234 | } else if (Array.isArray(headers[key])) { | |
| 253 | 235 | (<string[]>headers[key]).push(value); | |
| 254 | 236 | } else { | |
| 255 | - let values: string[] = [<string>headers[key]]; | ||
| 237 | + const values: string[] = [<string>headers[key]]; | ||
| 256 | 238 | values.push(value); | |
| 257 | 239 | headers[key] = values; | |
| 258 | 240 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments