Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add java.net.http.HttpClient based snippet generator #170

Merged
merged 1 commit into from
Jul 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/targets/java/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ module.exports = {

okhttp: require('./okhttp'),
unirest: require('./unirest'),
asynchttp: require('./asynchttp')

asynchttp: require('./asynchttp'),
nethttp: require('./nethttp')
}
63 changes: 63 additions & 0 deletions src/targets/java/nethttp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* @description
* HTTP code snippet generator for Java using java.net.http.
*
* @author
* @wtetsu
*
* for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
*/

'use strict'

var CodeBuilder = require('../../helpers/code-builder')

module.exports = function (source, options) {
var opts = Object.assign(
{
indent: ' '
},
options
)

var code = new CodeBuilder(opts.indent)

code.push('HttpRequest request = HttpRequest.newBuilder()')
code.push(2, '.uri(URI.create("%s"))', source.fullUrl)

var headers = Object.keys(source.allHeaders)

// construct headers
if (headers.length) {
headers.forEach(function (key) {
code.push(2, '.header("%s", "%s")', key, source.allHeaders[key])
})
}

if (source.postData.text) {
code.push(
2,
'.method("%s", HttpRequest.BodyPublishers.ofString(%s))',
source.method.toUpperCase(),
JSON.stringify(source.postData.text)
)
} else {
code.push(2, '.method("%s", HttpRequest.BodyPublishers.noBody())', source.method.toUpperCase())
}

code.push(2, '.build();')

code.push(
'HttpResponse<String> response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());'
)
code.push('System.out.println(response.body());')

return code.join()
}

module.exports.info = {
key: 'nethttp',
title: 'java.net.http',
link: 'https://openjdk.java.net/groups/net/httpclient/intro.html',
description: 'Java Standardized HTTP Client API'
}
6 changes: 6 additions & 0 deletions test/fixtures/available-targets.json
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,12 @@
"title": "AsyncHttp",
"link": "https://github.com/AsyncHttpClient/async-http-client",
"description": "Asynchronous Http and WebSocket Client library for Java"
},
{
"key": "nethttp",
"title": "java.net.http",
"link": "https://openjdk.java.net/groups/net/httpclient/intro.html",
"description": "Java Standardized HTTP Client API"
}
]
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("http://mockbin.com/har"))
.header("content-type", "application/x-www-form-urlencoded")
.method("POST", HttpRequest.BodyPublishers.ofString("foo=bar&hello=world"))
.build();
HttpResponse<String> response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
7 changes: 7 additions & 0 deletions test/fixtures/output/java/nethttp/application-json.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("http://mockbin.com/har"))
.header("content-type", "application/json")
.method("POST", HttpRequest.BodyPublishers.ofString("{\"number\":1,\"string\":\"f\\\"oo\",\"arr\":[1,2,3],\"nested\":{\"a\":\"b\"},\"arr_mix\":[1,\"a\",{\"arr_mix_nested\":{}}],\"boolean\":false}"))
.build();
HttpResponse<String> response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
7 changes: 7 additions & 0 deletions test/fixtures/output/java/nethttp/cookies.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("http://mockbin.com/har"))
.header("cookie", "foo=bar; bar=baz")
.method("POST", HttpRequest.BodyPublishers.noBody())
.build();
HttpResponse<String> response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
6 changes: 6 additions & 0 deletions test/fixtures/output/java/nethttp/custom-method.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("http://mockbin.com/har"))
.method("PROPFIND", HttpRequest.BodyPublishers.noBody())
.build();
HttpResponse<String> response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
9 changes: 9 additions & 0 deletions test/fixtures/output/java/nethttp/full.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("http://mockbin.com/har?foo=bar&foo=baz&baz=abc&key=value"))
.header("cookie", "foo=bar; bar=baz")
.header("accept", "application/json")
.header("content-type", "application/x-www-form-urlencoded")
.method("POST", HttpRequest.BodyPublishers.ofString("foo=bar"))
.build();
HttpResponse<String> response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
8 changes: 8 additions & 0 deletions test/fixtures/output/java/nethttp/headers.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("http://mockbin.com/har"))
.header("accept", "application/json")
.header("x-foo", "Bar")
.method("GET", HttpRequest.BodyPublishers.noBody())
.build();
HttpResponse<String> response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
6 changes: 6 additions & 0 deletions test/fixtures/output/java/nethttp/https.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://mockbin.com/har"))
.method("GET", HttpRequest.BodyPublishers.noBody())
.build();
HttpResponse<String> response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
7 changes: 7 additions & 0 deletions test/fixtures/output/java/nethttp/jsonObj-multiline.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("http://mockbin.com/har"))
.header("content-type", "application/json")
.method("POST", HttpRequest.BodyPublishers.ofString("{\n \"foo\": \"bar\"\n}"))
.build();
HttpResponse<String> response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
7 changes: 7 additions & 0 deletions test/fixtures/output/java/nethttp/jsonObj-null-value.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("http://mockbin.com/har"))
.header("content-type", "application/json")
.method("POST", HttpRequest.BodyPublishers.ofString("{\"foo\":null}"))
.build();
HttpResponse<String> response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
7 changes: 7 additions & 0 deletions test/fixtures/output/java/nethttp/multipart-data.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("http://mockbin.com/har"))
.header("content-type", "multipart/form-data; boundary=---011000010111000001101001")
.method("POST", HttpRequest.BodyPublishers.ofString("-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"foo\"; filename=\"hello.txt\"\r\nContent-Type: text/plain\r\n\r\nHello World\r\n-----011000010111000001101001--\r\n"))
.build();
HttpResponse<String> response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
7 changes: 7 additions & 0 deletions test/fixtures/output/java/nethttp/multipart-file.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("http://mockbin.com/har"))
.header("content-type", "multipart/form-data; boundary=---011000010111000001101001")
.method("POST", HttpRequest.BodyPublishers.ofString("-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"foo\"; filename=\"hello.txt\"\r\nContent-Type: text/plain\r\n\r\n\r\n-----011000010111000001101001--\r\n"))
.build();
HttpResponse<String> response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
7 changes: 7 additions & 0 deletions test/fixtures/output/java/nethttp/multipart-form-data.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("http://mockbin.com/har"))
.header("content-type", "multipart/form-data; boundary=---011000010111000001101001")
.method("POST", HttpRequest.BodyPublishers.ofString("-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"foo\"\r\n\r\nbar\r\n-----011000010111000001101001--\r\n"))
.build();
HttpResponse<String> response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
6 changes: 6 additions & 0 deletions test/fixtures/output/java/nethttp/query.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("http://mockbin.com/har?foo=bar&foo=baz&baz=abc&key=value"))
.method("GET", HttpRequest.BodyPublishers.noBody())
.build();
HttpResponse<String> response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
6 changes: 6 additions & 0 deletions test/fixtures/output/java/nethttp/short.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("http://mockbin.com/har"))
.method("GET", HttpRequest.BodyPublishers.noBody())
.build();
HttpResponse<String> response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
7 changes: 7 additions & 0 deletions test/fixtures/output/java/nethttp/text-plain.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("http://mockbin.com/har"))
.header("content-type", "text/plain")
.method("POST", HttpRequest.BodyPublishers.ofString("Hello World"))
.build();
HttpResponse<String> response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
5 changes: 5 additions & 0 deletions test/targets/java/nethttp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict'

require('should')

module.exports = function (snippet, fixtures) {}