Skip to content

Commit 82fa2b3

Browse files
author
Vladimir Enchev
committed
request body for android fixed and test added
1 parent b3a0b8d commit 82fa2b3

3 files changed

Lines changed: 45 additions & 8 deletions

File tree

Tests/http-tests.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,4 +304,24 @@ export var test_request_headersSentAndReceivedProperly = function () {
304304

305305
TKUnit.waitUntilReady(isReady, 3);
306306
TKUnit.assert(result["Content-Type"] === "application/json", "Headers not sent/received properly!");
307+
};
308+
309+
export var test_request_contentSentAndReceivedProperly = function () {
310+
var result;
311+
var completed: boolean;
312+
var isReady = function () { return completed; }
313+
314+
http.request({
315+
url: "http://httpbin.org/post", method: "POST",
316+
headers: { "Content-Type": "application/x-www-form-urlencoded" },
317+
content: "MyVariableOne=ValueOne&MyVariableTwo=ValueTwo"
318+
}).then(function (response) {
319+
completed = true;
320+
result = response.content.toJSON();
321+
}).fail(function (e) {
322+
console.log(e);
323+
});
324+
325+
TKUnit.waitUntilReady(isReady, 3);
326+
TKUnit.assert(result["form"]["MyVariableOne"] === "ValueOne" && result["form"]["MyVariableTwo"] === "ValueTwo", "Content not sent/received properly!");
307327
};

declarations.android.d.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,14 @@ declare module com {
370370

371371
export module http {
372372

373+
export class AsyncHttpRequest {
374+
constructor(uri: java.net.URI, method: string);
375+
addHeader(name: string, v: string);
376+
setTimeout(timeout: number);
377+
setBody(body: any);
378+
static extends(source: any);
379+
}
380+
373381
export module libcore {
374382
export class RawHeaders {
375383
constructor();
@@ -395,6 +403,13 @@ declare module com {
395403
export module body {
396404
export class StringBody {
397405
constructor(source: string);
406+
static extends(source: any);
407+
}
408+
export class UrlEncodedFormBody {
409+
constructor(source: any);
410+
}
411+
export class StreamBody {
412+
constructor(source: java.io.InputStream, length: number);
398413
}
399414
}
400415
}

http/http-request.android.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,7 @@ export function request(options: http.HttpRequestOptions): promises.Promise<http
1010
var d = promises.defer<http.HttpResponse>();
1111

1212
try {
13-
14-
var context = require("application").android.context;
15-
16-
var request = com.koushikdutta.ion.Ion.getDefault(context).configure().getAsyncHttpRequestFactory()
17-
.createAsyncHttpRequest(java.net.URI.create(options.url), options.method, null);
13+
var request = new com.koushikdutta.async.http.AsyncHttpRequest(java.net.URI.create(options.url), options.method);
1814

1915
if (options.headers) {
2016
for (var key in options.headers) {
@@ -27,7 +23,13 @@ export function request(options: http.HttpRequestOptions): promises.Promise<http
2723
}
2824

2925
if (typeof options.content == "string") {
30-
request.setBody(new com.koushikdutta.async.http.body.StringBody(options.content));
26+
var stringBody = com.koushikdutta.async.http.body.StringBody.extends({
27+
getContentType: function () {
28+
return null;
29+
}
30+
});
31+
32+
request.setBody(new stringBody(options.content));
3133
}
3234
else {
3335
// TODO: How to transfer everything else?
@@ -45,7 +47,7 @@ export function request(options: http.HttpRequestOptions): promises.Promise<http
4547
var key = rawHeaders.getFieldName(i);
4648
headers[key] = rawHeaders.getValue(i);
4749
}
48-
50+
4951
var outputStream = new java.io.ByteArrayOutputStream();
5052

5153
var dataCallback = new com.koushikdutta.async.callback.DataCallback({
@@ -56,7 +58,7 @@ export function request(options: http.HttpRequestOptions): promises.Promise<http
5658
});
5759

5860
response.setDataCallback(dataCallback);
59-
61+
6062
var endCallback = new com.koushikdutta.async.callback.CompletedCallback({
6163
onCompleted: function (error) {
6264
d.resolve({

0 commit comments

Comments
 (0)