mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-16 20:11:24 +08:00
request body for android fixed and test added
This commit is contained in:
@ -304,4 +304,24 @@ export var test_request_headersSentAndReceivedProperly = function () {
|
|||||||
|
|
||||||
TKUnit.waitUntilReady(isReady, 3);
|
TKUnit.waitUntilReady(isReady, 3);
|
||||||
TKUnit.assert(result["Content-Type"] === "application/json", "Headers not sent/received properly!");
|
TKUnit.assert(result["Content-Type"] === "application/json", "Headers not sent/received properly!");
|
||||||
|
};
|
||||||
|
|
||||||
|
export var test_request_contentSentAndReceivedProperly = function () {
|
||||||
|
var result;
|
||||||
|
var completed: boolean;
|
||||||
|
var isReady = function () { return completed; }
|
||||||
|
|
||||||
|
http.request({
|
||||||
|
url: "http://httpbin.org/post", method: "POST",
|
||||||
|
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
||||||
|
content: "MyVariableOne=ValueOne&MyVariableTwo=ValueTwo"
|
||||||
|
}).then(function (response) {
|
||||||
|
completed = true;
|
||||||
|
result = response.content.toJSON();
|
||||||
|
}).fail(function (e) {
|
||||||
|
console.log(e);
|
||||||
|
});
|
||||||
|
|
||||||
|
TKUnit.waitUntilReady(isReady, 3);
|
||||||
|
TKUnit.assert(result["form"]["MyVariableOne"] === "ValueOne" && result["form"]["MyVariableTwo"] === "ValueTwo", "Content not sent/received properly!");
|
||||||
};
|
};
|
15
declarations.android.d.ts
vendored
15
declarations.android.d.ts
vendored
@ -370,6 +370,14 @@ declare module com {
|
|||||||
|
|
||||||
export module http {
|
export module http {
|
||||||
|
|
||||||
|
export class AsyncHttpRequest {
|
||||||
|
constructor(uri: java.net.URI, method: string);
|
||||||
|
addHeader(name: string, v: string);
|
||||||
|
setTimeout(timeout: number);
|
||||||
|
setBody(body: any);
|
||||||
|
static extends(source: any);
|
||||||
|
}
|
||||||
|
|
||||||
export module libcore {
|
export module libcore {
|
||||||
export class RawHeaders {
|
export class RawHeaders {
|
||||||
constructor();
|
constructor();
|
||||||
@ -395,6 +403,13 @@ declare module com {
|
|||||||
export module body {
|
export module body {
|
||||||
export class StringBody {
|
export class StringBody {
|
||||||
constructor(source: string);
|
constructor(source: string);
|
||||||
|
static extends(source: any);
|
||||||
|
}
|
||||||
|
export class UrlEncodedFormBody {
|
||||||
|
constructor(source: any);
|
||||||
|
}
|
||||||
|
export class StreamBody {
|
||||||
|
constructor(source: java.io.InputStream, length: number);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,11 +10,7 @@ export function request(options: http.HttpRequestOptions): promises.Promise<http
|
|||||||
var d = promises.defer<http.HttpResponse>();
|
var d = promises.defer<http.HttpResponse>();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
var request = new com.koushikdutta.async.http.AsyncHttpRequest(java.net.URI.create(options.url), options.method);
|
||||||
var context = require("application").android.context;
|
|
||||||
|
|
||||||
var request = com.koushikdutta.ion.Ion.getDefault(context).configure().getAsyncHttpRequestFactory()
|
|
||||||
.createAsyncHttpRequest(java.net.URI.create(options.url), options.method, null);
|
|
||||||
|
|
||||||
if (options.headers) {
|
if (options.headers) {
|
||||||
for (var key in options.headers) {
|
for (var key in options.headers) {
|
||||||
@ -27,7 +23,13 @@ export function request(options: http.HttpRequestOptions): promises.Promise<http
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (typeof options.content == "string") {
|
if (typeof options.content == "string") {
|
||||||
request.setBody(new com.koushikdutta.async.http.body.StringBody(options.content));
|
var stringBody = com.koushikdutta.async.http.body.StringBody.extends({
|
||||||
|
getContentType: function () {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
request.setBody(new stringBody(options.content));
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// TODO: How to transfer everything else?
|
// TODO: How to transfer everything else?
|
||||||
@ -45,7 +47,7 @@ export function request(options: http.HttpRequestOptions): promises.Promise<http
|
|||||||
var key = rawHeaders.getFieldName(i);
|
var key = rawHeaders.getFieldName(i);
|
||||||
headers[key] = rawHeaders.getValue(i);
|
headers[key] = rawHeaders.getValue(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
var outputStream = new java.io.ByteArrayOutputStream();
|
var outputStream = new java.io.ByteArrayOutputStream();
|
||||||
|
|
||||||
var dataCallback = new com.koushikdutta.async.callback.DataCallback({
|
var dataCallback = new com.koushikdutta.async.callback.DataCallback({
|
||||||
@ -56,7 +58,7 @@ export function request(options: http.HttpRequestOptions): promises.Promise<http
|
|||||||
});
|
});
|
||||||
|
|
||||||
response.setDataCallback(dataCallback);
|
response.setDataCallback(dataCallback);
|
||||||
|
|
||||||
var endCallback = new com.koushikdutta.async.callback.CompletedCallback({
|
var endCallback = new com.koushikdutta.async.callback.CompletedCallback({
|
||||||
onCompleted: function (error) {
|
onCompleted: function (error) {
|
||||||
d.resolve({
|
d.resolve({
|
||||||
|
Reference in New Issue
Block a user