Skip to content

Commit 1366da0

Browse files
author
Shashi Ranjan
committed
java test
1 parent 3d28188 commit 1366da0

11 files changed

Lines changed: 74 additions & 80 deletions

File tree

src/targets/java/unirest.js

Lines changed: 37 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,51 +3,45 @@
33
var util = require('util');
44

55
module.exports = function(options) {
6-
var opts = util._extend({
7-
indent : ' '
8-
}, options);
9-
10-
var code = [];
11-
12-
code.push(util.format(
13-
'HttpResponse<JsonNode> jsonResponse = Unirest.%s("%s")', this.source.method.toLowerCase(),
14-
this.source.fullUrl));
15-
16-
// construct headers
17-
if (this.source.headers && this.source.headers.length) {
18-
this.source.headers.map(function(header) {
19-
code.push(util.format('.header("%s", "%s")', header.name,
20-
header.value));
21-
});
22-
}
23-
24-
25-
//construct cookies argument
26-
if (this.source.cookies && this.source.cookies.length) {
27-
var cookies = this.source.cookies.map(function(cookie) {
28-
return encodeURIComponent(cookie.name) + '='
29-
+ encodeURIComponent(cookie.value);
30-
});
31-
code.push(util.format('.header("Cookie", "%s")', cookies.join('; ')));
32-
}
33-
34-
//construct postdata
35-
if (this.source.postData) {
36-
if(this.source.postData.text){
37-
code.push(util.format('.body(%s)', JSON
38-
.stringify(this.source.postData.text)));
39-
}
40-
}
41-
42-
code.push(".asJson();");
43-
return code.join( '\n' + opts.indent);
44-
};
6+
var self = this;
7+
var opts = util._extend({
8+
indent : ' '
9+
}, options);
10+
11+
var code = [];
12+
13+
var methods = [ "GET", "POST", "PUT", "DELETE", "PATCH", "HEAD", "OPTIONS" ];
14+
15+
if (methods.indexOf(self.source.method.toUpperCase()) == -1) {
16+
return self.source.method.toUpperCase() + " method not supported by Unirest liabrary.";
17+
}
4518

19+
code.push(util.format('HttpResponse<String> response = Unirest.%s("%s")', self.source.method.toLowerCase(), self.source.fullUrl));
4620

21+
// Add headers, including the cookies
22+
var headers = Object.keys(self.source.allHeaders);
23+
24+
// construct headers
25+
if (headers.length) {
26+
headers.map(function(key) {
27+
code.push(util.format('.header("%s", "%s")', key, self.source.allHeaders[key]));
28+
});
29+
}
30+
31+
// construct postdata
32+
if (self.source.postData) {
33+
if (self.source.postData.text) {
34+
code.push(util.format('.body(%s)', JSON.stringify(self.source.postData.text)));
35+
}
36+
}
37+
38+
code.push(".asString();");
39+
return code.join('\n' + opts.indent);
40+
};
4741

4842
module.exports.info = {
49-
key: 'unirest',
50-
title: 'JAVA',
51-
link : 'http://unirest.io/java.html',
52-
description : 'Unirest Java interface'
43+
key : 'unirest',
44+
title : 'JAVA',
45+
link : 'http://unirest.io/java.html',
46+
description : 'Unirest Java interface'
5347
};
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
HttpResponse<JsonNode> jsonResponse = Unirest.post("http://mockbin.com/har")
2-
.header("Content-Type", "application/x-www-form-urlencoded")
3-
.body("foo=bar&hello=world")
4-
.asJson();
1+
HttpResponse<String> response = Unirest.post("http://mockbin.com/har")
2+
.header("content-type", "application/x-www-form-urlencoded")
3+
.body("foo=bar&hello=world")
4+
.asString();
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
HttpResponse<JsonNode> jsonResponse = Unirest.post("http://mockbin.com/har")
2-
.header("Content-Type", "application/json")
3-
.body("{\"number\": 1, \"string\": \"f\\\"oo\", \"arr\": [1, 2, 3], \"nested\": {\"a\": \"b\"}, \"arr_mix\": [1, \"a\", {\"arr_mix_nested\": {}}] }")
4-
.asJson();
1+
HttpResponse<String> response = Unirest.post("http://mockbin.com/har")
2+
.header("content-type", "application/json")
3+
.body("{\"number\": 1, \"string\": \"f\\\"oo\", \"arr\": [1, 2, 3], \"nested\": {\"a\": \"b\"}, \"arr_mix\": [1, \"a\", {\"arr_mix_nested\": {}}] }")
4+
.asString();
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
HttpResponse<JsonNode> jsonResponse = Unirest.post("http://mockbin.com/har")
2-
.header("Cookie", "foo=bar; bar=baz")
3-
.asJson();
1+
HttpResponse<String> response = Unirest.post("http://mockbin.com/har")
2+
.header("cookie", "foo=bar; bar=baz")
3+
.asString();
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
HttpResponse<JsonNode> jsonResponse = Unirest.post("http://mockbin.com/har?foo=bar&foo=baz&baz=abc&key=value")
2-
.header("Accept", "application/json")
3-
.header("Content-Type", "application/x-www-form-urlencoded")
4-
.header("Cookie", "foo=bar; bar=baz")
5-
.body("foo=bar")
6-
.asJson();
1+
HttpResponse<String> response = Unirest.post("http://mockbin.com/har?foo=bar&foo=baz&baz=abc&key=value")
2+
.header("cookie", "foo=bar; bar=baz")
3+
.header("accept", "application/json")
4+
.header("content-type", "application/x-www-form-urlencoded")
5+
.body("foo=bar")
6+
.asString();
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
HttpResponse<JsonNode> jsonResponse = Unirest.get("http://mockbin.com/har")
2-
.header("Accept", "application/json")
3-
.header("X-Foo", "Bar")
4-
.asJson();
1+
HttpResponse<String> response = Unirest.get("http://mockbin.com/har")
2+
.header("accept", "application/json")
3+
.header("x-foo", "Bar")
4+
.asString();
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
HttpResponse<JsonNode> jsonResponse = Unirest.post("http://mockbin.com/har")
2-
.header("Content-Type", "multipart/form-data")
3-
.body("-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"foo\"; filename=\"hello.txt\"\r\nContent-Type: text/plain\r\n\r\nHello World\r\n-----011000010111000001101001--")
4-
.asJson();
1+
HttpResponse<String> response = Unirest.post("http://mockbin.com/har")
2+
.header("content-type", "multipart/form-data; boundary=---011000010111000001101001")
3+
.body("-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"foo\"; filename=\"hello.txt\"\r\nContent-Type: text/plain\r\n\r\nHello World\r\n-----011000010111000001101001--")
4+
.asString();
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
HttpResponse<JsonNode> jsonResponse = Unirest.post("http://mockbin.com/har")
2-
.header("Content-Type", "multipart/form-data")
3-
.body("-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"foo\"; filename=\"hello.txt\"\r\nContent-Type: text/plain\r\n\r\n\r\n-----011000010111000001101001--")
4-
.asJson();
1+
HttpResponse<String> response = Unirest.post("http://mockbin.com/har")
2+
.header("content-type", "multipart/form-data; boundary=---011000010111000001101001")
3+
.body("-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"foo\"; filename=\"hello.txt\"\r\nContent-Type: text/plain\r\n\r\n\r\n-----011000010111000001101001--")
4+
.asString();
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
HttpResponse<JsonNode> jsonResponse = Unirest.post("http://mockbin.com/har")
2-
.header("Content-Type", "multipart/form-data")
3-
.body("-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"foo\"\r\n\r\nbar\r\n-----011000010111000001101001--")
4-
.asJson();
1+
HttpResponse<String> response = Unirest.post("http://mockbin.com/har")
2+
.header("content-type", "multipart/form-data; boundary=---011000010111000001101001")
3+
.body("-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"foo\"\r\n\r\nbar\r\n-----011000010111000001101001--")
4+
.asString();
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
HttpResponse<JsonNode> jsonResponse = Unirest.get("http://mockbin.com/har?foo=bar&foo=baz&baz=abc&key=value")
2-
.asJson();
1+
HttpResponse<String> response = Unirest.get("http://mockbin.com/har?foo=bar&foo=baz&baz=abc&key=value")
2+
.asString();

0 commit comments

Comments
 (0)