Skip to content

Commit 67db1ef

Browse files
author
Shashi Ranjan
committed
java module
1 parent 096ebfb commit 67db1ef

15 files changed

Lines changed: 112 additions & 0 deletions

‎src/targets/java/index.js‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
'use strict';
2+
3+
module.exports = require('require-directory')(module);
4+
5+

‎src/targets/java/info.js‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
'use strict';
2+
3+
module.exports = {
4+
key: 'java',
5+
title: 'JAVA',
6+
extname: '.java',
7+
default: 'unirest'
8+
};

‎src/targets/java/unirest.js‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
'use strict';
2+
3+
var util = require('util');
4+
5+
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+
};
45+
46+
47+
48+
module.exports.info = {
49+
key: 'unirest',
50+
title: 'JAVA',
51+
link : 'http://unirest.io/java.html',
52+
description : 'Unirest Java interface'
53+
};
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +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();
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +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();
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
HttpResponse<JsonNode> jsonResponse = Unirest.post("http://mockbin.com/har")
2+
.header("Cookie", "foo=bar; bar=baz")
3+
.asJson();
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +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();
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
HttpResponse<JsonNode> jsonResponse = Unirest.get("http://mockbin.com/har")
2+
.header("Accept", "application/json")
3+
.header("X-Foo", "Bar")
4+
.asJson();
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +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();
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +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();

0 commit comments

Comments
 (0)